I'm having an issue with my movement in my 'game' prototype. It's basically not working and admittedly my knowledge with vectors is lacking.
The issue I'm running into is that the ship won't move in the direction of my mouse. (I'm using my mouse as rotation)
Mouse look example video I took yesterday
I would love it if someone could look at my code/Vector math and tell me if they can notice anything I'm doing terribly wrong, I would greatly appreciate it.
private Vector2f acceleration;
private Vector2f force;
private Vector2f velocity;
private Vector2f position;
private Image image = null;
private float mass;
private float angle;
//Constructors
public Ship(float xin, float yin) {
this.mass = 5000; // temp value of mass
this.angle = 0;
// Image of the ship
try {
this.image = new Image("res/ship.png");
} catch (SlickException e) {
e.printStackTrace();
}
// Vectors
position = new Vector2f(xin, yin);
velocity = new Vector2f(0, 0);
acceleration = new Vector2f(0, 0);
force = new Vector2f(0, 0);
}
public void update(Input input, float delta) {
float mouseX = input.getMouseX();
float mouseY = input.getMouseY();
float xDistance = mouseX - (position.x + image.getWidth()/2);
float yDistance = mouseY - (position.y + image.getHeight()/2);
angle = (float) (Math.toDegrees(Math.atan2(yDistance, xDistance)) + 90);
image.setRotation((float)angle);
if (input.isKeyDown(Input.KEY_W)) {
this.move(delta);
} else if (input.isKeyDown(Input.KEY_S)) {
//this.move(delta);
} else {
acceleration = new Vector2f(0, 0);
}
}
public void move(float delta) {
/*
* Acceleration = Force / Mass
* Velocity += Acceleration * ElapsedTime (delta)
* Position += Velocity * ElapsedTime (delta)
*/
System.out.println(delta); //debugging
//delta = delta/10; //debugging
//System.out.println(delta); //debugging
force = angleToVector(angle); // Direction
force = force.normalise();
force = force.scale(2);
acceleration = force.scale(1/mass);
velocity = velocity.add(acceleration.scale(delta));
position = position.add(velocity.scale(delta));
}
public Vector2f angleToVector(float angle) {
return new Vector2f((float)Math.cos(angle), (float)Math.sin(angle));
}
If you want to see the full .java for my ship class here's a link.