Time-based Gravity

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.

Moderator: PC Supremacists

Post Reply
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Time-based Gravity

Post by Joeyotrevor »

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:

Code: Select all

this->gravity++;
	if(this->gravity != 0)
	{
		for(int x = 0; x < abs(this->gravity); x++)
		{
			if(this->gravity > 0)
			{
				this->y -= NV_P_JUMPSPEED;
			}
			if(this->gravity < 0)
			{
				this->y += NV_P_JUMPSPEED;
			}
			if(this->gravity < 0 && this->CheckCollision())
			{
				while(this->CheckCollision())
				{
					this->y -= NV_P_FALLSPEED;
				}
				this->gravity = 0;
				break;
			}
			if(this->gravity > 0 && this->CheckCollision())
			{
				while(this->CheckCollision())
				{
					this->y += NV_P_JUMPSPEED;
				}
				this->gravity = 0;
				break;
			}
		}
	}
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?

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Time-based Gravity

Post by LeonBlade »

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...
There's no place like ~/
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Time-based Gravity

Post by MarauderIIC »

this->yvelocity -= gravity;
this->yposition += yvelocity;

?

"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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Time-based Gravity

Post by Falco Girgis »

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.
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: Time-based Gravity

Post by Joeyotrevor »

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

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Time-based Gravity

Post by Falco Girgis »

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.

Basically:

Code: Select all

for(;;) { //heartbeat

    GetInput()
    Render()
    DoShit()

   //DO NOTHING UNTIL A CERTAIN AMOUNT OF TIME PASSES.
}
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Time-based Gravity

Post by M_D_K »

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.

anyways

Code: Select all

//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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Time-based Gravity

Post by Falco Girgis »

edit: nevermind.

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.

But yeah, your answer is what he was looking for.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Time-based Gravity

Post by M_D_K »

OH YEAH! I was helpful :)
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.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Time-based Gravity

Post by LeonBlade »

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.

anyways

Code: Select all

//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.
There's no place like ~/
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: Time-based Gravity

Post by Joeyotrevor »

Thanks guys!

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: Time-based Gravity

Post by CC Ricers »

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.

Code: Select all

frameTime = 20; // in milliseconds
while(running) {

    currentTime = getMilliseconds();
    timeCount += (currentTime - lastTime);
    lastTime = currentTime;

    if(timeCount > frameTime) {
        display();
	// Continuously take events
        while(timeCount > frameTime) {
            handleActions();
            timeCount -= frameTime;
        }
    } else {
        // optional sleep function, to conserve CPU usage
    }
}
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Time-based Gravity

Post by Arce »

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
systat
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Sun Jan 04, 2009 3:27 pm

Re: Time-based Gravity

Post by systat »

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
Frame limiter, does it ring the bell?


Sorry for my bad english.
Image
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Time-based Gravity

Post by RyanPridgeon »

Commercial games control speed with time by using the time elapsed since the last time the player moved.

a shoddy way of explaining it would be;
change in position = movespeed x time

For instance, if it has been 5 ticks since last time, move the player 10 pixels, if it has been 10 ticks, move the player 20 pixels.

You can get the total ticks since the start of the program in SDL by using SDL_GetTicks()
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply