0

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?

4

1 回答 1

1

您不能使用 acos 来获取角度。例如 Math.cos(Math.PI*3/4) 和 Math.cos(Math.PI*5/4) 都给你 -0.7071。查看余弦曲线以了解原因。要获得向量的角度,请使用 Math.atan2(y,x)。另外,为什么要将向量转换为角度然后再转换回向量。那是没有必要的。为什么不使用归一化向量作为方向,使用浮点/双精度表示速度,并在每次更新时将其相乘?

或者你可以做 move = motion.add(movement.normalized().mul(0.2));

于 2013-10-03T03:02:39.360 回答