射弹跟踪代码有困难

发布于 2024-09-01 09:16:14 字数 1843 浏览 7 评论 0原文

我在游戏中为射弹类编写了一些代码,使其能够跟踪目标(如果可以的话):

            if (_target != null && !_target.IsDead)
            {
                Vector2 currentDirectionVector = this.Body.LinearVelocity;
                currentDirectionVector.Normalize();
                float currentDirection = (float)Math.Atan2(currentDirectionVector.Y, currentDirectionVector.X);
                Vector2 targetDirectionVector = this._target.Position - this.Position;
                targetDirectionVector.Normalize();
                float targetDirection = (float)Math.Atan2(targetDirectionVector.Y, targetDirectionVector.X);
                float targetDirectionDelta = targetDirection - currentDirection;
                if (MathFunctions.IsInRange(targetDirectionDelta, -(Info.TrackingRate * deltaTime), Info.TrackingRate * deltaTime))
                {
                    Body.LinearVelocity = targetDirectionVector * Info.FiringVelocity;
                }
                else if (targetDirectionDelta > 0)
                {
                    float newDirection = currentDirection + Info.TrackingRate * deltaTime;
                    Body.LinearVelocity = new Vector2(
                        (float)Math.Cos(newDirection),
                        (float)Math.Sin(newDirection)) * Info.FiringVelocity;
                }
                else if (targetDirectionDelta < 0)
                {
                    float newDirection = currentDirection - Info.TrackingRate * deltaTime;
                    Body.LinearVelocity = new Vector2(
                        (float)Math.Cos(newDirection),
                        (float)Math.Sin(newDirection)) * Info.FiringVelocity;
                }
            }

这有时有效,但取决于与目标射弹的相对角度,而不是偏离目标。我被难住了;有人可以指出我的代码中的缺陷吗?

更新:思考并尝试一些东西让我得出这样的结论:当方向(以弧度为单位)低于 0 并且当前射弹角度高于 0 时,它与此有关。

I wrote some code for a projectile class in my game that makes it track targets if it can:

            if (_target != null && !_target.IsDead)
            {
                Vector2 currentDirectionVector = this.Body.LinearVelocity;
                currentDirectionVector.Normalize();
                float currentDirection = (float)Math.Atan2(currentDirectionVector.Y, currentDirectionVector.X);
                Vector2 targetDirectionVector = this._target.Position - this.Position;
                targetDirectionVector.Normalize();
                float targetDirection = (float)Math.Atan2(targetDirectionVector.Y, targetDirectionVector.X);
                float targetDirectionDelta = targetDirection - currentDirection;
                if (MathFunctions.IsInRange(targetDirectionDelta, -(Info.TrackingRate * deltaTime), Info.TrackingRate * deltaTime))
                {
                    Body.LinearVelocity = targetDirectionVector * Info.FiringVelocity;
                }
                else if (targetDirectionDelta > 0)
                {
                    float newDirection = currentDirection + Info.TrackingRate * deltaTime;
                    Body.LinearVelocity = new Vector2(
                        (float)Math.Cos(newDirection),
                        (float)Math.Sin(newDirection)) * Info.FiringVelocity;
                }
                else if (targetDirectionDelta < 0)
                {
                    float newDirection = currentDirection - Info.TrackingRate * deltaTime;
                    Body.LinearVelocity = new Vector2(
                        (float)Math.Cos(newDirection),
                        (float)Math.Sin(newDirection)) * Info.FiringVelocity;
                }
            }

This works sometimes, but depending on the relative angle to the target projectiles turn away from the target instead. I'm stumped; can someone point out the flaw in my code?

Update: thinking about it and trying stuff has led me to the conclusion that it has something to do with when the direction (being in radians) is below 0 and the current projectile angle is above 0.

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

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

发布评论

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

评论(1

迷乱花海 2024-09-08 09:16:15

变量targetDirectionDelta并不总是到目标的最短方向。如果targetDirectionDelta的绝对值大于PI弧度,就会出现弹丸正在偏离目标的情况。向另一个方向转弯的时间较短,也是预期的。

示例:

currentDirection = 2
targetDirection = -2

抛射体可以转动 -4 弧度(负方向),或 2*(PI-2) 弧度(约 2.2 弧度)(正方向)。

对于这种情况,您的代码始终计算较长的方向,但您期望射弹转向较短的方向:

targetDirectionDelta = targetDirection - currentDirection

The variable targetDirectionDelta is not always the shortest direction to the target. If the absolute value of targetDirectionDelta is greater than PI radians, it will appear the projectile is turning away from the target. Turning in the other direction is shorter and expected.

Example:

currentDirection = 2
targetDirection = -2

The projectile can turn -4 radians (in the negative direction), or 2*(PI-2) radians (about 2.2 radians) (in the positive direction).

For this case, your code always calculates the longer direction, but you are expecting the projectile to turn towards the shorter direction:

targetDirectionDelta = targetDirection - currentDirection

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