I put always the library dot I use, but I don’t know why not this time. :yawn:
I’m using libGDX and physics Box2D.
I have a class for the player control:
package com.unclain.udevelop.prueba1.utils;
import java.util.List;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.WorldManifold;
public class CharacterController {
private boolean jump = false;
private boolean grounded = false;
private final static float MAX_VELOCITY = 7f;
float stillTime = 0;
long lastGroundTime = 0;
public Body bodyPlayer;
private FixtureDef fixtureDefPlayer;
private Fixture fixturePlayer;
private MovingPlatform groundedPlatform = null;
private ContactCheck checker;
private World world;
private FPSLogger fps;
private BitmapFont font;
public CharacterController(World world, Body bodyPlayer, Fixture fixturePlayer, FixtureDef fixtureDefPlayer){
this.bodyPlayer = bodyPlayer;
this.fixtureDefPlayer = fixtureDefPlayer;
this.fixturePlayer = fixturePlayer;
this.world = world;
addKeyControls();
fps = new FPSLogger();
System.out.println("Jump = " + jump);
checker = new ContactCheck(world, bodyPlayer, fixturePlayer);
font = new BitmapFont();
}
public void addKeyControls(){
Vector2 vel = bodyPlayer.getLinearVelocity();
Vector2 pos = bodyPlayer.getPosition();
grounded = isGrounded(Gdx.graphics.getDeltaTime());
fixtureDefPlayer.density = 0.0f;
fixtureDefPlayer.restitution = 0.00000f;
if(grounded){
lastGroundTime = System.nanoTime();
} else {
if(System.nanoTime() - lastGroundTime < 100000000){
grounded = true;
}
}
if(grounded && stillTime > 0.1){
}
if(Math.abs(vel.x) > MAX_VELOCITY){
vel.x = Math.signum(vel.x) * MAX_VELOCITY;
bodyPlayer.setLinearVelocity(vel.x, vel.y);
}
if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D)) {
stillTime += Gdx.graphics.getDeltaTime();
bodyPlayer.setLinearVelocity(vel.x * 0.9f, vel.y);
}
else {
stillTime = 0;
}
if(!grounded) {
fixturePlayer.setFriction(100f);
} else {
if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D) && stillTime > 0.2) {
fixturePlayer.setFriction(100f);
}
else {
fixturePlayer.setFriction(0.2f);
}
if(groundedPlatform != null && groundedPlatform.dist == 0) {
bodyPlayer.applyLinearImpulse(0, -24, pos.x, pos.y);
}
}
if(Gdx.input.isKeyPressed(Keys.A) && vel.x > -MAX_VELOCITY) {
bodyPlayer.applyLinearImpulse(-2f, 0, pos.x, pos.y);
}
if(Gdx.input.isKeyPressed(Keys.D) && vel.x < MAX_VELOCITY) {
bodyPlayer.applyLinearImpulse(2f, 0, pos.x, pos.y);
}
if(Gdx.input.isKeyPressed(Keys.W)){
jump = true;
}
if(jump) {
jump = false;
if(grounded == true) {
bodyPlayer.setLinearVelocity(vel.x, 0);
System.out.println("jump before: " + bodyPlayer.getLinearVelocity());
bodyPlayer.setTransform(pos.x, pos.y + 0.01f, 0);
bodyPlayer.applyLinearImpulse(0, 10, pos.x, pos.y);
System.out.println("jump, " + bodyPlayer.getLinearVelocity());
}
}
}
public boolean isGrounded(float deltaTime) {
groundedPlatform = null;
List<Contact> contactList = world.getContactList();
for(int i = 0; i < contactList.size(); i++) {
Contact contact = contactList.get(i);
if(contact.isTouching() && (contact.getFixtureA() == fixturePlayer)) {
Vector2 pos = bodyPlayer.getPosition();
WorldManifold manifold = contact.getWorldManifold();
boolean below = true;
for(int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
below &= (manifold.getPoints()[j].y < pos.y - 1.5f);
}
if(below) {
if(contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) {
groundedPlatform = (MovingPlatform)contact.getFixtureA().getBody().getUserData();
}
if(contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) {
groundedPlatform = (MovingPlatform)contact.getFixtureB().getBody().getUserData();
}
return true;
}
return true;
}
}
return false;
}
public void drawFont(SpriteBatch batch){
bodyPlayer.setAwake(true);
batch.begin();
font.drawMultiLine(batch, "friction: " + fixturePlayer.getFriction() + "\ngrounded: " + grounded + "\njump: " +
jump + "\nFPS: " + Gdx.graphics.getFramesPerSecond() + "\nrestitution: " + fixtureDefPlayer.restitution +
"\ndensity: " + fixtureDefPlayer.density + "\nawake: " + bodyPlayer.isAwake() + "\nstilltime: " + stillTime, 15, Gdx.graphics.getHeight() - 15);
batch.end();
}
}
P.D: It’s the first time dot anybody reply me with spanish, hahahaha. Your spanish answer it’s perfect, but before English you need put “el”, we are at peace. ;D
Thanks for reply me. Good luck with your projects. 