I've created 2 buttons for movement in my game but when those button are being pressed, the character gets basically thrown away and by pressing only once the movement it's continuous and i don't understand with box2D what's the right way to handle the movement of the character
The code looks something like this:
public class PlayScreen implements Screen {
private SuperShooter game;
private OrthographicCamera gamecam;
private Viewport gamePort;
private Hud hud;
private OrthographicCamera guicam;
Rectangle wleftBounds;
Rectangle wrightBounds;
//Vector3 for keeping the coordinates
Vector3 touchPoint;
//Texture for touch buttons
Texture leftRegion;
Texture rightRegion;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
//Box2D Variables
private World world;
private Box2DDebugRenderer b2dr;
private John player;
public PlayScreen(SuperShooter game){
this.game = game;
gamecam = new OrthographicCamera();
gamePort = new StretchViewport(SuperShooter.V_WIDTH / SuperShooter.PPM, SuperShooter.V_HEIGHT / SuperShooter.PPM, gamecam);
hud = new Hud(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("Level1WithObjects2.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1/SuperShooter.PPM);
gamecam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);
world = new World(new Vector2(0,-15), true);
new B2WorldCreator(world, map);
b2dr = new Box2DDebugRenderer();
player = new John(world);
guicam = new OrthographicCamera(480, 320);
guicam.position.set(480/2F, 320/2F, 0);
wleftBounds = new Rectangle(0,0,80,25);
wrightBounds = new Rectangle(80, 0, 80,25);
//creating the vector
touchPoint = new Vector3();
//Instantiating Textures
leftRegion = new Texture(Gdx.files.internal("back.png"));
rightRegion = new Texture(Gdx.files.internal("next (1).png"));
}
@Override
public void show() {
}
public void handleInput(float dt){
if(Gdx.input.isKeyPressed(Input.Keys.R)){
game.setPlayScreen();
}
for (int i=0; i<5; i++){
if (Gdx.input.isTouched(i)) continue;
guicam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
if (wleftBounds.contains(touchPoint.x, touchPoint.y)){
player.b2body.applyLinearImpulse(new Vector2(-0.02f, 0), player.b2body.getWorldCenter(),true);
}else if (wrightBounds.contains(touchPoint.x, touchPoint.y)){
player.b2body.applyLinearImpulse(new Vector2(0.02f, 0), player.b2body.getWorldCenter(),true);
}
}
}
public void update(float dt){
handleInput(dt);
world.step(1/60f, 6, 2);
gamecam.position.x = player.b2body.getPosition().x;
gamecam.update();
guicam.update();
renderer.setView(gamecam);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
//render our Box2dDebugLines
b2dr.render(world, gamecam.combined);
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
//draw touch buttons
game.batch.begin();
game.batch.draw(leftRegion, wleftBounds.x, wleftBounds.y, wleftBounds.width, wleftBounds.height);
game.batch.draw(rightRegion, wrightBounds.x, wrightBounds.y, wrightBounds.width, wrightBounds.height);
game.batch.end();
}
@Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}
So, the player moves with 0.2f and -0.2f and if i change it to 0.1f it won't move at all..and the movement feels like it's too fast, you touch it once and the character keeps moving :( are there any other ways to do this?or what's wrong with this code? should i change the way that the force it's applied to the character?