I am working in a RPG 2d in libgdx/java using Box2d.
I have the main character and other 3 following. I would like them to follow the same path of the main character in line like a snake(same as Master System / Phantasy Star and other old RPG) to avoid messing up when found a narrow entrance.
In my PlayScreen class I call all characters individually.
private static Alis _player;
private static Myau _follower;
private static Odin _follower1;
private static Noah _follower2;
on the Constructor PlayScreen
_player = new Alis(world, this, mapManager);
_follower = new Myau(world, this, mapManager);
_follower1 = new Odin(world, this, mapManager);
_follower2 = new Noah(world, this, mapManager);
HandleInput(float dt) method I only move the main Character
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) && _player.b2body.getLinearVelocity().x <= 0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(0.05f, 0), _player.b2body.getWorldCenter(), true);
}else if(Gdx.input.isKeyPressed(Input.Keys.LEFT) && _player.b2body.getLinearVelocity().x >= -0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(-0.05f, 0), _player.b2body.getWorldCenter(), true);
}else if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) == Gdx.input.isKeyPressed(Input.Keys.LEFT)){
_player.b2body.setLinearVelocity(0f, _player.b2body.getLinearVelocity().y);
}
if(Gdx.input.isKeyPressed(Input.Keys.UP) && _player.b2body.getLinearVelocity().y <= 0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(0, 0.05f), _player.b2body.getWorldCenter(), true);
} else if(Gdx.input.isKeyPressed(Input.Keys.DOWN) && _player.b2body.getLinearVelocity().y >= -0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(0, -0.05f), _player.b2body.getWorldCenter(), true);
} else if(Gdx.input.isKeyPressed(Input.Keys.UP) == Gdx.input.isKeyPressed(Input.Keys.DOWN)){ _player.b2body.setLinearVelocity(_player.b2body.getLinearVelocity().x, 0f);
}
update(float dt) method
handleInput(dt);
_player.update(dt);
_follower.update(dt, _player.b2body.getPosition());
_follower1.update(dt, _follower.b2body.getPosition());
_follower2.update(dt, _follower1.b2body.getPosition());
render(float delta) method
game.batch.begin();
_follower2.draw(game.batch);
_follower1.draw(game.batch);
_follower.draw(game.batch);
_player.draw(game.batch);
game.batch.end();
dispose() method
_player.dispose();
_follower.dispose();
_follower1.dispose();
_follower2.dispose();
Now, inside my characters class, on update(float dt, Vector2 position) method
if ((position.x - b2body.getPosition().x) > (15 /PhantasyStar.PPM)) {
b2body.setLinearVelocity(new Vector2(0.5f, 0));
} else if ((position.x - b2body.getPosition().x) < ( - 15 /PhantasyStar.PPM)) {
b2body.setLinearVelocity(new Vector2(-0.5f, 0));
} else if ((position.y - b2body.getPosition().y) > (15 / PhantasyStar.PPM)) {
b2body.setLinearVelocity(new Vector2(0, 0.5f));
} else if ((position.y - b2body.getPosition().y) < (-15 / PhantasyStar.PPM)) {
b2body.setLinearVelocity(new Vector2(0, -0.5f));
} else if (((position.y - b2body.getPosition().y) <= (15 / PhantasyStar.PPM) && (position.y - b2body.getPosition().y) >= (-15 / PhantasyStar.PPM)) || ((position.x - b2body.getPosition().x) <= (15 /PhantasyStar.PPM) && (position.x - b2body.getPosition().x) >= (-15 /PhantasyStar.PPM)) ) {
b2body.setLinearVelocity(new Vector2(0, 0));
}
setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 4);
setRegion(getFrame(dt));
This code gave me a good result, for example, if I walk only on x-axis or only y-axis, the players walk exactly how I need with a correct distance from each other and coming back correctly too, but if I start to move up and down they start to follow each other but every time I found an obstacle, some of the followers stuck. So I want them to follow the exactly path the main character leads.
I am 100% what I did is not correct, but I tried to find some literature for this and I didn't find. I believe if I found a way to get the last position of the main player and make it a target for the second.
I tried to collect after update the player position to be the last position and used on the next update in the follower, however, even I ask the follower to follow that position, it is coming too close, so I assume it is not the correct position to collect, or maybe it is a different way to do it. Should I need to create an Array to collect all the positions and pass this info to the other characters with delay?
It is somebody with more experience in game development to help me?