[Include] projectile.inc - PeppeAC's physics modified and simpler version
#1

projectile.inc

Version: 1.4.0 || Last Updated: 27 Sept, 2018

Include will let you create SPHERE based physics SIMULATION with real-time variables like Gravity, Ground Friction, Collision Friction (with an object), and Air Resistance.

You can involve an object to act with the simulation under a callback: OnProjectileUpdate.

downloads:

Source (projectile.inc)https://github.com/Agneese-Saini/SA-...projectile.inc
Example: Throw Deaglehttps://github.com/Agneese-Saini/SA-...row_deagle.pwn
Example: M4-Grenade Launcherhttps://github.com/Agneese-Saini/SA-...e_launcher.pwn
Dependency: ColAndreas Pluginhttps://sampforum.blast.hk/showthread.php?tid=586068
* Coming soon: Billiards pool game

functions:
PHP код:
CreateProjectile(Float:xFloat:yFloat:zFloat:vxFloat:vyFloat:vzFloat:rx 0.0Float:ry 0.0Float:rz 0.0Float:spherecol_radius 1.0Float:ground_friction 5.0Float:collision_friction 0.2Float:air_resistance 0.5Float:gravity 10.0Float:playercol_radius 0.8bool:collide_simulation falseFloat:mass 1.0); 
Purpose:
Create's a physics simulation.
Returns:
Simulation id. (range: MAX_PROJECTILES)
Returns INVALID_PROJECTILE_ID if function was unsuccessful.
Parameters:
"x": initial x position
"y": initial y position
"z": initial z position
"vx": initial x velocity
"vy": initial y velocity
"vz": initial z velocity
"rx": initial x rotation (optional)
"ry": initial y rotation (optional)
"rz": initial z rotation (optional)
"spherecol_radius": simulation's collision sphere radius (optional)
"ground_friction": value of friction caused by ground (optional)
"collision_friction": value of friction caused by colliding with an object (optional)
"air_resistance": value of resistance caused by air (optional)
"gravity": value of gravity's pull (optional)
"playercol_radius": value of collision radius of a player, if set to 0.0, there will be no collision with players (optional)
"collide_simulation": true = simulation will be able to detect collision with another simulation and bounce back (optional)
"mass": mass of simulation (only useful if collide_simulation is set to true) (optional)
PHP код:
IsValidProjectile(projid
Purpose:
Validates a projectile's existance.
Returns:
true = projectile exists
false = doesn't exist
Parameters:
"projid": projectile id
PHP код:
DestroyProjectile(projid
Purpose:
Destroys a projectile simulation.
Returns:
1 = projectile was destroyed
0 = projectile id doesn't exist
Parameters:
"projid": projectile id
PHP код:
GetProjectilePoolSize() 
Purpose:
Returns the highest projectile currently in use of the server.
Returns:
The highest projectileid currently in use on the server or -1 if there are no active projectiles
Parameters:
"This function has no parameters."
PHP код:
GetProjectilePos(projid, &Float:x, &Float:y, &Float:z
Purpose:
Gets projectile's current position.
Returns:
1 = function was success
0 = unsuccessful
Parameters:
"projid": projectile id
"x": x position will be stored in here
"y": y position will be stored in here
"z": z position will be stored in here
PHP код:
GetProjectileRot(projid, &Float:rx, &Float:ry, &Float:rz
Purpose:
Gets projectile's current rotation.
Returns:
1 = function was success
0 = unsuccessful
Parameters:
"projid": projectile id
"rx": x rotation will be stored in here
"ry": y rotation will be stored in here
"rz": z rotation will be stored in here
PHP код:
GetProjectileVel(projid, &Float:vx, &Float:vy, &Float:vz
Purpose:
Gets projectile's current velocity.
Returns:
1 = function was success
0 = unsuccessful
Parameters:
"projid": projectile id
"vx": x velocity will be stored in here
"vy": y velocity will be stored in here
"vz": z velocity will be stored in here
PHP код:
UpdateProjectileVel(projidFloat:vxFloat:vyFloat:vz
Purpose:
Sets projectile's current velocity to the values.
Returns:
1 = function was success
0 = unsuccessful
Parameters:
"projid": projectile id
"vx": x velocity to change
"vy": y velocity to change
"vz": z velocity to change
callbacks:
PHP код:
OnProjectileUpdate(projid
Purpose:
Called every time a projectile's position/velocity is changed.
Returns:
This callback does not handle returns
Parameters:
"projid": projectile id
PHP код:
OnProjectileStop(projid
Purpose:
Called when a projectile has no more velocity left (i.e. 0).
Returns:
This callback does not handle returns
Parameters:
"projid": projectile id
PHP код:
OnProjectileCollide(projidtypeFloat:xFloat:yFloat:zextraid
Purpose:
Called when a projectile collides with something.
Returns:
This callback does not handle returns
Parameters:
"projid": projectile id
"type": type of thing the projectile collided with (checkout "definitions" for type defines)
"x": collision x position
"y": collision yposition
"z": collision z position
"extraid": id of the collided thing (PROJECTILE_COLLIDE_GROUND and PROJECTILE_COLLIDE_CIELING won't return any extraid - PROJECTILE_COLLIDE_SIMULATION will return the other simulation's id - PROJECTILE_COLLIDE_OBJECT will return the objectid - PROJECTILE_COLLIDE_PLAYER will return the player's id)
definitions:
PHP код:
#define MAX_PROJECTILES 100 
^^ Define the maximum amount of projectiles that can be created and handled at once
(user can pre-define before inclusion)
PHP код:
#define PROJECTILE_TIMER_INTERVAL 20 
^^ Define the timer interval which will be responsible for updating projectiles' data in every timer step
(user can pre-define before inclusion)
PHP код:
#define INVALID_PROJECTILE_ID -1 
^^ Invalid id definition of a projectile (returned by "CreateProjectile" when function is unsuccessful)
PHP код:
#define PROJECTILE_COLLIDE_GROUND 0
#define PROJECTILE_COLLIDE_CIELING 1
#define PROJECTILE_COLLIDE_SIMULATION 2
#define PROJECTILE_COLLIDE_OBJECT 3
#define PROJECTILE_COLLIDE_PLAYER 4 
^^ Type of projectiles (used in callback: "OnProjectileCollide")
Reply
#2

Great release! This really does simplify the use projectiles.

There should be some form of rotation though.
Reply
#3

Quote:
Originally Posted by Crayder
Посмотреть сообщение
There should be some form of rotation though.
Since this is a simulation based on sphere's it doesn't matter. You can make your own rotation under OnProjectileUpdate, and use the velocity function to depict direction.


Also i was concerned about raycasting 3 times in a 20ms timer. That would cause lag right?
Reply
#4

Very imprassive +REP , I loved it
Reply
#5

Quote:
Originally Posted by Gammix
Посмотреть сообщение
Since this is a simulation based on sphere's it doesn't matter. You can make your own rotation under OnProjectileUpdate, and use the velocity function to depict direction.
YOU should do the rotation BECAUSE it's a sphere based system... Take the example for example (lol), the gun should spin a little from throwing it. And balls spin.
Quote:
Originally Posted by Gammix
Посмотреть сообщение
Also i was concerned about raycasting 3 times in a 20ms timer. That would cause lag right?
Not necessarily, unless you had like 500 objects moving at once... which I'm going to try...
Reply
#6

Quote:
Originally Posted by Crayder
Посмотреть сообщение
YOU should do the rotation BECAUSE it's a sphere based system... Take the example for example (lol), the gun should spin a little from throwing it. And balls spin.Not necessarily, unless you had like 500 objects moving at once... which I'm going to try...
That would be great, let me know the result and are you going to use this include for testing or just raycast?

And anyways, i'll add rotation soon in updates upon your request.
Reply
#7

Impressive, +REP,
still curious tho, what can be accomplished by this? I mean any ideas?
Reply
#8

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
Impressive, +REP,
still curious tho, what can be accomplished by this? I mean any ideas?
- Weapon drop (when player dies, make them fall in the direction where player fells)
- Custom grenades, simulating tear gas to make it accurate for cough animation on affected players
- Simulating grenades/moltoves to detect their position (hard!)
- Knife throw
- Car weapons, like a minigun with actual physics!
etc.
Reply
#9

Update v1.1!
- Player collision detection added:
Код:
Projectile(Float:x, Float:y, Float:z, Float:vx, Float:vy, Float:vz, Float:sphere_radius = 1.0, Float:acceleration = 0.0, Float:mass = 1.0, Float:friction = 10.0, Float:air_resistance = 0.5, Float:gravity = 4.1, bool:playercol = true);
Notice bool:playercol = true, setting it false will disable player collisions and the simulation will pass through players.
Reply
#10

nicely done Gammix
Reply
#11

Quote:
Originally Posted by Gammix
Посмотреть сообщение
Since this is a simulation based on sphere's it doesn't matter. You can make your own rotation under OnProjectileUpdate, and use the velocity function to depict direction.


Also i was concerned about raycasting 3 times in a 20ms timer. That would cause lag right?
No it should not. I use it to get Z for every NPC (about 30 of them) on every OnPlayerUpdate.
Reply
#12

Quote:
Originally Posted by Gammix
Посмотреть сообщение
That would be great, let me know the result and are you going to use this include for testing or just raycast?
This include of course, I wanna see it in action!

Won't be able to until either this weekend or the week after next though. If anyone else wants to do it in the meantime that'd be great, but I'm still going to try it on my PC since it's shitty (which means the test will accommodate cheap, shitty servers too kinda).

EDIT: @DRIFT_HUNTER: Well a ColAndreas raycast is a bit heavier than the position functions. It still shouldn't, but that's why people fear ColAndreas. They think it's a lot heavier than it really is.
Reply
#13

We have never had any complaints of ColAndreas not being able to keep up. Even 1000 times a second should be no more than a few drops in the cup.

Early testing. (MapAndreas generation)

http://forum.sa-mp.com/showpost.php?...0&postcount=92

3.) 6000x6000 is only 36000000 raycasts the amount of objects does not matter this would take approximately 4 minutes.
Reply
#14

Quote:
Originally Posted by Crayder
Посмотреть сообщение
This include of course, I wanna see it in action!

Won't be able to until either this weekend or the week after next though. If anyone else wants to do it in the meantime that'd be great, but I'm still going to try it on my PC since it's shitty (which means the test will accommodate cheap, shitty servers too kinda).

EDIT: @DRIFT_HUNTER: Well a ColAndreas raycast is a bit heavier than the position functions. It still shouldn't, but that's why people fear ColAndreas. They think it's a lot heavier than it really is.
I meant that im finding ground pos for every NPC using ColAndreas. And yeah im one of the people that feared and still fears it could cause some problems when i implement local collision avoidance and path fining. But so far so good, no problems at all.
Reply
#15

Interesting, I was waiting an update from PeppeAC to finish my custom vehicle explosion (tires, hoods, trunks, exhausts flying away), but it was kind of weird due to optimization and rotation issues. Maybe this version help with the optimization.
Reply
#16

Update v1.2:
  • SA world collision check is now highly accurate or say 50% more collision accuracy than before. And 1 or 2 less calculation.
  • There are 2 types of frictions now:
    - "collision_friction": affects the speed when collision occurs with player or an object.
    - "fround_friction": affects the speed when collision occurs with low Z bound
  • Rotation has been introduced:
    PHP код:
    GetProjectileRot(projid, &Float:rx, &Float:ry, &Float:rz); 
  • New example posted as well!
Reply
#17

Update v1.3:
  • "acceleration" has been removed from Projectile(..) function.
  • Now detecting max and min heights is much better using CA_RayCastLine instead of CA_RayCastLineAngle and there is a workaround done if no collision is detected.
  • Collision detection made more accurate with SA world and Players (players are treated as an object as well so the projectile will rotate accordingly!)
  • "OnProjectileCollide" callback updated with new parameters:
    PHP код:
    forward OnProjectileCollide(projidtypeFloat:xFloat:yFloat:zmodelid); 
  • New function "PushProjectile", lets you add to current velocity:
    PHP код:
    PushProjectile(projidFloat:vxFloat:vyFloat:vz); 
Edit:
Quick update v1.3.2:
- Now timer is one global one since multiple timers were causing trouble since they are SAMP timers!
- Fixed simulation going under ground due to min_height calculation.
Reply
#18

Pretty neat! Good job.
Reply
#19

Are the objects in your examples supposed to hold the same altitude while they move? They also don't seem to react to objects with which they collide.
Reply
#20

Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
Are the objects in your examples supposed to hold the same altitude while they move? They also don't seem to react to objects with which they collide.
Altitude is varying, 2 CA_RayCastLine are used to calculate the maximum and minimum Z the object can reach.

By object reaction, you mean simulation to simulation response? If so no there is no simulation to simulation reaction yet but can be added in future version.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)