erratic jumping

Started by
11 comments, last by Neen10do 20 years, 11 months ago
i finally got my jumping for my dude to work... kinda sorta. when the player jumps, it sets the velocity at a constant rate, ie 80. but each time the player jumps, it goes to different heights. considering the velocity always starts the same, and there are no external factors, why wouldn''t it jump the same every time? here is some code input function
  
void cMainGame::CheckInput()
{
	//BUTTON PRESSES///////////

	static bool Z;
	///////////////////////////

 
        //this just keeps the player from going through the floor, for now. primiative collision.

	if(Player.mySprite.myY >= 396)
	{
		Player.myPhysics.vVelocity = 0;
		Player.mySprite.myY = 396;
		Player.myPhysics.jumping = false;
	}

	switch(GameMode)
	{
		//The Game is Running

		case MainGame:
			//Jump

			if(dk.KeyPressed(DIK_Z))
			{
				if(Z == false)
					if(Player.myPhysics.jumping == false)
					{
						Player.Jump(80);
					}
				Z = true;
			}
			else
			{
				Z = false;
			}

		break;
		default: break;
	};
  
here is the jump function
  
//Makes the object jump based on physics

void cObject::Jump(int velocity)
{
	myPhysics.jumping = true;
	myPhysics.vVelocity = velocity;
}
//////////////////////////////////////////

  
and here is the physics function that is applied to the object every frame. TimeDelta is the difference between frames, in milliseconds.
  
//APPLIES PHYSICS//////////////

void cPhysics::ApplyPhysics(float &Xpos, float &Ypos, DWORD TimeDelta)
{
	
	//if jumping

	if(jumping)
	{
		//Terminal velocity

		if(vVelocity > -100)
		{
			//Applies Gravity

			vVelocity = vVelocity + (int)GRAVITY * TimeDelta;
		}

		//Moves object

		Ypos = Ypos - (vVelocity / 10);
	}
}	
///////////////////////////////

  
can you guys find any reason my jumping would be erratic? ---------------------- i code therefore i am. Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
Advertisement
Not sure, but I think it''s because each time segment, you adjust the position by the final velocity, not the average velocity. Try storing the initial velocity in a temp variable before changing it, then use (velocity + initialVelocity) / 2 as the average.

How appropriate. You fight like a cow.
As well, if you're using time based scaling, you should be using floating point co-ordinates and values for everything or else you'll run into horrendous round-off errors. This might already be the case for this example.

Your assumption about "external factors" is inccorect in this case. The frame time is external, and without adequate precision, the error introduced by rounding the velocities and co-ordinates at EVERY FRAME will really throw off calculations.

Start by using floating points for anything time-scaled, then see if that fixes it. If not, come back here and post the new (modified) problem and code.

[edited by - AndyTX on May 5, 2003 5:29:04 PM]
No it''s far simpler than that. You''ve adjusted the velocity with deltatime but not the position.
ok i took both of your suggestions, but i still get erratic heights. here is the code:

INPUT

  void cMainGame::CheckInput(){	//BUTTON PRESSES///////////	static bool Z;	///////////////////////////	if(Player.mySprite.myY >= 396)	{		Player.myPhysics.vVelocity = 0;		Player.mySprite.myY = 396;		Player.myPhysics.jumping = false;	}	switch(GameMode)	{		//The Game is Running		case MainGame:			//Jump			if(dk.KeyPressed(DIK_Z))			{				if(Z == false)					if(Player.myPhysics.jumping == false)					{						Player.Jump(30);					}				Z = true;			}			else			{				Z = false;			}		break;		default: break;	};  

JUMP

  //Makes the object jump based on physicsvoid cObject::Jump(float velocity){	myPhysics.jumping = true;	myPhysics.vVelocity = velocity;	myPhysics.ivVelocity = velocity;}//////////////////////////////////////////  


the physics function

  //APPLIES PHYSICS//////////////void cPhysics::ApplyPhysics(float &Xpos, float &Ypos, DWORD TimeDelta){		//if jumping	if(jumping)	{		//Terminal velocity		if(vVelocity > -100)		{			//Applies Gravity			vVelocity = vVelocity + GRAVITY * TimeDelta;		}		//Moves object		Ypos = Ypos - (ivVelocity + vVelocity / 2) / 10;	}}	  


PS: is it wrong to divide by 10? it seems this is the only way i can slow things down.


}
///////////////////////////////////
[/source]


----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
"ivVelocity + vVelocity / 2"


Remember that division is higher precedence than addition. Stick some parentheses around that addition, mister.

How appropriate. You fight like a cow.

//if jumping
if(jumping)
{
//Terminal velocity
if(vVelocity > -40)
{
//Applies Gravity
vVelocity = vVelocity + GRAVITY * TimeDelta;
}

//Moves object
Ypos = Ypos - TimeDelta * ((ivVelocity + vVelocity) / 2);
}

i did both of your suggesstions, still no dice. sometimes he jumps and its really choppy, and some times he never jumps at all. he is all i am blitting to the screen, so time delta is small (0-2). anyone? this is really frustration.

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
Hmm why not change position directly? s = ut + 1/2*a*t^2

or

vectorPos = vectorPos * timeDelta + 0.5 * g * SQR(timeDelta);

here is my code under Kyo''s suggestion:

//APPLIES PHYSICS//////////////
void cPhysics::ApplyPhysics(float &Xpos, float &Ypos, DWORD TimeDelta)
{

//if jumping
if(jumping)
{
//Moves object
Ypos = Ypos * TimeDelta + (float).5 * GRAVITY * (TimeDelta * TimeDelta);

//Terminal velocity
if(vVelocity > -120)
{
vVelocity = -120;
}
}
}
///////////////////////////////

the only problem with this is it has no velocity in it!! i dont know what s = ut + 1/2*a*t^2 is but i implemented the other one you gave me and it just sticks the dude on the cealing forever.

is there anyone in the world who has gotten this to work!?!?! some sample code, your game, whatever it takes!! this should not be that hard!!!!


----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!


[edited by - Chigaimasu on May 5, 2003 9:03:06 PM]
Curse you windows and your poor explanation of my errors!!!

This topic is closed to new replies.

Advertisement