'adding' two angles

2019-07-04 00:15发布

Alright, so I got two angles. One is the joystick's angle, and the other is the camera to player angle. The camera's angle. Now I want it so when I press up on the joystick it moves the player away from the camera. How would I do this? And is there a easy way to do it in Java or Ardor3d?

edit: Here is the code of how I get my angles.

float camDegree = (float) Math.toDegrees(Math.atan2(
                     _canvas.getCanvasRenderer().getCamera().getLocation().getXf() - colladaNode.getTranslation().getXf(),
                     _canvas.getCanvasRenderer().getCamera().getLocation().getYf()) - colladaNode.getTranslation().getYf());

            player.angle = (float) Math.toDegrees(Math.atan2(padX, padY));
            Quaternion camQ = new Quaternion().fromAngleAxis(camDegree, Vector3.UNIT_Y);

1条回答
The star\"
2楼-- · 2019-07-04 00:51

I have to say that I don't really understand your question, but it seems to be about how to implement camera-relative control using a joystick.

The most important piece of advice I can give you is that it's better not to compute angles, but to work directly with vectors.

Suppose that the camera is looking in the direction v (in some types of game this vector will be pointing directly at the player, but not all types of game, and not always):

the look vector from a camera to a player can be resolved into vertical and horizontal components

Typically you don't care about the vertical component of this vector, so remove it to get the horizontal component, which I'll call y for reasons that will become apparent later:

y = v − (v · up) up

where up is a unit vector pointing vertically upwards.

We can find the horizontal vector that's perpendicular to y using the cross product (and remembering the right hand rule):

x = v × up

the vectors x and y form a horizontal coordinate basis

Now you can see that y is a vector in the plane pointing forwards (away from the camera), and x a vector in the plane pointing right (sideway with respect to the camera). If you normalise these vectors:

= x / |x|

ŷ = y / |y|

then you can use and ŷ as the coordinate basis for camera-relative motion of the player. If your joystick readings are Jx and Jy, then move the player by

s (Jx + Jy ŷ)

where s is an appropriate scalar value proportional to the player's speed.

(Notice that no angles were computed at any point in this answer!)

查看更多
登录 后发表回答