- Home /
Aim preview for projectile trajectory
I need to calculate and draw an arc showing the trajectory that a projectile will take when it is launched. This is for a 2d side-view game involving physics and gravity. You pull back to aim and adjust your shot strength, then release to shoot your projectile, in this case a ball... not a bird ;). The ball is being launched by setting it's velocity along the vector chosen during aiming.
Currently I am making an arc by shooting smaller projectiles and using the trail renderer, but this is NOT a good solution as it is very slow to update the actual arc and lags behind the aiming a lot when adjusting your aim. I know there is a way to calculate a real-time trajectory arc for a physics based object, because I've seen it in the Rochard trailer (Unity game coming out for PSN). You can see the trailer here: http://www.youtube.com/watch?v=j-ULKB1-8Ko
As you can see in the video, the trajectory path is being updated instantaneously as the aim is adjusted. This is the effect I am looking for. I have scoured UnityAnswers for a solution. I've seen other people ask the same question, but so far the only answers I've seen simply don't provide the effect I am looking for. Any help would be greatly appreciated :)
Okay, I've done a little research on the matter and it seems that I have all the information available to me that I need in order to calculate the trajectory properly.
Apparently all that is needed is the initial velocity, the mass, the angle of launch, the drag, and the acceleration of gravity. All of these are know at any given time. The question is, how to actually do the calculation. If someone could explain I would appreciate it.
I also understand that I should be populating an array of points based on this calculation, using some arbitrary time step. I can then connect the dots, so to speak, in order to draw the path of the trajectory. That actually seems like the easy part. I just need the basic formula for calculating the trajectory.
As an example, this applet is doing EXACTLY what I need to do: http://www.rabidgeek.net/physics-applets/projectile-motion/
All I need is the method they're using :)
Answer by jyounger · Aug 17, 2011 at 07:28 PM
Actually, I take it back. After looking at the Rochard trailer again, it seems that the trajectory being drawn and the actual flight path of the boxes being shot are in fact a parabolic trajectories that don't take drag into account.
I've talked to two math guys about this (including the fellow who made that physics applet I linked to) and I've gotten two different answers, neither of which has worked out very well:
X = initialVelocity*cos(initialAngle)
Y = initialVelocity*sin(initialAngle)
loop N points times(
X = X - (1/2) * drag * (velX*velX) * tStep/mass
Y = Y - (1/2) * drag * (velY*velY) * tStep/mass - grav*tStep
)
and
X = X + mass/drag*initialVelocity*cos(initialAngle)*(1-e*(-drag*tStep/mass))
Y = Y + mass*gravity/drag*tStep+mass/drag*(initialVelocity*sin(initialAngle)+mass*gravity/drag)*(1-e*(-drag/mass*tStep))
The first one actually works a bit better, though drag appears to only be affecting things in a leftward direction. It's hard to tell though because the gravity is behaving very strangely and seems to change directions depending on the initial angle.
The second one doesn't seem to even begin aiming in the proper direction, which at least the first one does.
Man, why is this so hard :(