Hi, I’m tryin dot my character jump, but I can’t do correctly it. I was experimenting per days how do it, but I can’t do a correct jump…
I have a class for control the Player movements, so it is this:
package com.unclain.udevelop.prueba1.utils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.unclain.udevelop.prueba1.contacts.ContactCheck;
public class CharacterController {
private boolean jump = false;
private static final int MAX_VELOCITY = 1002;
private Body bodyPlayer;
private FixtureDef fixtureDefPlayer;
private Fixture fixturePlayer;
private boolean isPlayerGrounded;;
private Vector2 vel;
private ContactCheck checker;
private World world;
private FPSLogger fps;
public CharacterController(World world, Body bodyPlayer, Fixture fixturePlayer, FixtureDef fixtureDefPlayer){
this.bodyPlayer = bodyPlayer;
this.fixtureDefPlayer = fixtureDefPlayer;
this.fixturePlayer = fixturePlayer;
vel = bodyPlayer.getLinearVelocity();
this.world = world;
addKeyControls();
fps = new FPSLogger();
System.out.println("¿is Grounded? (CharacterController) " + isPlayerGrounded);
}
public void addKeyControls(){
if(Gdx.input.isKeyPressed(Keys.D)){
bodyPlayer.applyLinearImpulse(new Vector2(400, 0), bodyPlayer.getPosition());
} else if(Gdx.input.isKeyPressed(Keys.A)){
bodyPlayer.applyLinearImpulse(new Vector2(-400, 0), bodyPlayer.getPosition());
} else if(Gdx.input.isKeyPressed(Keys.W)){
jump = true;
jumpControls();
} else {
bodyPlayer.getLinearVelocity().x = 0;
}
}
public void jumpControls(){
checker = new ContactCheck(world, bodyPlayer, fixturePlayer);
isPlayerGrounded = checker.isGrounded();
vel = bodyPlayer.getLinearVelocity();
if(isPlayerGrounded = true){
fixtureDefPlayer.friction = 1f;
}
if(jump){
fixtureDefPlayer.friction = 0f;
fixtureDefPlayer.density = 0f;
fixtureDefPlayer.restitution = 0f;
bodyPlayer.applyLinearImpulse(new Vector2(0.01f, 2000), bodyPlayer.getPosition());
System.out.println("Jump = " + jump);
jump = false;
bodyPlayer.getLinearVelocity().x += (Gdx.graphics.getDeltaTime() * 20);
} else if(isPlayerGrounded = false){
fixtureDefPlayer.friction = 1000f;
bodyPlayer.getLinearVelocity().y -= world.getGravity().y - (Gdx.graphics.getDeltaTime() * 30);
bodyPlayer.getLinearVelocity().x -= (Gdx.graphics.getDeltaTime() * 30);
System.out.println("Vel.x " + bodyPlayer.getLinearVelocity().x + "Vel.y " + bodyPlayer.getLinearVelocity().y);
}
}
}
Can somebody give me a example of how to do it?
¡Thanks!