如何正确调整玩家精灵速度? (基本上是一道数学题?)

发布于 2024-09-18 00:28:57 字数 547 浏览 7 评论 0原文

背景:我有一个鸟瞰的JavaScript游戏,玩家通过触摸一个圆圈来控制一艘太空飞船——例如触摸圆圈中心的左侧,飞船将向左移动,触摸右上角,它将移动到顶部对等等……距离伪摇杆圆心越远,该方向的速度越快。但是,我不是直接调整船的速度,而是设置 targetSpeed.x 和 targetSpeed.y 值,然后船将使用以下内容调整其速度:

if (this.speed.x < this.targetSpeed.x) {
    this.speed.x += this.speedStep;
}
else if (this.speed.x > this.targetSpeed.x) {
    this.speed.x -= this.speedStep;
}

...对于 y 速度和 speedStep 也是如此是一个较小的值,使其更平滑并且不会太突然(船不应该从快速向左方向转到立即快速向右方向)。

我的问题:使用上面的代码,但是我相信速度在对角线方向上会调整得更快,而沿水平/垂直线会调整得更慢。我如何纠正这个问题以获得相同的目标速度?

非常感谢您的帮助!

Background: I have a bird view's JavaScript game where the player controls a space ship by touching a circle -- e.g. touch to the left of the circle center, and the ship will move left, touch the top right and it will move to the top right and so on... the further away from the circle center of pseudo joystick, the more speed in that direction. However, I'm not directly adjusting the ship's speed, but rather set a targetSpeed.x and targetSpeed.y value, and the ship will then adjust its speed using something like:

if (this.speed.x < this.targetSpeed.x) {
    this.speed.x += this.speedStep;
}
else if (this.speed.x > this.targetSpeed.x) {
    this.speed.x -= this.speedStep;
}

... and the same for the y speed, and speedStep is a small value to make it smoother and not too abrupt (a ship shouldn't go from a fast leftwards direction to an immediate fast rightwards direction).

My question: Using above code, I believe however that the speed will be adjusted quicker in diagonal directions, and slower along the horizontal/ vertical lines. How do I correct this to have an equal target speed following?

Thanks so much for any help!

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

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

发布评论

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

评论(2

有木有妳兜一样 2024-09-25 00:28:57
var xdiff = targetSpeed.x - speed.x;
var ydiff = targetSpeed.y - speed.y;
var angle = Math.atan2(ydiff, xdiff);
speed.x += speedStep * Math.cos(angle);
speed.y += speedStep * Math.sin(angle);
var xdiff = targetSpeed.x - speed.x;
var ydiff = targetSpeed.y - speed.y;
var angle = Math.atan2(ydiff, xdiff);
speed.x += speedStep * Math.cos(angle);
speed.y += speedStep * Math.sin(angle);
剩余の解释 2024-09-25 00:28:57

假设您已经检查过触摸位于圆圈内部,并且圆圈的边缘代表最大速度,并且圆圈的中心是 circleTouch == [0, 0]

在某些 C++ 中 -就像伪代码:

Scalar circleRadius = ...;
Scalar maxSpeed = ...;
Scalar acceleration = ...;

Vector calculateTargetSpeed( Vector circleTouch ) {
    Vector targetSpeed = maxSpeed * circleTouch / circleRadius;

    return targetSpeed;
}

Vector calculateNewSpeed( Vector currentSpeed, Vector targetSpeed ) {
    Vector speedDiff = targetSpeed - currentSpeed;

    Vector newSpeed = currentSpeed + acceleration * normalized(speedDiff);

    return newSpeed;
}

// Divide v by its length to get normalized vector (length 1) with same x/y ratio
Vector normalized( Vector v ) {
    return v / length(v);
}

// Pythagoras for the length of v
Scalar length( Vector v ) {
    Scalar length = sqrt(v.x * v.x + v.y * v.y); // or preferably hypot(v.x, v.y)

    return length;
}

这只是我的想法,我还没有测试过。另一个答案很好,我只是想给出一个没有三角函数的答案。 :)

Assuming you already checked that the touch is inside the circle, and that the edge of the circle represents max speed, and that the center of the circle is circleTouch == [0, 0]

In some C++-like pseudo code:

Scalar circleRadius = ...;
Scalar maxSpeed = ...;
Scalar acceleration = ...;

Vector calculateTargetSpeed( Vector circleTouch ) {
    Vector targetSpeed = maxSpeed * circleTouch / circleRadius;

    return targetSpeed;
}

Vector calculateNewSpeed( Vector currentSpeed, Vector targetSpeed ) {
    Vector speedDiff = targetSpeed - currentSpeed;

    Vector newSpeed = currentSpeed + acceleration * normalized(speedDiff);

    return newSpeed;
}

// Divide v by its length to get normalized vector (length 1) with same x/y ratio
Vector normalized( Vector v ) {
    return v / length(v);
}

// Pythagoras for the length of v
Scalar length( Vector v ) {
    Scalar length = sqrt(v.x * v.x + v.y * v.y); // or preferably hypot(v.x, v.y)

    return length;
}

This is just off the top of my head, and i haven't tested it. The other answer is fine, i just wanted to give an answer without trigonometry functions. :)

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