Moving a 2D physics body on a parabolic path with initial impulse in unity

送分小仙女□ 提交于 2021-02-10 05:10:27

问题


What I have:

A projectile in Unity 5 2D, affected by gravity, that I want to move from point A to point B with initial impulse on a parabolic path

What I know:

  • The 2D coordinates of a random starting position (A)
  • The 2D coordinates of a random target position (B)
  • The time (X) I want the body to reach the target position after

What I want to know:

  • The initial impulse (as in unity applyforce with forcemode impulse) that I have to apply to the body onetime in order for it to reach the desired position B after X time.

Notes:

  • It is a 2D world using Unity 2D physics
  • The two positions A and B are totally random and may or may NOT be at equal heights (y coordinates)
  • I want to apply the impulse one time only when spawning the projectile
  • The projectile does NOT have any other forces affecting him other than GRAVITY (for the current problem lets assume that it always with magnitude 9.8 pointed down)

Here is a (bad) drawing of what I want :D

Thanks upfront guys!


回答1:


Let's solve a few simpler problems. Our goal is to find an initial velocity for the particle, instead of an "impulse", because we'll achieve better control this way.

First, consider how we would solve this without Unity physics. A particle's position as a function of time depends on its initial position, velocity and acceleration. The same equations apply regardless of how many dimensions you have. This equation is:

current_position = 1/2 * acceleration * time^2 + velocity * time + starting_position

Let current_position be our desired destination, time be the duration of travel you choose, and the starting_position to be the starting position of the particle.

In the x-dimension, there is no acceleration. Let X_0 be our starting X position, X_1 be our destination, and T be the duration of time we want to spend getting to that destination. We want to find v_x, or the velocity in the x-dimension. For ease of interpretation, I've marked all the constants in uppercase and the variable in lowercase.

  1. X_1 = v_x*T + X_0
  2. v_x = (X_1-X_0)/T

In the y-dimension, we have gravity of some kind. So let's incorporate that as our acceleration:

  1. Y_1 = 0.5*G*T*T + v_y*T + Y_0
  2. v_y = (Y_1 - Y_0 - 0.5*G*T*T)/T

Now that you have v_x and v_y, just set the velocity:

rigidbody2d.velocity = new Vector2(v_x, v_y);

Since different objects have different mass, you don't want to try to calculate an impulse. Just set the velocity to what you want in the frame when the object is spawned.

This runs contrary to advice you might find elsewhere on the Internet, but try it first before doing something more complicated.

Some important notes:

  • Physics processing in Unity isn't stable: you might get different results on different platforms, especially between different mobile devices. If there is performance pressure, there may be fewer physics evaluations and therefore incorrect results. This is generally not a problem with 2D physics.
  • You may experience an issue with the scale of gravity, depending on your version and what other settings you have.



回答2:


The formulas of position of a projectile influenced only by gravity are these:

x = x0 + v_x0*t
y = y0 + v_y0*t + 0.5*g*t^2

so, to know the starting velocity, with some basic calculations:

v_x0 = (x-x0)/t
v_y0 = (y-y0)/t + 0.5*g*t^2/t = (y-y0)/t - 4.9*t

Now, in order to add this startin velocity by using AddForce, we could have used ForceMode.VelocityChange, but this mode is only available for 3D rigidbodies, so we need to use the ForceMode.Impulse and multiplying the force by the mass of the object:

RigidBody2D rigid = GetComponent<RigidBody2D>();
rigid.AddForce(new Vector2 (v_x0, v_y0) * rigid.mass, ForceMode2D.Impulse);

and that's it.

Of course you can just set the velocity of the rigid body directly instead of using AddForce.



来源:https://stackoverflow.com/questions/42792320/moving-a-2d-physics-body-on-a-parabolic-path-with-initial-impulse-in-unity

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