How to make an object move in the path of an arc?

こ雲淡風輕ζ 提交于 2021-02-04 14:12:33

问题


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.

Both robots are on the x axis.

How can I implement such a thing in my game? I tried different approaches, none worked.

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.

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.

My situation:

enter image description here

Thanks a lot


回答1:


You should add velocity to your missiles.

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:

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.

Edit: 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;



回答2:


Adding to Michal's answer, to make sure the missile hits the robot (if you want it to track the robot), you need to adjust its x velocity.

void Update()
{
    ball.dy += gravity;    // gravity = -9.8 or whatever makes sense in your game
    ball.dx = (target.x - ball.x); // this needs to be normalized.

    double ballNorm = sqrt(ball.dx^2 + ball.dy^2);
    ball.dx /= ballNorm;


    ball.x += ball.dx;
    ball.y += ball.dy
}

This will cause the missile to track your target. Normalizing the x component of your vector ensures that it will never go above a velocity of one. It's nor fully "normalizing" the vector because normally you would have to do this to the y component too. If we didn't normalize here, we would end up with a ball that jumps all the way to your target on the first update. If you want to make your missile travel faster, just multiply ballNorm by some amount.




回答3:


you can get every thing you need from these few equations for the max height to time to distance.

g = gravity
v = start vorticity M/S
a = start angle deg

g = KG of object * 9.81
time = v*2 * sin(a) / g
range = v^2 * sin(a * 2) / g
height = v^2 * sin(a)^2 / 2*g


来源:https://stackoverflow.com/questions/21368210/how-to-make-an-object-move-in-the-path-of-an-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!