[SOLVED] [libGdx] what's wrong with my lookAt and move forward code ?

Hello everyone,

so am still in the process of getting familiar with libGdx and one of the fun things i love to do is to make basics method for reusability on future projects, and for now am stacked on getting a Sprite rotate toward target (vector2) and then move forward based on that rotation
the code am using is this :

// set angle
	public void lookAt(Vector2 target) {

		float angle = (float) Math.atan2(target.y - this.position.y, target.x
				- this.position.x);
		angle = (float) (angle * (180 / Math.PI));
		
		setAngle(angle);
		
	}

	// move forward
	public void moveForward() {
		this.position.x += Math.cos(getAngle())*this.speed;
		this.position.y += Math.sin(getAngle())*this.speed;
	}

and this is my render method :

@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		Gdx.gl.glClearColor(0, 0, 0.0f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		// groupUpdate();

		Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
		camera.unproject(mousePos);
		ball.lookAt(new Vector2(mousePos.x, mousePos.y));
		//

		if (Gdx.input.isTouched()) {
			ball.moveForward();
		}

		batch.begin();
		batch.draw(ball.getSprite(), ball.getPos().x, ball.getPos().y, ball
				.getSprite().getOriginX(), ball.getSprite().getOriginY(), ball
				.getSprite().getWidth(), ball.getSprite().getHeight(), .5f,
				.5f, ball.getAngle());
		batch.end();
	}

the goal is to make the ball always look at the mouse cursor, and then move forward when i click, am also using this camera :

// create the camera and the SpriteBatch
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 800, 480);

aaaand the result was so creepy lol

http://s28.postimg.org/65e134vf1/rot_Bug.gif

just to give you everything i have, here is the full Ball class (please tell me if you found any “mistake” or bad habit)

public class Ball {

	public String type;
	public float speed, angle;
	public Vector2 position;
	public Sprite sprite;

	public Ball(Sprite sprite, String type, Vector2 position, float speed,
			float angle) {
		this.sprite = sprite;
		this.type = type;
		this.position = position;
		this.speed = speed;
		this.angle = angle;
		
		
	}

	// get set sprite
	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
	}

	public Sprite getSprite() {
		return this.sprite;
	}

	// get set type
	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return this.type;
	}

	// get set position
	public void setPos(Vector2 pos) {
		this.position = pos;
	}

	public Vector2 getPos() {
		return this.position;
	}

	// get set speed ;
	public void setSpeed(float speed) {
		this.speed = speed;
	}

	public float getSpeed() {
		return this.speed;
	}

	// get set angle
	public void setAngle(float angle) {
		this.angle = angle;
	}

	public float getAngle() {
		return this.angle;
	}

	// set angle
	public void lookAt(Vector2 target) {

		float angle = (float) Math.atan2(target.y - this.position.y, target.x
				- this.position.x);
		angle = (float) (angle * (180 / Math.PI));
		
		setAngle(angle);
		
	}

	// move forward
	public void moveForward() {
		this.position.x += Math.cos(getAngle())*this.speed;
		this.position.y += Math.sin(getAngle())*this.speed;
	}
	
	
}

thank you ;D !!

Try using atan instead of atan2, also that will return degrees so you won’t need the line of code after it that changes radians to degrees

-edit

that line should actually turn degrees into radians therefore it should be

angle * Math.PI / 180

what :stuck_out_tongue: ?
why ? and how ?

Why not this?


public void lookAt(Vector2 target) {
      setAngle(target.sub(position).angle());
}

One liner.

sorry, brain is fried, you’re correct using atan2

Awesome :smiley: i didn’t know about that !!
now how about the “moving forward” code ?
Reminder:
i don’t want the object to move toward another object, i want it to move forward based on it’s direction

thanx

Well your code is correct except that Math.sin/cos expect radians.


public void moveForward() {
      position.add(new Vector2(speed, 0).rotate(getAngle()));

      // alternatively your code + toRadians fix:
      //this.position.x += Math.cos(Math.toRadians(getAngle()))*this.speed;
      //this.position.y += Math.sin(Math.toRadians(getAngle()))*this.speed;
}

EDIT: I would just keep a [icode]velocity[/icode] Vector2 field around instead of messing around with separate speed and angle fields.

PERFECT ;D !!
i am really impressed by the build-in method of libGdx, thnx a lot man