Jumping

Started by Daslee, May 20, 2012, 10:32:15

Previous topic - Next topic

Daslee

Hello. Does someone have jumping physics in lwjgl? I tried to make it myself, but when i holding space and it jumps into air, it wont get back on the ground while im holding space. So it doesn't worked and i removed that code... Or maybe you can help me to make it? :)

EDIT: Now i made my code again in which it won't go back on ground while im holding space. Here is my code:
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE) == false && camera.position.y > ground){ //ground = -1.2f;
	camera.position.y -= 0.0215;
	camera.jumped = 1;
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
	camera.jump(3);
	camera.jumped = 0;
}

In camera class:
public void jump(float distance){
	if(position.y < distance){
		position.y += 0.0087;
	}else{
		jumped = 1f;
		position.y -= 0.0215;
	}
}

Mickelukas

Instead of looking for if the key is down or up, look for clicks.

Mike

Daslee

So then it jumps very very small in air (position.y += 0.0175;) and gets back on the ground. Need to make somehow to do loop in increase y coordinates (in jump method).

EDIT: Fixed it. Thats what i used:

if(jumping){
	        	if(camera.position.y < 5){
		        	camera.position.y += 0.0175;
	        	}else{
	        		jumping = false;
	        		goingdown = true;
	        	}
	        }
	        if(jumping == false && goingdown){
	        	if(camera.position.y > ground){
	        		camera.position.y -= 0.0240;
	        	}else{
	        		goingdown = false;
	        	}
	        }

Simon Felix

Usually (simplified pseudo code):

final double TIME_STEP=1/60.0;
final double GRAVITY=9.81;
boolean jumping;
double yPosition;
double yVelocity;

void perFrame()
{
  if(jumpKeyIsPressed() && !jumping)
  {
    jumping=true;
    yVelocity=-1;
  }
  
  if(playerIsOnTheFloor())
  {
    jumping=false;
    yVelocity=0;
  }
  
  yPosition+=yVelocity*TIME_STEP;
  yVelocity+=GRAVITY*TIME_STEP;
}
Download Cultris II, the fastest Tetris clone from http://gewaltig.net/

Daslee

Quote from: Simon Felix on May 21, 2012, 02:02:53
Usually (simplified pseudo code):

final double TIME_STEP=1/60.0;
final double GRAVITY=9.81;
boolean jumping;
double yPosition;
double yVelocity;

void perFrame()
{
  if(jumpKeyIsPressed() && !jumping)
  {
    jumping=true;
    yVelocity=-1;
  }
  
  if(playerIsOnTheFloor())
  {
    jumping=false;
    yVelocity=0;
  }
  
  yPosition+=yVelocity*TIME_STEP;
  yVelocity+=GRAVITY*TIME_STEP;
}


Look, i put these lines at the top of code:
final double TIME_STEP = 1 / 60.0;
    final double GRAVITY = 9.81;
    boolean jumping;
    double yVelocity;


And this code:
while(Keyboard.next()){
	        	if(Keyboard.getEventKeyState()){
	        		if(Keyboard.getEventKey() == Keyboard.KEY_SPACE && !jumping){
	        			jumping = true;
	        			yVelocity =- 1;
	        		}
	        	}
	        }

if(camera.position.y == ground){
    			jumping = false;
    			yVelocity = 0;
    		}
	        
    		camera.position.y += yVelocity * TIME_STEP;
    		yVelocity += GRAVITY * TIME_STEP;


In while(!Display.isCloseRequested())
But why it doesn't do anything when i click space?

ground is -1.2f and camera.position.y is -1.2f

EDIT: I fixed it! Maked boolean if(jumped) for those lines:
camera.position.y += yVelocity * TIME_STEP;
    		yVelocity += GRAVITY * TIME_STEP;


And changed
jumping = true;
	        			yVelocity =- 1; //This changed to: 2 and not -2


And here:
yVelocity += GRAVITY * TIME_STEP;

Changed instead of add to subtract

Thanks for code Simon Felix ! :)