“添加”两个角度

发布于 2024-10-05 12:22:50 字数 634 浏览 2 评论 0原文

好吧,所以我有两个角度。一是操纵杆的角度,二是摄像机与玩家的角度。相机的角度。现在我想要它,所以当我按下操纵杆时,它会将玩家移离摄像机。我该怎么做?在 Java 或 Ardor3d 中是否有一种简单的方法可以做到这一点?

编辑:这是我如何获取角度的代码。

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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-10-12 12:22:51

我不得不说,我不太明白你的问题,但它似乎是关于如何使用操纵杆实现与相机相关的控制。

我能给你的最重要的建议是,最好不要计算角度,而是直接使用向量。

假设相机朝 v 方向看(在某些类型的游戏中,该向量将直接指向玩家,但并非所有类型的游戏,且并非总是如此):

从摄像机到玩家的外观向量可以解析为垂直和水平分量

通常您不关心该向量的垂直分量,因此,删除它以获得水平分量,我将其称为 y ,原因稍后会变得显而易见:

y = v − (v · 向上) 向上

其中向上 是垂直向上的单位向量。

我们可以使用叉积找到垂直于 y 的水平向量(并记住右手定则):

x = v × 向上

向量x 和 y 形成水平坐标基础

现在您可以看到 y 是平面中指向前方(远离相机)的向量,而 x 是指向右侧的平面(相对于相机的侧面)。如果对这些向量进行归一化:

= x / |x|

ŷ = y / |y|

那么您可以使用ŷ作为玩家相对于相机运动的坐标基础。如果您的操纵杆读数为 JxJy,则将玩家移动

s (Jx + Jy ŷ)

其中 s 是与玩家速度成正比的适当标量值。

(请注意,此答案中的任何一点都没有计算角度!)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文