将四元数完全旋转为向量

发布于 2024-11-25 15:23:03 字数 1380 浏览 0 评论 0原文

我有两个对象(目标和玩家),都有位置(Vector3)和旋转(四元数)。我希望目标旋转并面向玩家。目标在射击物体时应该直接射击玩家。

我见过很多对玩家进行 slerp 的例子,但我不想要增量轮换,好吧,我想只要我能让 slerp 达到 100%,并且只要它确实有效,就可以了。

仅供参考 - 我能够利用位置和旋转来做很多其他事情,除了我无法弄清楚的最后一块之外,一切都很好。

编辑

代码示例在目标类中运行,位置 = 目标位置,阿凡达 = 玩家。

对 Slerp 使用值 1 是行不通的。下面的代码会进行一些旋转,但我认为有些事情还很遥远,因为当绘制它时,目标会随着玩家靠近而放大然后缩小。

var A = new Vector3(Position.X, Position.Y, Position.Z);
var B = new Vector3(GameState.Avatar.Position.X, GameState.Avatar.Position.Y, GameState.Avatar.Position.Z);
A.Normalize();
B.Normalize();
var angle = Math.Acos(Vector3.Dot(A, B));
var axis = Vector3.Normalize(Vector3.Cross(A, B));
var rotOnAngle = new Quaternion(axis, (float)angle);
var newRot = Quaternion.Slerp(Quaternion.Identity, rotOnAngle, 1f);
Rotation = newRot;
Cannon.Shoot(Position, Rotation, this);

我尝试使用这个,但它也不太起作用......目标确实旋转,但不面对玩家。但至少缩放问题消失了。

Quaternion q = new Quaternion();
var pos = Vector3.Normalize(Position);
var pos2 = Vector3.Normalize(GameState.Avatar.Position);%
var a = Vector3.Cross(Position, GameState.Avatar.Position);
q.X = a.X; q.Y = a.Y; q.Z = a.Z;
q.W = (float)Math.Sqrt(((int)Position.Length() ^ 2) * ((int)GameState.Avatar.Position.Length() ^ 2)) + Vector3.Dot(Position, GameState.Avatar.Position);
q.Normalize();
Rotation = q;
Cannon.Shoot(Position, Rotation, this);

I have two objects (target and player), both have Position (Vector3) and Rotation (Quaternion). I want the target to rotate and be facing right at the player. The target, when it shoots something should shoot right at the player.

I've seen plenty of examples of slerping to the player, but I don't want incremental rotation, well, I suppose that would be ok as long as I can make the slerp be 100%, and as long as it actually worked.

FYI - Im able to use the position and rotation to do plenty of other things and it all works great except this last piece I cant figure out.

EDIT

Code samples run in the Target's class, Position = the targets position, Avatar = the player.

Using the value of 1 for the Slerp isn't work. This code below rotates some, but I think something is way off becuase when it's drawn the target scales up and then down as the player gets closer.

var A = new Vector3(Position.X, Position.Y, Position.Z);
var B = new Vector3(GameState.Avatar.Position.X, GameState.Avatar.Position.Y, GameState.Avatar.Position.Z);
A.Normalize();
B.Normalize();
var angle = Math.Acos(Vector3.Dot(A, B));
var axis = Vector3.Normalize(Vector3.Cross(A, B));
var rotOnAngle = new Quaternion(axis, (float)angle);
var newRot = Quaternion.Slerp(Quaternion.Identity, rotOnAngle, 1f);
Rotation = newRot;
Cannon.Shoot(Position, Rotation, this);

I tried using this and it doesn't quite work either...the target does rotate, but not to face the player. But at least the scaling problem goes away.

Quaternion q = new Quaternion();
var pos = Vector3.Normalize(Position);
var pos2 = Vector3.Normalize(GameState.Avatar.Position);%
var a = Vector3.Cross(Position, GameState.Avatar.Position);
q.X = a.X; q.Y = a.Y; q.Z = a.Z;
q.W = (float)Math.Sqrt(((int)Position.Length() ^ 2) * ((int)GameState.Avatar.Position.Length() ^ 2)) + Vector3.Dot(Position, GameState.Avatar.Position);
q.Normalize();
Rotation = q;
Cannon.Shoot(Position, Rotation, this);

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

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

发布评论

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

评论(2

忘东忘西忘不掉你 2024-12-02 15:23:03

我已经有一段时间没有进行此类数学运算了,但我猜测第三个参数只是 1。

编辑:为了限定这一点,我上次这样做时,它被称为 Managed directX,而不是 XNA!

It's been a while since I did this sort of math, but I would have guessed that the 3rd parameter there would simply be 1.

Edit: To qualify that, the last time I did this, it was called Managed directX, not XNA!

反话 2024-12-02 15:23:03

我碰巧在游戏开发堆栈交换中问了同样的问题,那里有人回答了。请务必阅读答案中的评论,无论如何,答案/解决方案效果很好!谢谢,也很抱歉在这里提问。

https://gamedev.stackexchange.com/questions/15070/orienting-模型面对目标

I happen to ask the same question over in Game Dev stack exchange and someone answered over there. Make sure to read the comments in the answer, regardless, the answer/solution works great! Thanks, and sorry about asking here as well.

https://gamedev.stackexchange.com/questions/15070/orienting-a-model-to-face-a-target

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