r/GraphicsProgramming • u/sprinklesday • Nov 14 '23
Determining meters per second in scaled terrain
I have a terrain in OpenGL with a width of 199m and height of 99m and I'm scaling this train by 500. What would be 1 meter in the new terrain, would it be 500? I'm trying to move my character in meters per second and trying to figure out the correct speed amount to move him in meters per second.
I'm also trying to generate a random number for footballs speeds, where the balls can be shot at you ranging from 30 m/s to 400 m/s but I'm quite confused on the measurements to determine m/s. I thought 500 would be correct for 1 meter in the new terrain but this would then mean I would need to generate a random value between 30*500 and 400 * 500 which seems to crash. Would appreciate any insight into this
// Random number generator
float s = Random(20.0f * 500.0f, 400.0f * 500.0f);
float Random(float min, float max)
{
float range = max - min;
float randomNumber = (float) random() / (float) RAND_MAX;
randomNumber *= range;
return randomNumber + min;
} 2
u/waramped Nov 15 '23
1 unit is whatever you want it to be. The speed of your player should have nothing to do with the scale of your terrain. If your terrain is 199*500 units, and you decide that 1 unit is 1 meter, then your terrain is 99,500meters wide.
If you want a random speed between 20 and 400 (which is over Mach 1 btw) then you just need a number between 20 and 400.
1
u/sprinklesday Nov 15 '23
Thanks for the reply! I wanted the movement to be meters per second. The original terrain before scaling would have 1 meter be just 1 unit but if you then scale the terrain by 500, it should now be that 1 meter = 500 units in the new terrain? The terrain is also centred after being scaled so instead of being 199*500 for width, it's actually 49500 as the maximum. I think this is where I'm getting confused as to how the meters per second is being changed
1
u/waramped Nov 15 '23
Ok I think we're missing some information here. The scale of an object doesn't change the scale of the global coordinates. 1 m/s is just 1 unit per second. What is happening that's making you think you need to scale your velocity?
1
u/sprinklesday Nov 15 '23
Thanks for getting back. It would not change it relative to the global coordinates but relative to the coordinates of that terrain, 1 meter would now be 500 right? I just have an issue where I don't feel my footballs, when ejected are not correctly taking into account the terrain scaled which means they're not moving as far as they should.
Currently I am generating a random speed between 30-400 m/s which would look correct on the original scale of the terrain but in my scaled terrain, they do not go as far. If I scale these by 500, my footballs just shoot into orbit so I feel like I am missing something to get these to look correct.
This is my current code:
float speed = RandomSpeed(60.0f, 300.0f);
velocity = {velocity * speed, velocity.y * speed, velocity.z * speed};
pos.x += m_velocity.x * deltaTime;pos.y += m_velocity.y * deltaTime + 0.5 * m_gravity * deltaTime * deltaTime;pos.z += m_velocity.z * deltaTime;velocity.x = m_velocity.x;velocity.y = m_velocity.y + m_gravity * deltaTime;velocity.z = m_velocity.z;1
u/waramped Nov 16 '23
It might help you to place some objects of a known size on the terrain to give you a perceptual sense of scale. Just a barren terrain is very hard to judge size and scale on because you have no references for your brain. To start, texture your terrain with a grid of 1 meter checkerboard patterns. 1 square = 1 square meter. This will give you a much better sense of how far and how fast things are going. Your terrain is 100km wide, so your footballs definitely wont go very far relative to that.
and in that code, why are you updating velocity with m_velocity? shouldn't it be the other way around?
1
u/sprinklesday Nov 16 '23
Thanks for getting back. They won't get very far relative to the same of the terrain with the current code but to make them do so, we'd multiply the random speed to be 30*500 and 300*500 since if we say 1 meter = 500 units then 30 meters would be 30*500 etc? I would try texture and visualise the size like you mentioned but im not sure how to do that in legacy OpenGL.
Also, you mentioned an issue with the code and my apologies, I made a mistake while re-typing it. It should actually be:
m_velocity.x = m_velocity.x;m_velocity.y = m_velocity.y + m_gravity * deltaTime;m_velocity.z = m_velocity.z;1
u/waramped Nov 16 '23
If you have fragment shaders then just output a color in the terrain pixel shader. Like
float check = floor(worldPos.x) + floor(worldPos.y);
check = frac(check * 0.5f) * 2.0f;
return check;This will give you a black and white checkerboard pattern in world space. (assuming Z is up for you, if not, just change the first line accordingly to get the horizontal plane)
If you don't have shaders, then just create some 1m cubes in the scene to give you a sense of the scale.
1
u/sprinklesday Nov 16 '23
Thanks for the reply. I don't currently have fragment shaders. If we assume 500 units is 1 meter, would I be correct in thinking that the speed range passed to RandomSpeed() would need to multiplied by 500 or do we account for the scale of the new terrain somewhere else?
1
u/waramped Nov 16 '23
If I'm understanding you correctly, you need to forget about the 500 thing altogether. The only thing you've done is rendered the terrain at a larger scale. Don't adjust anything else. Place some objects on the terrain of a known size to give yourself useful reference points. Then see if you think your velocities aren't correct. Take a video for us if you can.
1
u/sprinklesday Nov 18 '23
Thanks for the reply again. Think im going to take a step back and just re-think how im going about this. Thanks for all the help
8
u/Smashbolt Nov 14 '23
That is 100% up to you. No graphics API I'm aware of ascribes real-world units to any part of its math. World scaling like that tends to become problematic when the numbers you're working with require too much precision (specifically both very large and very small numbers have this problem). And that's only further compounded by your terrain density. If you decide 1 unit = 500m and you want your player to be say 1.6m tall, then your terrain will look terrible at character's eye view scale if your terrain mesh is all unit squares (eg: vertices are all whole numbers).
There is no way from what you've said that anyone would be able to tell you why a random number from 15000 to 200000 would cause a crash when used as a velocity.
Like... DirectX? OpenGL? Hand-made rasterizer? Raycasting? Is your terrain generating by deforming a plane mesh on the CPU or in a vertex shader? Heck, for all we can surmise, you might be using some premade terrain package in Unity or something.