- Home /
How to calculate the distance of an object with velocity?
Instead of calculating distance between 1 object and another, can I calculate the distance between an object and another, can I calculate the distance using the 'arch' an object with dynamics has (like a mortar firing)? Here's my example:
projectile trajectory:
http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
good luck with that math! :) unforuntately I can't help you with that lol
Answer by alwayscodeangry · Jul 12, 2014 at 10:58 PM
Most of the trajectory equations can also be found here. I'm not 100% sure whether you're trying to get the horizontal distance or the arc length, so I've included both, and luckily Mathf
has all the functionality we need:
private static float GRAVITY = 9.8067f;
public static float GetHorizontalDistance(float velocity, float launchAngle, bool degrees = false)
{
// convert to radians if necessary
if (degrees)
{
launchAngle *= Mathf.Deg2Rad;
}
return (velocity * velocity * Mathf.Sin(2.0f * launchAngle)) / GRAVITY;
}
public static float GetArcLength(float velocity, float launchAngle, bool degrees = false)
{
// convert to radians if necessary
if (degrees)
{
launchAngle *= Mathf.Deg2Rad;
}
// pre-calculate sine and cosine of launchAngle and square of velocity
float sinAngle = Mathf.Sin(launchAngle);
float cosAngle = Mathf.Cos(launchAngle);
float vSquared = velocity * velocity;
return (vSquared * (sinAngle + cosAngle * Mathf.Log((1.0f + sinAngle) / cosAngle))) / GRAVITY;
}
You may need to swap out the const GRAVITY for Unity's value, depending on your usage case.
that is some angrycode!... :)
(couldn't help myself.. I just like your pseudo name)
Your answer
Follow this Question
Related Questions
calculate the estimated distance travel based on the Force applied to Rigid Body 1 Answer
Enemy AI help with height check 1 Answer
Calculate where an object is going to land 0 Answers
Maintaining specific distance between two objects 1 Answer
real life:sin(2Pi)=0 unity Mathf.Sin(2Pi)=1.748456E-07 1 Answer