I try to make a simple Race-Game. For that I got a car with a vector for the position and one for the direction it is facing.
I have an update and an input method:
public void update(double delta) {
float rotation = 0;
if(movement.normalized().getY() < 0) {
rotation = (float) (2*Math.PI - Math.acos(movement.normalized().getX()));
} else {
rotation = (float) Math.acos(movement.normalized().getX());
}
pos = pos.add(new Vector3f((float) (Math.cos(rotation) * (movement.length() - 2) * delta), (float) (Math.sin(rotation) * (movement.length() - 2) * delta), 0f));
v1 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos((rotation + basicAngel)) * radius), (float) (pos.getY() + (Math.sin((rotation + basicAngel)) * radius)), 0), new Vector2f(0, 0));
v2 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos(((2*Math.PI - basicAngel) + rotation)) * radius), (float) (pos.getY() + Math.sin(((2*Math.PI - basicAngel) + rotation)) * radius), 0), new Vector2f(0, 0));
v3 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos((Math.PI + rotation + basicAngel)) * radius), (float)(pos.getY() + Math.sin((Math.PI + rotation + basicAngel)) * radius), 0), new Vector2f(0, 0));
v4 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos(((Math.PI - basicAngel) + rotation)) * radius), (float)(pos.getY() + Math.sin(((Math.PI - basicAngel) + rotation)) * radius), 0), new Vector2f(0, 0));
v5 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos((windowAngel + rotation)) * windowRadius), (float)(pos.getY() + Math.sin((windowAngel + rotation)) * windowRadius), 0), new Vector2f(0, 0));
v6 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos(((2*Math.PI - windowAngel) + rotation)) * windowRadius), (float)(pos.getY() + Math.sin(((2*Math.PI - windowAngel) + rotation)) * windowRadius), 0), new Vector2f(0, 0));
}
The first if-statement checks whether the y component of the normalized vector below 0 is.
That makes sure that the car rotates below the horizontal axis.
The next line applies the rotation and speed to the position.
The block with v1...v4 calculates the corners of the car to render it.
v5 and v6 are the corners of the cars window.
public void input() {
float tempX = movement.normalized().getX();
float tempY = movement.normalized().getY();
float tempLength = movement.length();
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
movement = movement.rotate(3);
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
movement = movement.rotate(-3);
}
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
if (tempLength <= 7) {
movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength + 0.2)));
movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength + 0.2)));
}
} else {
if(tempLength > 2) {
movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength - 0.05)));
movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength - 0.05)));
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
if (tempLength >= 0) {
movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength - 0.2)));
movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength - 0.2)));
}
} else {
if (tempLength < 2) {
movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength + 0.05)));
movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength + 0.05)));
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
reset();
}
}
The first two if-statements are checking whether the left or right key were pressed. If that happened the movement vetor gets rotated. The next two statements are checking the up and down key and appliing the speed to the movement vector by using this formula:
x = cos a * (l + speed)
y = sin a * (l + speed)
where a is the angle of the movement vector
and l is the length of the movement vector
Everything should work fine and it does, except that it miscalculates sometimes so that the car goes in a different direction as expected.
Can someone help me finding the mistake?