Ronny Ronny - 3 months ago 6
Java Question

what reason is there for non-smooth 2d sprite movement in this example

Just in process of making a space invaders style game for android.
I want the player to be able to touch the screen anywhere to right or left of character (at bottom of screen) to move him that direction.
The code compiles without error and the playe does move, but
A) he's moving much slower than I expected
B) The movement is 'jittery' even though I have multiplied the movement speed by deltatime in a few different ways.

Please could someone be kind enough to take a look at my code to say where I have gone wrong? :-

package com.moneylife.stashinvaders;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class StashInvaders extends ApplicationAdapter {
GameManager gameManager;
@Override
public void create () {
gameManager = new GameManager();

}

@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameManager.update();
gameManager.draw();

}

@Override
public void dispose () {
gameManager.spriteBatch.dispose();

}
}


GameManager class:

package com.moneylife.stashinvaders;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
* Created by Dave on 12/08/2016.
*/
public class GameManager {
SpriteBatch spriteBatch;
Player player1;

public GameManager(){
spriteBatch = new SpriteBatch();
player1 = new Player();
}

public void update(){
player1.update();
}

public void draw(){
spriteBatch.begin();

player1.draw(spriteBatch);

spriteBatch.end();
}
}


Player class:

package com.moneylife.stashinvaders;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;

/**
* Created by Dave on 12/08/2016.
*/
public class Player {
Vector2 position;
Texture texture;
int speed = 50;
float deltaTime;

public Player(){
Gdx.input.setInputProcessor(new GestureDetector(new MyGestureDetector()));
texture = new Texture("bazookaman.png");
position = new Vector2(Gdx.graphics.getBackBufferWidth() / 2 - texture.getWidth() / 2, 0);
}

public void update(){
deltaTime = Gdx.graphics.getDeltaTime();
}

public void draw(SpriteBatch spriteBatch){
spriteBatch.draw(texture, position.x, position.y);
}


public class MyGestureDetector implements GestureDetector.GestureListener {
@Override
public boolean touchDown(float x, float y, int pointer, int button) {
return false;
}

@Override
public boolean tap(float x, float y, int count, int button) {
return false;
}

@Override
public boolean longPress(float x, float y) {
return false;
}

@Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
if (x > position.x){
position.x += speed * deltaTime;
}
if (x < position.x){
position.x -= speed * deltaTime;
}
return false;
}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
return false;
}

@Override
public boolean zoom(float initialDistance, float distance) {
return false;
}

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
return false;
}

@Override
public void pinchStop() {

}
}
}

Answer

Look closely at your logic.

if (x > position.x){
    position.x += speed * deltaTime; //if close to touch point, position.x is now bigger than x
}
if (x < position.x){ //if we just moved right past the touch point, undo it
    position.x -= speed * deltaTime;
}

Furthermore, this pan method will only be called on frames where the finger position was polled and was found to have moved. The finger position is not polled 60 times per second like your game is probably running, and movement will not always have occurred.

Instead, you should use the panning to modify a target position for your character. In the update() method you can always be moving towards that target position with some speed. It's up to you whether you should be using x or deltaX in the pan method to change your target X. Different types of gameplay.

Comments