SA-MP Forums [Include] [INC]Trajectory - V 0.1 || By ~Cueball~
 Members List Search Today's Posts Mark Forums Read

 12/11/2008, 08:24 AM #1 Cueball Big Clucker   Join Date: Jul 2007 Posts: 109 Reputation: 14 [INC]Trajectory - V 0.1 || By ~Cueball~ 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 ` I have included a_samp.inc inside trajectory.inc, so you don't even need to '#include ' 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 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~
 12/11/2008, 01:53 PM #2 Grove Huge Clucker   Join Date: Jan 2007 Posts: 299 Reputation: 6 Re: [INC]Trajectory - V 0.1 || By ~Cueball~ Interesting... nice work, I like the sound of the aiming system to
 12/11/2008, 03:36 PM #3 eXtreme72 Little Clucker   Join Date: Jul 2008 Posts: 37 Reputation: 0 Re: [INC]Trajectory - V 0.1 || By ~Cueball~ Very nice. Maybe gonna use this.
13/11/2008, 05:20 AM   #4
Cueball
Big Clucker

Join Date: Jul 2007
Posts: 109
Reputation: 14
Re: [INC]Trajectory - V 0.1 || By ~Cueball~

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~

 03/02/2009, 06:16 AM #5 Backwardsman97 High-roller     Join Date: Nov 2007 Posts: 1,501 Reputation: 36 Re: [INC]Trajectory - V 0.1 || By ~Cueball~ I think a lot could be done with this and I wanna use it but idk where to start. __________________
 03/02/2009, 10:38 AM #6 extra Big Clucker   Join Date: Apr 2008 Posts: 111 Reputation: 0 Re: [INC]Trajectory - V 0.1 || By ~Cueball~ Nice INC GJ
 25/02/2010, 01:11 AM #7 LucasB[FL] Little Clucker   Join Date: Feb 2009 Posts: 5 Reputation: 0 Re: [INC]Trajectory - V 0.1 || By ~Cueball~ Filefront and Sendspace links are down. Can you fix them?
25/02/2010, 05:57 AM   #8
Cueball
Big Clucker

Join Date: Jul 2007
Posts: 109
Reputation: 14
Re: [INC]Trajectory - V 0.1 || By ~Cueball~

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~

 04/04/2012, 06:33 PM #9 AirKite Gangsta     Join Date: Feb 2009 Posts: 733 Reputation: 21 Re: [INC]Trajectory - V 0.1 || By ~Cueball~ Nice... Need more exaples + videos please =)
 04/04/2012, 07:00 PM #10 Vukilore Huge Clucker     Join Date: Jan 2011 Location: http://forum.sa-mp-fr.com/index.php Posts: 300 Reputation: 110 Re : [INC]Trajectory - V 0.1 || By ~Cueball~ Nice !!!!!!!!!! I will be able to make a knife shot __________________ http://forum.sa-mp-fr.com/index.php. << here rest the french forum, in piece.

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post [Include] [INC] Zones By ~Cueball~ - V 2.0 Cueball Includes 37 29/12/2018 02:03 AM

All times are GMT. The time now is 03:43 AM.

 -- (EN) English -- (RU) Русски SA-MP - Archive - Top