Question

I want to draw projectile curve before throwing object.

I have tried to create projectile motion from the following link

http://www.iforce2d.net/src/iforce2d_Trajectories.h

But it gives an example thrown object transferring to the target body.

I want the followings

  1. Draw projectile curve between two positions(1- fixed box2d object and 2 - touch position).

  2. If i changes the position of touch, the projectile curve will also moves to corresponding position.

Any idea to follow the logic?

Thanks in advance.

Was it helpful?

Solution

It is a little ambiguous if you are asking to draw the curve when you already know the launch velocity (angle and magnitude, or Vx(0),Vy(0) components). If you know the launch angle and velocity, you can use the following formulas:

Px(t) = Vx(0)*t Py(t) = Vy(0)*t + 0.5*a*t*t

Where Px(t), Py(t) are the position in the x,y plane respectively.

You can figure out the total flight time by calculating Xdelta (from you launch and target postion). Then

Xdelta = Tf*Vx(0) (Total Flight Time Tf), or Tf = Xdelta/Vx(0), which you can then iterate over in a loop and plot the calculated points.

NOTE: There will be a slight difference in the actual path from Box2d vs. the closed formula path because Box2D is approximating the integral. If your (fixed) timestep is on the order of 1.0/60.0, there should be very little difference. Your mileage may vary here.

Regardless, this will allow you to plot any point on the path.

HOWEVER, I SUSPECT YOUR QUESTION IS REALLY THIS:

How do I calculate the launch velocity to hit a specific target. This is not a trivial answer.

Starting with a launch angle and a launch velocity Vo and Theta, you can write the two equations for Xdelta, Ydelta (from the equations above) as:

Xdelta = Tf * Vo*cos(theta)

Ydelta = Tf * V0*sin(theta) + 0.5*a*Tf*Tf

If you solve the first for Tf, you get

Tf = Xdelta/(Vo*cos(theta))

Substitute into the second (you should check my algebra)

Ydelta = (xDelta*Vo*sin(theta))/(Vo*cos(theta)) + 0.5 * a * Xdelta^2/(Vo^2*cos(theta)^2))

Doing some rearranging (again, check algebra):

2*Vo^2*cos(theta)^2*Ydelta - Vo^2*Xdelta*sin(2*theta) - a*Xdelta^2 = 0

This is a nonlinear equation in Vo and theta.
If your angle is fixed, you can plug in theta, solve the quadratic for Vo, and should be able to get solutions out of it.

In that situation, I get (again, check algebra):

Vo = +/- sqrt((a * Xdelta^2)/(2*cos(theta)^2*Ydelta - sin(2*theta)*Xdelta)

If the value in the sqrt(.) is negative, you can't make the shot at all.

If your Vo is fixed, you may be able to find find a closed form solution for theta, or solve it numerically.

Once you know Vo and theta, there is still the question of "does a collision occur when this projectile is launched".

If you know the path it follows, you can test points along the path in a loop using an AABB in Box2D to see if there are any objects it might collide with. This could all be done in a single function which iterates from 0 to Tf, with dt = fixed step size for the simulation. Note that if your velocity is really high, you might "bullet" through something.

Was this helpful?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top