[Include] [INC]Trajectory - V 0.1 || By ~Cueball~
#1

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.

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>
I have included a_samp.inc inside trajectory.inc, so you don't even need to '#include <a_samp>' if you don't want to

_________________________________________________

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. */
Now, how do you use the information plotted to the array? Along with the examples included in the zipped package and further down the page, I will explain what the FLIGHT_DATA enumeration holds, and how you can use it.

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, &Float:y, Float:gravity = 9.8
@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, &Float:y, Float:gravity = 9.8
@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, &Float:y
@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, Floateak, &Float:velocity, &Float:angle, Float:gravity = 9.8
@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, Float:y
@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;
}
The output would look like this:
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
With that output, you can see that the total velocity actually increased (it was accelerating) after point 4 because it was falling due to gravity.

_________________________________________________

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]);
That will create a curve to the right. You can do all sorts of crazy things with projectiles, so just get going with it!

_________________________________________________

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:




Just something nice and simple

_________________________________________________

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

~Cueball~
Reply
#2

Interesting... nice work, I like the sound of the aiming system to
Reply
#3

Very nice. Maybe gonna use this.
Reply
#4

Addage to the main post (due to the 20000 character limit, I have to post here and refer to it):

_________________________________________________

FLIGHT_DATA contains this:
pawn Code:
enum FLIGHT_DATA {
  Float:FLIGHT_DISTANCE,  // The distance away from the launch position (x-axis).
  Float:FLIGHT_HEIGHT,  // The height above the launch position (y-axis).
  Float:FLIGHT_VELOCITY[2] // The x- and y-axis velocities of the object.
}
Read the wiki for documentation on enumeration.

Now, to use the information stored, we simply use the enumerated index (using the previous example code):
pawn Code:
// We can now use any index and any enumerated index on the array:
MoveObjectPos(myObject, 0.0, data[4][FLIGHT_DISTANCE], data[4][FLIGHT_HEIGHT], GetVelocity(data[4][FLIGHT_VELOCITY][0], data[4][FLIGHT_VELOCITY][1]));
That's just a simple method, so try to understand it. To move the object from a launch position that is not the origin (x-, y-, and z-axis = 0.0), you have to add the distance and height on to the current object position. Check the included example in the zipped package for how to do this

A note about the velocities - the first cell is x-axis velocity (movement in a one dimensional world, so 360 degrees to the vertical), and the second cell is y-axis velocity (movement up and down). To get the total velocity from these two values, use GetVelocity() (explained further down the page).

_________________________________________________

Quote:
Originally Posted by Grove
Interesting... nice work, I like the sound of the aiming system to
Quote:
Originally Posted by ęΧţřЄшĕ95
Very nice. Maybe gonna use this.
Quote:
Originally Posted by Westie
This is really nice. I might add something like this into my object grouper, for well, setting the trajectory of objects, etc.
Cheers everybody. The aiming system is going well, should be completed relatively soon, just all depends on what I'm doing with friends and chums, and whether I feel like spending a few hours perfecting something

@Westie: Go for it, would be awesome to see multiple objects flying across the map (missile strike anybody?)

I'm not expecting any updates for this include at this point, because I really have done everything I could find on trajectory (took about a week to track down everything I wanted), but hopefully it can help people.

A side note: These functions ignore air resistance, because there is no "wind" in sa-mp, so I decided not to have to introduce mass and friction and all that stuff. If you want all this stuff, you're welcome to research it

~Cueball~
Reply
#5

I think a lot could be done with this and I wanna use it but idk where to start.
Reply
#6

Nice INC GJ
Reply
#7

Filefront and Sendspace links are down. Can you fix them?
Reply
#8

Quote:
Originally Posted by LucasB[FL
]
Filefront and Sendspace links are down. Can you fix them?
Indeed they have, and I have no copies of the zipped package nor what was in it, so unless someone else can put up a copy of theirs (which I will be very grateful for), we will have to make do with the two files I updated in the first post as pastebin links. Sorry

By the way, nice 1 year-old bump

~Cueball~
Reply
#9

Nice... Need more exaples + videos please =)
Reply
#10

Nice !!!!!!!!!!
I will be able to make a knife shot
Reply
#11

It seems very nice, and well organized thread. Good job, Cueball.
Reply
#12

Looks good but seems real advance to me. What else can i script using this include?
Reply
#13

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Hey, I just decided to use this again (I did a while ago but didn't understand trajectory enough)
Now I'm back with more knowledge!

But, I can't seem to find the rotation angle?
I'm assuming 'angle' is the angle of elevation, but I can't see where I input the angle of rotation in your example.


If there is none, then how can I change the direction of my flight path?

Say I want to set an array of paths in the direction 46 degrees from north, angled 60 degrees in the air and a heading velocity of 15 m/s and to land at a base of -5.0...
I hope you realize you've made people think that this is a recent addition to the forum! Haha! Flawless bump indeed.

Anyways, I've not given much look into the matter but I'm fairly certain you'd need something along the lines of:
pawn Код:
const NUM_ELEMENTS = 30; // This can be any arbitrary number, it merely represents the number of "steps" into the trajectory. For a smoother transition, a larger number of steps is ideal.

new data[NUM_ELEMENTS][FLIGHT_DATA];

GetFlightData(-5.0, 15.0, 60.0, data);

for(new i = 0; i < sizeof(data); ++i)
{
    CreateObject(1337, x + (data[i][FLIGHT_DISTANCE] * floatsin(-46.0, degrees)), y + (data[i][FLIGHT_DISTANCE] * floatcos(-46.0, degrees)), z + data[i][FLIGHT_HEIGHT], 0.0, 0.0, 0.0);
}
Hopefully I've helped some!
Reply
#14

Interesting to make throwable weapons or a golf minigame, haha.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)