Jump physics don't work right.

I can’t get my player to go in a arc while jumping it always turns into a ^ motion. Here’s what I have for physics.


public void update(Map m, Enemy[] e1, int max_enemies, Item[] i1, int max_Items){
		//System.out.println("x: "+(int) (x/m.tileWidth));
		//System.out.println("y: "+(int) ((y/m.tileHeight) - 0.1));
		if(state != 2)
		{
			if(jumping){
				if(jumpdist <= jumpheight && m.tiles[(int) (x/m.tileWidth)][(int)  ((y/m.tileHeight))] == 0 &&
					jumpdist <= jumpheight && m.tiles[(int) (x/m.tileWidth)][(int)  ((y + height)/m.tileHeight)] == 0 &&
					jumpdist <= jumpheight && m.tiles[(int) ((x + width)/m.tileWidth)][(int)  ((y/m.tileHeight))] == 0 &&
					jumpdist <= jumpheight && m.tiles[(int) ((x + width)/m.tileWidth)][(int)  ((y + height)/m.tileHeight)] == 0 &&
					!colbottom(i1, max_Items)){
					velo += jumpspeed;
					jumpdist++;
				}
				
				else
					jumping = false;
					
			}
	
			if(m.tiles[(int) ((x/m.tileWidth) + 0.1)][(int) ((y/m.tileHeight) - 0.0005)] == 0 &&
			   m.tiles[(int) ((((x + width)/m.tileWidth)) - 0.1)][(int) ((y/m.tileHeight) - 0.0005)] == 0 &&
			   	!coltop(i1, max_Items) && !jumping){
				falling = true;
				velo -= grav;
			}
			
			else falling = false;
			
			y = velo;
			
			if(collision(e1,max_enemies))
			{
				state = 2;
				frame = 0;
			}
		}
	}

When I did something like this, I started with a certain jump height and subtracted a little off it every game loop. Eventually it became negative and the player dropped. I was working with a y positive, of course.

I’ll take a stab at the pseudo code as I’m going to write something similar in my next game I think.

Basically you want to accelerate at a decreasing rate. X axis does not matter.

speedY = 12; //pixels speed or delta or acceleration whatever you want to call it.
VELOCITY_Y = 5; //base movement speed

//jump loop
if (timer > 20)
–speedY; //Every 20 miliseconds your speed decreases by 1.

//set max fall speed
if (speedY < -12)
speedY = -12;

velocity = VELOCITY_Y + speedY;

// modify velocity by timelapse frame rate move.

setY(velocity)

Should be something like that. You could get more complex I’m sure.

This worked, thanks!

In other words, you’re giving the player a velocity and then adjusting it. Thinking of it using velocity is a lot nicer and more robust. If you’ve learned any sort of physics, you should understand that position is the derivative of velocity which is the derivative of acceleration, i.e. position is changed by velocity and velocity is changed by acceleration. Gravity constantly applies downward acceleration, when you jump you apply a very immediate and large upwards acceleration, which will be much greater than gravity initially, but because it is only applied once gravity will eventually overcome that boost.

ex.
while (gameIsRunning)
{
//Initial jump.
if (jumping && isOnGround)
{
velocity.y += 100;
}

 //Gravity.
 velocity.y -= 9.8;

 //Adjust position by velocity.
 position += velocity;

}

That’s a good explanation of it. I think there is value, also, in doing things intuitively. When I was doing this, I didn’t think about gravity or velocity or anything. I only thought about the result I wanted and what would bring it. Sometimes doing things that way can give out of the box solutions that can be interesting.

Well, I don’t think I’ve ever done a project where I was able to get away with just using accurate physics, anyway, but it’s logical to keep things constrained in a pos/vel/acc system because you can re-use it in every single game. Much more importantly, however, it’s very very clear exactly how and why the character is moving a certain way. If you want him to hang in the air longer, you reduce gravitational acceleration. If you want him to jump higher, you give a higher jump velocity.

It’s simple and compartmentalized. Intelligently encapsulated. Multi-use. Good things in an OOP environment.

Yes, that’s true. It’s kind of like path finding. You can reuse it in a lot of programs. I think a full blown physics system is overkill for many games, so something like that would be very useful. I might work something like that out the next time I need it. I’d like to have some base code, eventually, so I could put a game together in a lot less time. Right now, I’m just getting used to things in Java. A simple physics class would be nice to have.

[quote=“Demonpants,post:5,topic:33433”]
Minor correction :slight_smile:
acceleration is the derivative of velocity, which is the derivative of position, “i.e. position is changed by velocity and velocity is changed by acceleration”

Ha ha true. :stuck_out_tongue: