12.11.2008, 08:24
TRAJECTORY
By ~Cueball~
"Trajectory is the path a moving object follows through space."
A little while ago, while on one of my frequent curiosity-benders, I came up with the idea to learn about projectile motion, flight paths, and all that sort of stuff, and I found it really interesting how launch angles and velocities, etc. directly effect the flight path of an object, and as I have done many times before, decided to implement a PAWN version of my findings.By ~Cueball~
"Trajectory is the path a moving object follows through space."
This include is basically a set of math functions, with a few extras, that will allow you to map moving objects around your server, create cool moving effects, add to your game modes, or if you are like me, simply be fascinated by the direct relation between factors of a flight.
_________________________________________________
To use the include, copy trajectory.inc from pastebin to your 'pawno/include' directory.
Then to include the code to your script, just add the following to your mode:
pawn Code:
#include <trajectory>
![Wink](images/smilies/wink.png)
_________________________________________________
Here is a list of meanings/parameters that are common in most of the functions:
- base: The height of launch in relation to the height of destination. If you want the object to land 10 units above the launch position, use a base height of 10.
- velocity: The total velocity you want to launch with. Remember that launch velocity and launch angle affect all characteristics of flight.
- angle: The angle of launch in relation to the horizontal. Remember that launch velocity and launch angle affect all characteristics of flight.
- gravity: The amount of gravity that you wish to exert on the object as it flies. For realism, use 9.8 (regarded as Earth's normal gravity at sea level), though it is fun to mess around with this
With that said, let's get to the function list (arranged in alphabetical order):
GetFlightData()
Description: Maps the flight of the object into an array.
Parameters: Float:base, Float:velocity, Float:angle, fData[][FLIGHT_DATA], points = sizeof(fData), Float:gravity = 9.8
@param - base: Noted above.
@param - velocity: Noted above.
@param - angle: Noted above.
@param - fData: The array you want to map the flight path in. The array must contain the enumerated index FLIGHT_DATA. See below for an example.
@param - points: The amount of positions along the flight path you wish to map. Using 10 mapping positions provides a realistic flight path - the more you have, the more realistic the flight appears, but the more work the server has to do in regards to moving objects.
@param - gravity: Noted Above.
EXAMPLE
pawn Code:
new data[10][FLIGHT_DATA]; // Creating an array with 10 sets of our FLIGHT_DATA enumeration. This means that we will be mapping 10 positions of flight (if you follow this method).
GetFlightData(-20.0, 10.0, 30.0, data);
/* The object will land 20 units below the launch,
and the launch will occur with a velocity of 10 units
and the launch angle at 30 degrees to the horizontal.
The flight is mapped to our enumerated 'data' array,
and the function will by default assume 'points' to be
the size of 'data' (in this case 10), and gravity to be 9.8. */
See this post for an explanation.
GetFlightConditionsAtDistance()
Description: Retrieves the current flight conditions at any given distance along a flight path. The distance is along the x axis.
Parameters: Float:base, Float:velocity, Float:angle, Float:distance, &Float:time, &Float:height, &Float
![angry](images/smilies/mad.gif)
@param - base: Noted above.
@param - velocity: Noted above.
@param - angle: Noted above.
@param - distance: The distance along the flight path that you want to retrieve the conditions for. The distance is along the x-axis - this means that it is in a forwards direction. Let's say the flight range is 30 units, you could use this function anywhere from 0 to 30 units.
@param - time: The current time that has passed since launch when the object reached the defined distance. This function stores the value to a variable.
@param - height: The current height of the object when the object reached the defined distance. This function stores the value to a variable.
@param - x: The current velocity along the x-axis when the object reached the defined distance. This function stores the value to a variable.
@param - y: The current velocity along the y-axis when the object reached the defined distance. This function stores the value to a variable.
@param - gravity: Noted above.
EXAMPLE
pawn Code:
new Float:time, Float:height, Float:x, Float:y;
GetFlightConditionsAtDistance(-20.0, 10.0, 30.0, 2.0, time, height, x, y);
/* The object will land 20 units below the launch,
and the launch will occur with a velocity of 10 units
and the launch angle at 30 degrees to the horizontal.
The current conditions when the distance was reached
are stored into each argument, and gravity is 9.8. */
GetFlightConditionsAtDistance()
Description: Retrieves the current flight conditions at any given time during a flight.
Parameters: Float:base, Float:velocity, Float:angle, Float:time, &Float:distance, &Float:height, &Float
![angry](images/smilies/mad.gif)
@param - base: Noted above.
@param - velocity: Noted above.
@param - angle: Noted above.
@param - time: The time along the flight that you want to retrieve the conditions for. Let's say the flight max. time is 10 seconds, you could use this function anywhere from 0 to 10 seconds.
@param - distance: The current distance that the object is at from launch when the defined time was reached. This function stores the value to a variable.
@param - height: The current height of the object when the defined time was reached. This function stores the value to a variable.
@param - x: The current velocity along the x-axis when the defined time was reached. This function stores the value to a variable.
@param - y: The current velocity along the y-axis when the defined time was reached. This function stores the value to a variable.
@param - gravity: Noted above.
EXAMPLE
pawn Code:
new Float:distance, Float:height, Float:x, Float:y;
GetFlightConditionsAtTime(-20.0, 10.0, 30.0, 1.5, distance, height, x, y);
/* The object will land 20 units below the launch,
and the launch will occur with a velocity of 10 units
and the launch angle at 30 degrees to the horizontal.
The current conditions when the time was reached
are stored into each argument, and gravity is 9.8. */
GetFlightInitialVelocity()
Description: Retrieves the x- and y-axis velocities from the total velocity and the launch angle.
Parameters: Float:velocity, Float:angle, &Float
![angry](images/smilies/mad.gif)
@param - velocity: Noted above.
@param - angle: Noted above.
@param - x: The initial x-axis velocity of the flight. This function stores the value to a variable.
@param - y: The initial y-axis velocity of the flight. This function stores the value to a variable.
EXAMPLE
pawn Code:
new Float:x, Float:y;
GetFlightInitialVelocity(10.0, 30.0, x, y);
/* The launch will occur with a velocity of 10 units
and the launch angle at 30 degrees to the horizontal.
The initial x- and y-axis velocities will be stored into
each argument, and gravity is 9.8. */
GetFlightMaxHeight()
Description: Retrieves the max. flight height from the total velocity and the launch angle.
Parameters: Float:base, Float:velocity, Float:angle, Float:gravity = 9.8
@param - base: Noted above.
@param - velocity: Noted above.
@param - angle: Noted above.
@param - gravity: Noted above.
Returns: The max. height that the object will reach along the flight path.
EXAMPLE
pawn Code:
new Float:height;
height = GetFlightMaxHeight(-20.0, 10.0, 30.0);
/* The object will land 20 units below the launch,
and the launch will occur with a velocity of 10 units
and the launch angle at 30 degrees to the horizontal
and gravity is 9.8. */
GetFlightMaxRange()
Description: Retrieves the max. range from the total velocity and the launch angle.
Parameters: Float:base, Float:velocity, Float:angle, Float:gravity = 9.8
@param - base: Noted above.
@param - velocity: Noted above.
@param - angle: Noted above.
@param - gravity: Noted above.
Returns: The max. range that the object will reach along the flight path.
EXAMPLE
pawn Code:
new Float:range;
range = GetFlightMaxRange(-20.0, 10.0, 30.0);
/* The object will land 20 units below the launch,
and the launch will occur with a velocity of 10 units
and the launch angle at 30 degrees to the horizontal
and gravity is 9.8. */
GetFlightMaxTime()
Description: Retrieves the max. time from the total velocity and the launch angle.
Parameters: Float:base, Float:velocity, Float:angle, Float:gravity = 9.8
@param - base: Noted above.
@param - velocity: Noted above.
@param - angle: Noted above.
@param - gravity: Noted above.
Returns: The max. time that the object will take to complete the flight along the flight path.
EXAMPLE
pawn Code:
new Float:time;
time = GetFlightMaxTime(-20.0, 10.0, 30.0);
/* The object will land 20 units below the launch,
and the launch will occur with a velocity of 10 units
and the launch angle at 30 degrees to the horizontal
and gravity is 9.8. */
GetRequiredAngle()
Description: Retrieves the required launch angle from the total velocity and the max. range.
Parameters: Float:range, Float:velocity, Float:gravity = 9.8
@param - range: The max. range the flight path will take.
@param - velocity: Noted above.
@param - gravity: Noted above.
Returns: The required launch angle to complete the flight along the flight path.
EXAMPLE
pawn Code:
new Float:angle;
angle = GetRequiredAngle(40.0, 10.0);
/* The object will land 40 units away from the launch,
and the launch will occur with a velocity of 10 units,
and gravity is 9.8. */
GetRequiredConditions()
Description: Retrieves the required launch velocity and angle from the max. range and max. height.
Parameters: Float:range, Float
![Tongue](images/smilies/razz.gif)
@param - range: The max. range the flight path will take.
@param - peak: The max. height the object will reach along the flight path.
@param - velocity: The required velocity for the object to reach the given range and peak. This function stores the value to a variable.
@param - angle: The required angle for the object to reach the given range and peak. This function stores the value to a variable.
@param - gravity: Noted above.
EXAMPLE
pawn Code:
new Float:velocity, Float:angle;
GetRequiredConditions(40.0, 20.0, velocity, angle);
/* The object will land 40 units away from the launch,
and the launch will have a max. height of 10 units.
The required velocity and angle will be stored into
each argument, and gravity is 9.8. */
GetRequiredVelocity()
Description: Retrieves the required launch velocity from the launch angle and the max. range.
Parameters: Float:range, Float:angle, Float:gravity = 9.8
@param - range: The max. range the flight path will take.
@param - angle: Noted above.
Returns: The required launch velocity to complete the flight along the flight path.
EXAMPLE
pawn Code:
new Float:velocity;
velocity = GetRequiredVelocity(40.0, 30.0);
/* The object will land 40 units away from the launch,
and the launch will occur with a launch angle of 30 degrees
to the horizontal, and gravity is 9.8. */
GetVelocity()
Description: Retrieves the total velocity from the x- and y-axis counterparts.
Parameters: Float
![angry](images/smilies/mad.gif)
@param - x: The x-axis velocity.
@param - y: The y-axis velocity.
Returns: The total velocity from it's x- and y-axis counterparts.
EXAMPLE
pawn Code:
new Float:velocity;
velocity = GetVelocity(10.0, 60.0);
/* The current x- and y-axis velocities are 10 and 60,
and the function will return what the total velocity is. */
Now, all that is well and good, but how can we actually use these functions effectively? Let's start off with an example - how about printing out the flight data from our previous GetFlightData() explanation? Easy! Just throw this into a new script:
pawn Code:
#include <trajectory>
public OnFilterScriptInit()
{
new Float:base = -20.0, Float:velocity = 10.0, Float:angle = 30.0, data[10][FLIGHT_DATA];
printf("Destination height:\t%.2f", base);
printf("Initial velocity:\t%.2f", velocity);
printf("Initial angle:\t\t%.2f\n", angle);
GetFlightData(base, velocity, angle, data);
for(new i; i < sizeof(data); i++)
{
printf("Point: \t\t\t%d", i + 1);
printf("Distance from launch:\t%.2f", data[i][FLIGHT_DISTANCE]);
printf("Height above launch:\t%.2f", data[i][FLIGHT_HEIGHT]);
printf("Velocity - x direction:\t%.2f", data[i][FLIGHT_VELOCITY][0]);
printf("Velocity - y direction:\t%.2f", data[i][FLIGHT_VELOCITY][1]);
printf("Velocity - total:\t%.2f\n", GetVelocity(data[i][FLIGHT_VELOCITY][0], data[i][FLIGHT_VELOCITY][1]));
}
return 1;
}
Code:
Destination height: -20.00 Initial velocity: 10.00 Initial angle: 30.00 Point: 1 Distance from launch: 2.24 Height above launch: -19.03 Velocity - x direction: 8.66 Velocity - y direction: 2.45 Velocity - total: 9.00 Point: 2 Distance from launch: 4.49 Height above launch: -18.72 Velocity - x direction: 8.66 Velocity - y direction: -0.08 Velocity - total: 8.66 Point: 3 Distance from launch: 6.73 Height above launch: -19.07 Velocity - x direction: 8.66 Velocity - y direction: -2.62 Velocity - total: 9.04 Point: 4 Distance from launch: 8.98 Height above launch: -20.08 Velocity - x direction: 8.66 Velocity - y direction: -5.16 Velocity - total: 10.08 Point: 5 Distance from launch: 11.23 Height above launch: -21.75 Velocity - x direction: 8.66 Velocity - y direction: -7.71 Velocity - total: 11.59 Point: 6 Distance from launch: 13.47 Height above launch: -24.08 Velocity - x direction: 8.66 Velocity - y direction: -10.25 Velocity - total: 13.42 Point: 7 Distance from launch: 15.72 Height above launch: -27.07 Velocity - x direction: 8.66 Velocity - y direction: -12.79 Velocity - total: 15.44 Point: 8 Distance from launch: 17.97 Height above launch: -30.72 Velocity - x direction: 8.66 Velocity - y direction: -15.33 Velocity - total: 17.61 Point: 9 Distance from launch: 20.21 Height above launch: -35.03 Velocity - x direction: 8.66 Velocity - y direction: -17.87 Velocity - total: 19.86 Point: 10 Distance from launch: 22.46 Height above launch: -40.00 Velocity - x direction: 8.66 Velocity - y direction: -20.42 Velocity - total: 22.18
_________________________________________________
Another example may help a lot of you, and that is how to use the data effectively. How could I make a projectile shoot across my map? Once again, this is a relatively easy task, so I have included it in the pastebin links (see below).
SEE LINKS FOR CODE - flight.pwn
That's easy as pie, just some basic coding to pull off an awesome effect - a grenade flying across the map in the direction the player is facing! It doesn't stop there, you can curve the flight by changing the angle to retrieve the co-ordinates at:pawn Code:
GetXYInDirectionOfPosition((fAngle += 5.0), x, y, data[count][FLIGHT_DISTANCE]);
_________________________________________________
I am working on an aiming system to launch projectiles, so check out some of the preview screens. I may not finish this project though, so don't hold me to it:
![](/imageshack/img378/6699/samp108if8.png)
![](/imageshack/img152/4030/samp109ma7.png)
Just something nice and simple
![Wink](images/smilies/wink.png)
_________________________________________________
Now, on to the download links: _________________________________________________
That's really all there is to say, this is just a simple include that lets you use projectiles, flight paths, all that jazz. Hopefully it helps somebody, but if not, at least I enjoyed learning about trajectory
![Smiley](images/smilies/smile.png)
~Cueball~