#### Archived

This topic is now archived and is closed to further replies.

# 2d game jump

This topic is 5536 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a 2d game, and I have problems coding the jump, First: Its absolutely necesary the time variable of v = v0 + at / v = v0 + gt (g = -9.8) ? can be made a jump without knowing anything time information? Second: The input system affect in any way the behaviour and coding of the jump routine? this means, its the same program with an event-driven system, or a boolean array of the keyboard like in msdos ... ?

What?

##### Share on other sites
1) the time variable determines how long the acceleration has been applied, which directly affects the speed of the object falling.

2) huh? it is up to you how you implement this...

##### Share on other sites
trying to do it without time would be virtually impossible if you want it to look ''right''.

You could look into Verlet integration meaning that you don''t store velocities, the velocity is implied from the relative postions of an object over two frames, but there is still a notion of time (between the two updates).

To use the keys as an event system within the jump code you will need to react to the ''jump'' key by setting the player velocity to some number (opposite direction to the gravity acceleration 9.8) so that the player starts of rising quickly, but due to the gravity will eventually stop in mid air and then start accelerating towards the ground.

No idea if this helps or not...

##### Share on other sites
yes it helps, do you know how this time variable is usually used?, I mean, is in seconds, ms,... how much times must you request for system time,... ??

##### Share on other sites
you save the time every frame, and subtract from it the time in the last frame. this gives you the time elapsed since last frame, and you use this time in your equations.

search through the "articles & resources" section, and the forums, for details about getting the time on different systems.

##### Share on other sites
A lot of games don''t use realistic jumping physics at all. Instead they just keep the character moving upward until the player releases the button or the maximum height is reached and then decelerate the jumper and apply either realistic or more unrealistic physics for the fall.

##### Share on other sites
i had the same issue with one of my demos.

ill save you time, and give you my physics engine (primarily for 2d side scrolling jumpers). i think all that is implimented is jumping (vertical velocity).

this is cPhysics.h
//PHYSICS ENGINE////////////////////////includes///////////////////#include <windows.h>/////////////////////////////#pragma once//Physics engine class///////////////////////class cPhysics{public:	//Velocities	float             vVelocity;//vertical	float             hVelocity;//horizontal	//Terminal Velocities	float             vTerminal;//has to be a negative value	//Object properties	bool              jumping;	float             jump_power;//how high they jump 5-8	float             walk_power;//how fast they walk 1-3	//Timing Properties	DWORD             LastUpdate;	//Functions	void ApplyPhysics(float &Xpos, float &Ypos, DWORD TimeInterval);	//Constructor	cPhysics();};/////////////////////////////////////////////

this is the cpp file
//PHYSICS IMPLEMENTATION#include "cPhysics.h"//CONSTANTS////////////////////const float GRAVITY = (float)-0.098;/////////////////////////////////CONSTRUCTOR//////////////////cPhysics::cPhysics(){	vVelocity = 0;	hVelocity = 0;	jumping = false;	LastUpdate = timeGetTime();}/////////////////////////////////APPLIES PHYSICS////////////////////////////////X and Y are the float variables of your sprite position.//TimeInterval is a DWORD, which should be the time between //frames, calculated by your timer (timeGetTime()) in mine.//jumping is a bool value, so you can turn it off or not.////////void cPhysics::ApplyPhysics(float &Xpos, float &Ypos, DWORD TimeInterval){	//if(timeGetTime() - LastUpdate >= TimeInterval)	//{		//ifjumping/////////////////////////////////////////		if(jumping)		{			//Terminal velocity			if(vVelocity < vTerminal)			{				vVelocity = vTerminal;			}			else			{					vVelocity += GRAVITY * TimeInterval / 12;			}			//Moves object				Ypos -= vVelocity * TimeInterval / 12;		}		/////////////////////////////////////////////////////	}}	///////////////////////////////

the constant, GRAVITY, you may have to adjust depending on how you want your guys to jump, but that is what jump power is.

each object, sprite, whatever should have a cPhysics component in them. to make them jump, you set the jumping bool value to true, set their vVelocity to their jump power, and tada! They''ll jump. when they''ve collided into something, turn jumping to false, and make their vVelocity automatically equal 0. the terminal velocity is to keep them from falling at a crazy rate. in my game i set it to -180, IT ALWAYS HAS TO BE A NEGATIVE VALUE.

1. 1
2. 2
3. 3
Rutin
16
4. 4
JoeJ
13
5. 5

• 10
• 9
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632646
• Total Posts
3007637
• ### Who's Online (See full list)

There are no registered users currently online

×