Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.
Hi everyone,
I've been working on a game engine and I just recently made the movement time based, but I have no idea how to make my gravity time based. I've been using something like this which is called every frame:
and I have no clue how to make it time based. Multiplying the jumpspeed by the target fps divided by the current fps just makes you not jump as high. I've heard about a way to make time based realistic gravity by setting the y velocity to -9.8*time or something but what is "time" and how would I find it?
Just have a downwards force acting upon your player.
When you are touching the ground then you stop all velocity moving down.
I'm having some trouble with it as well...
"time" can literally be time elapsed between time(NULL) calls but the general rule is that if you can fake it well and fake it fast then don't spend the time doing it "real" and not-fast.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
I have a question for you and anybody else who bases their physics system on time. Why?
Why not just assume every frame is a unit of time? The calculations are easier (no numerical derivatives/integration for values), and everything is easier.
The acceleration due to gravity is -9.8j (j being the verticle component of the vector). You can calculate velocity from this by integrating. You can calculate position by integrating that again.
Okay, here's the calculus:
a(t) = -9.8 (constant acceleration)
v(t) = -9.8t + c (c is your constant of integration, being your initial velocity)
p(t) = -4.9t^2 + ct + c2 (ct being initial velocity times time, c2 being initial position).
Integrating acceleration gives us velocity, integrating velocity gives us position.
Well when I turn vsync off or I use a monitor with a different refresh rate and my app is running at 3500fps I don't want to guy to be moving around at the speed of light. :P
That's not the approach you want. You need to put something in your main loop that makes the entire frame take longer rather than basing every single thing off of time.
//this is all you need for time based movement...
//timems = Delta time. This is the amount of time thats passed since the last frame to this one
m_velocity.x += m_acceleration.x * timems;
m_velocity.y += m_acceleration.y * timems;
m_velocity.z += m_acceleration.z * timems;
m_position.x += m_velocity.x * timems;
m_position.y += m_velocity.y * timems;
m_position.z += m_velocity.z * timems;
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote:
<sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
I misinterpreted. Yeah, you are basing it off of the delta and are only using it to determine movement. I see people who use functions of total time to describe their physics as if they grabbed some equation straight from a physics book and didn't even think about it.
By adding the changes every frame (as you are doing), you are eliminating the parameter t from the physics formulas. Since a frame is equivalent to 1 of whatever the hell unit of time you are wanting to use, still having a parameter t in your equations is pointless.
M_D_K wrote:The whole point of time based movement is so it doesn't matter if you run at 30fps or 300fps you will move the same amount of distance in a second.
//this is all you need for time based movement...
//timems = Delta time. This is the amount of time thats passed since the last frame to this one
m_velocity.x += m_acceleration.x * timems;
m_velocity.y += m_acceleration.y * timems;
m_velocity.z += m_acceleration.z * timems;
m_position.x += m_velocity.x * timems;
m_position.y += m_velocity.y * timems;
m_position.z += m_velocity.z * timems;
Listen to M_D_K... he knows what he is talking about LOL he helped me out with my physics... granted it's not perfect (didn't start from scratch) but I now understand how it works.
For future reference, if you want to keep your actions independent of the rendering, I suggest you keep your functions for graphics rendering totally separated from your game actions. This will be useful for older computers that might suffer some slowdown in the graphics. Incidentally, this will lock the peak framerate of your program, but when slowdown occurs, game actions continue on their own, avoiding situations where everything, such as the gravity, is running in slo-mo.
Honestly, I intentionally design my projects so that a slowdown IS universal; I don't like a rendering slowdown to cause death because the rest of the game is still running and you can't see what the fuck is going on. xD
Though there are definately time and instances where I'd use your independant draw methods, namely network and socket apps.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
Joeyotrevor wrote:Well when I turn vsync off or I use a monitor with a different refresh rate and my app is running at 3500fps I don't want to guy to be moving around at the speed of light. :P