如何使物体沿圆弧路径移动? [英] How to make an object move in the path of an arc?
问题描述
我正在制作一个游戏,其中应该有一个机器人向另一个机器人扔球状物体.
I'm making a game where there should be a robot throwing ball-shaped objects at another robot.
投出的球应呈对称弧形飞行.很确定这个数学词是抛物线.
The balls thrown should fly in the shape of a symmetrical arc. Pretty sure the math-word for this is a parabola.
两个机器人都在 x 轴上.
Both robots are on the x axis.
如何在我的游戏中实现这样的功能?我尝试了不同的方法,都没有奏效.
How can I implement such a thing in my game? I tried different approaches, none worked.
我的游戏中当前移动物体的系统是这样的:每个物体都有 x
和 y
坐标(变量),以及 dx
和 dy
变量.
The current system of moving things in my game, is like so: Every object has x
and y
co-ordinates (variables), and dx
and dy
variables.
每个对象都有一个 move()
方法,该方法在游戏循环的每个循环中都会被调用.它只是将 dx
添加到 x
并将 dy
添加到 y
.
Every object has a move()
method, that get's called every cycle of the game-loop. It simply adds dx
to x
and dy
to y
.
我如何将我描述的内容应用到这个系统中?
How can I implement what I described, into this system?
如果涉及到很多数学,请尽量用简单的方式解释,因为我不太擅长数学.
If there is a lot of math involved, please try to explain in a simply way, because I'm not great with math.
我的情况:
非常感谢
推荐答案
你应该为你的导弹添加速度.
You should add velocity to your missiles.
Velocity 是一个向量,这意味着它表示导弹在 x 轴上移动的速度和在 y 轴上移动的速度.现在,不要使用 Move()
,而是使用类似 Update()
的东西.像这样:
Velocity is a vector, which means it says how fast the missile moves in x-axis and how fast in y-axis. Now, instead of using Move()
use something like Update()
. Something like this:
void Update()
{
position.X += velocity.X;
position.Y += velocity.Y;
}
现在让我们想想,导弹一旦被击中会发生什么:
Now let's think, what happens to the missile, once it is shot:
一开始它有一些启动速度.例如,有人在 x 方向以 1 m/s 的速度发射导弹,在 y 方向以 -0.5 m/s 的速度发射导弹.然后当它发射时,导弹将被拉到地面 - 它的 Y 速度将向地面增加.
In the beginning it has some start velocity. For example somebody shot the missile with speed of 1 m/s in x, and -0.5 m/s in y. Then as it files, the missile will be pulled to the ground - it's Y velocity will be growing towards ground.
void Update()
{
velocity.Y += gravity;
position.X += velocity.X;
position.Y += velocity.Y;
}
这将使您的导弹根据物理原理(不包括空气阻力)移动,并生成漂亮的抛物线.
This will make your missile move accordingly to physics (excluding air resistance) and will generate a nice-looking parabola.
您可能会问如何计算初始速度.假设我们有给定的射击角度(在射击线和地面之间)和初始速度(我们可能知道射击后的导弹有多快,只是不知道 X和 Y 值).然后:
You might ask how to calculate the initial velocity. Let's assume we have a given angle of shot (between line of shot and the ground), and the initial speed (we may know how fast the missiles after the shot are, just don't know the X and Y values). Then:
velocity.X = cos(angle) * speed;
velocity.Y = sin(angle) * speed;
这篇关于如何使物体沿圆弧路径移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!