如何正确调整玩家精灵速度? (基本上是一道数学题?)
背景:我有一个鸟瞰的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您已经检查过触摸位于圆圈内部,并且圆圈的边缘代表最大速度,并且圆圈的中心是
circleTouch == [0, 0]
在某些 C++ 中 -就像伪代码:
这只是我的想法,我还没有测试过。另一个答案很好,我只是想给出一个没有三角函数的答案。 :)
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:
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. :)