如何围绕另一个向量2旋转一个向量2?

发布于 2024-12-10 23:53:34 字数 530 浏览 2 评论 0原文

我目前正在制作一款游戏。该游戏由玩家从自上而下的角度操作机器人组成。移动、碰撞和射击全部实现。

然而,机器人有 2 个“手臂”,可以从这些手臂发射武器,为了从这些手臂射击,我基本上设置了 2 个临时位置供机器人发射,每个手臂一个,包括 X 方向的偏移+/- 15. 当玩家向下看时,手臂射击得很好,子弹似乎是从机器人手臂中射出的,但是,当我转动玩家在 Y 轴上向左或向右看时,它显示为虽然机器人正在发射子弹它的体内。我有一张图片可以帮助说明问题

http://imageshack.us/photo /my-images/694/problemrs.png/

正如您所看到的,在机器人身体旋转之前,左右射弹生成点都处于正确的位置。我想要做的是沿着机器人身体旋转这 2 个 Vector2 位置。红色块显示射弹的产卵位置,白色块代表我希望的产卵位置。我尝试了一些旋转方法,但没有一个在之后产生效果。

有什么线索吗?

I'm currently in the process of making a game. he game consists of the player operating a robot from a top down perspective. The movement, collisions and firing is all implemented.

However, the robot has 2 'arms' from which it fires its weapons, to fire from these arms i have basically set up 2 makeshift locations for the robot to fire from, one for each arm, consisting of an offset in the X direction of +/- 15. When the player is looking down the arms shoot fine and the bullets appear to be coming out from the robots arms, however, when i turn the player to look to the left or right on the Y axis, it appears as though the robot is shooting the bullets from within its body. I have an image to help illustrate the issue

http://imageshack.us/photo/my-images/694/problemrs.png/

As you can see, the left and right projectile spawn points are in the correct position until the robots body is rotated. What i want to be able to do, is rotate those 2 Vector2 positions around alongside the robots body. The red blocks show where the projectiles are spawning from, and the white blocks represent where i would like the spawn position to be. I've tried a few rotation methods but none have had the effect im after.

Any clues?

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

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

发布评论

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

评论(3

迟月 2024-12-17 23:53:34

您可以使用仿射变换来做到这一点......但是,就您而言,使用基本三角函数可能更容易做到这一点。

XY为机器人的位置。

T为机器人的旋转角度。

DX为从机器人中心到“手臂”的距离。 (对另一只手臂使用 -DX)。

ArmX = (cos(T) * DX) + X

ArmY = ( sin(T) * DX) + Y

You could do it with an affine transform... but, in your case, it might be easier to do it with basic trig.

LET X, Y be the location of the robot.

LET T be the angle of rotation of the robot.

LET DX be the distance from the center of the robot to the "arm". (use -DX for the other arm).

ArmX = (cos(T) * DX) + X

ArmY = (sin(T) * DX) + Y

浪漫之都 2024-12-17 23:53:34

我想,你的机器人有一个前进方向和一个中心,然后

 F = forward vector
 c = center point
 d = distance from center to arms

 NF = normal to forward

 NF = new Vector2(F.Y, -F.X);

 NF.Normalize();

 LeftArmShootOrigin = c + NF * d;
 RightArmShootOrigin = c - NF * d;

I suppose, you have a forward direction and a center for your robot, then

 F = forward vector
 c = center point
 d = distance from center to arms

 NF = normal to forward

 NF = new Vector2(F.Y, -F.X);

 NF.Normalize();

 LeftArmShootOrigin = c + NF * d;
 RightArmShootOrigin = c - NF * d;
奶气 2024-12-17 23:53:34

看来您正确地旋转了向量本身。您缺少的部分是您需要将矢量的基部/尾部的位置(它开始的位置)编码为矢量并旋转它。

It looks like you're rotating the vectors themselves correctly. The part you're missing is that you need to encode the position of the base/tail of your vector (the place it starts from) as a vector and rotate that as well.

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