问题描述
我正在做一个游戏,其中应该有一个机器人向另一个机器人投掷球形物体.
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.
我的情况:
非常感谢
推荐答案
您应该在导弹上添加速度.
速度是一个向量,这意味着它表示导弹在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;
这篇关于如何使物体在圆弧的路径上移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!