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 !!