I am new to Libgdx and I am a little confused on the best way to do collision/movement. And it is bothering me a lot and I cant seem to move on from there unless I know I am not doing it the wrong way.
This is my current classes that I am using.
public class Play extends GameState{
public Play(GameStateManager {
super(gsm);
world = new World(new Vector2(0, -9.81f), true);
b2dr = new Box2DDebugRenderer();
cl = new MyContactListener();
world.setContactListener(cl);
createPlayer();
b2dCam = new OrthographicCamera();
b2dCam.setToOrtho(false, Game.V_WIDTH / PPM, Game.V_HEIGHT / PPM);
}
public void update(float dt) {
handleInput();
player.update(Gdx.graphics.getDeltaTime());
world.step(dt, 6, 2);
}
public void render() {
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
tmr.setView(cam);
tmr.render();
sb.setProjectionMatrix(cam.combined);
player.render(sb);
b2dr.render(world, b2dCam.combined);
}
public void handleInput() {
if(MyInput.isPressed(MyInput.BUTTON1)){
if(cl.isPlayerOnGround()){
player.getBody().applyForceToCenter(0, 150, true);
}
}
if(MyInput.isDown(MyInput.BUTTON2)){
if(cl.isPlayerOnGround())
player.setVelocity(new Vector2(1, player.getVelocity().y));
else if(Math.abs(player.getVelocity().y) < .3)
player.getBody().applyLinearImpulse(.15f, 0, player.getPosition().x, player.getPosition().y, true);
}else{
if(MyInput.isDown(MyInput.BUTTON3)){
if(cl.isPlayerOnGround()) {
player.setVelocity(new Vector2(-1, player.getVelocity().y));
}else if(Math.abs(player.getVelocity().y) < .3){
player.getBody().applyLinearImpulse(-.15f, 0, player.getPosition().x, player.getPosition().y, true);
}
}else{
if(cl.isPlayerOnGround())
player.setVelocity(new Vector2(0, player.getVelocity().y));
}
}
}
}
My Player class extends this
public class AnimationHandler {
protected Body body;
protected Animation animation;
protected float width;
protected float height;
private float time;
public AnimationHandler(Body body){
this.body = body;
animation = new Animation(1/12f);
}
public void setAnimation(TextureRegion[] region, float delay){
animation = new Animation(delay, region);
width = region[0].getRegionWidth();
height = region[0].getRegionHeight();
}
public void render(SpriteBatch sb){
time += Gdx.graphics.getDeltaTime();
sb.begin();
sb.draw(
animation.getKeyFrame(time, true),
body.getPosition().x * B2DVars.PPM - width / 2,
body.getPosition().y * B2DVars.PPM - width / 2
);
sb.end();
}
public Body getBody() {
return body;
}
So the main thing I wanted to take a look at is in The first class (Play) method handleInput(). I am currently using player.setVelocity() as movement. What this does for me is it automatically does collision handling for walking left and right. And it works pretty well I admit. But I want someones opinion on this, I keep having a feeling there are flaws in the way I am doing it. One flaw I was thinking about is if I have a platformer type game and want to jump up through a platform but land on it when I come back down I am not sure that is possible.. Because it is doing the collision itself instead of manually doing it. Any thoughts? Any input would be really appreciated, I am new to game programming and I want to be doing things the best way possible.