Game Development Community

Ballistic Physics in TGB

by Tyler Slabinski · in Technical Issues · 05/10/2009 (8:08 pm) · 9 replies

After looking through this site, I am still completely stumped on how to make accurate ballistic physics. Currently, my projectile acts like this:

img11.imageshack.us/img11/7681/ballisticfail.png
That is not a 'perfect' representation, but it does show the problem. In real life, projectiles go farther at 45 degrees than 90. But I do not understand how this works (I have not entered trigonometry classes yet). Here are my variables:

%speedX (the projectile's linearVelocityX at launch)
%speedY (the projectile's linearVelocityY at launch)
%angle (the angle the projectile is heading at launch)

and here is my code:

if (%angle > 90)
      {
            %speedX = 90 - (%angle - 90);
      }
      else
      {
            %speedX = %angle;
      }

      %speedY = (%angle - 45)r * -1;

Does anyone know what the actual equation is for this?

Here is how the degrees are handles at the moment:

img25.imageshack.us/img25/2956/degreesballistic.png

#1
05/11/2009 (11:43 am)
Woops, I sort of didn't double check my post to see if the images were showing. Now everyone should understand.
#2
05/11/2009 (12:07 pm)
%angle = 45;
%totalspeed = 100;

%speedx = cos(%angle) * %totalspeed;  // cos(45) ~= 0.7071
%speedy = sin(%angle) * %totalspeed;  // sin(45) also 0.7071

You'll have to adjust the +/- of the %angle to work just right.

What's going on there is basically that cos(angle) is the ratio of the x component to the hypotenuse (which is your total velocity). Likewise for sin and the y component.

#3
05/11/2009 (12:35 pm)
Ahh, thanks... I wish I was in trigonometry now, I seem to need plenty of help in it. Yet I believe that is all I need...
#4
05/11/2009 (12:52 pm)
Well... Here is basically my code:

function convertVelocity()
{
      //Get the power for how far the projectile will go.
      $speedX = mCos($angle) * $power;

      //Get the height for the projectile.
      $speedY = mSin($angle) * $power;
}

That is basically what you told me to put in. Yet it seems the projectile shoots in a random direction. If the angle is even slightly off, it shoots almost the exact other way.
#5
05/11/2009 (1:15 pm)
Ok, you need to convert your angle to radians.

%radians = (%angle / 180) * 3.141;

Then do mCos(%radians) instead of %angle.

Radians are a just a different way to describe an angle than degrees. There are 2*pi rads in a circle (which is 360degrees). It's not uncommon for Cos() and Sin() to work with radians.

#6
05/11/2009 (1:27 pm)
Ahh, well I will just get rid of my mRadToDeg function. I used it because I use degrees easier.
#7
05/11/2009 (2:05 pm)
This is how it acts now:

img140.imageshack.us/img140/2956/degreesballistic.png
The highlighted ones are currently the only ones correct.
#8
05/12/2009 (12:59 pm)
Bump
#9
05/14/2009 (3:16 pm)
Bump