2

I'm looking for the answer for quite long time but I still haven't found solution.

I'm creating a simple game using Swift/SpriteKit and the rules are also simple. Player touches screen and ball goes to the touch location in a parabolic trajectory.

I want to achieve something like this http://www.onlinegames.com/basketball/ however I don't know how to calculate needed impulse.

The code is simple ball.physicsBody.applyImpulse(CGVectorMake(x, y));

All I have is mass of the ball and gravity.

How to find x,y? Please help.

Daniel
  • 338
  • 3
  • 13

1 Answers1

1

An impulse is the change in momentum m*v of the ball. Thus, given the ball's mass, impulse/mass = delta_v - the change in velocity.

Impulse as a Function of Time of Flight

The ball has to reach its target in both the x and y dimensions. We can write this as as a system of two equations, one per each dimension,

x' = x + v_x * t,
y' = y + v_y * t + 0.5 * g_y * t^2,

where t is the time it takes to reach the target, ' (prime) denotes the final position (at the target), x,y are components of initial position, v_dim denotes a component of the initial velocity, and g_y is the vertical component of the acceleration of gravity. g_y should be negative if your y points "up".

We also know that the initial velocity is v = v0 + impulse/mass, where v0 is the velocity the ball had prior to application of the impulse, and everything but mass is a vector.

Looking at both equations, the initial and target positions are known, as is the acceleration of gravity and the initial ball velocity. The only unknowns are impulse and time of flight.

The impulse is a function of the desired time of flight. So, first select some time of flight, and then obtain the impulse for it. So, let's solve for impulse:

x' = x + (v0_x + imp_x/mass) * t
(x' - x)/t = v0_x + imp_x/mass
mass * ((x' - x)/t - v0_x) = imp_x

y' = y + (v0_y + imp_y/mass) * t + 0.5 * g_y * t^2,
(y' - y)/t = v0_y + imp_y/mass + 0.5 * g_y * t
mass * ((y' - y)/t - v0_y - 0.5*g_y*t) = imp_y

The arbitrary, positive and nonzero value of t implies that we can "tweak" our approach according to some criteria. This would require additional equation(s) that express the constraint and produce the value of t.

Selecting a Time of Flight

We might wish that the magnitude (length) of the average ball velocity v_avg is fixed, such that the time of flight varies linearly with distance from the starting point to the target.

By definition, v_avg = (pos' - pos)/t, or, per component:

v_avg_x = (x' - x) / t
v_avg_y = (y' - y) / t

Since we wish to constrain the magnitude of the average velocity, or the scalar average velocity vsa without regard where exactly the vector average velocity points, we obtain one equation:

vsa^2 = v_avg_x^2 + v_avg_y^2
vsa^2 = (x' - x)^2/t^2 + (y' - y)^2/t^2
t^2 = [(x' - x)^2 + (y' - y)^2] / vsa^2
t = sqrt[(x' - x)^2 + (y' - y)^2] / vsa

Recap

You can choose some average scalar velocity vsa, say 1m/s, apply it to the above to obtain t, then calculate the imp_x and imp_y to get the ball to its location in given time at given scalar average velocity.

Don't forget that your g_y is negative if positive y points gravitationally "up".

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313