SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Includes (https://forum.sa-mp.com/forumdisplay.php?f=83)
-   -   [Include] [alpha] Objects Physics - Handle objects collisions and more. (https://forum.sa-mp.com/showthread.php?t=446286)

PeppeAC 25/06/2013 12:14 PM

Objects Physics - Handle objects 3D/2D physics (collisions, gravity, ...)
 
Objects Physics

This include handles via script collisions and other physics of the objects.
Open physics.inc to view the list of functions.

Required:
modelsizes.inc by ******
foreach.inc by ******
ColAndreas by Pottus

Github:
https://github.com/uPeppe/physics.inc

Examples:
Changelogs:
Objects Physics 0.1
  • Added support to 3D physics.
  • Some functions edited tu support 3D features.
  • Some improvements and bug fixes.
Objects Physics 0.2
  • PHY_ObjectDelate renamed to PHY_DeleteObject.
  • Wall collisions fixed and improved.
  • Added optional "constant" parameter to PHY_CreateWall, like in PHY_SetObjectZBound.
  • Failed attempt to make a better ball rolling using quaternions. See the internal function PHY_Rall if you want to try to fix it.
Objects Physics 0.2 R2
  • Use of foreach.inc to optimize the script.
  • Added PHY_ApplyRotation (can be used in PHY_OnObjectUpdate to make a manual rotation), PHY_GetObjectSpeed and PHY_GetObjectMoveAngle.
Objects Physics 0.3
  • PHY_MoveObject renamed to PHY_SetObjectVelocity.
  • Added PHY_SetObjectAcceleration and PHY_GetObjectAcceleration.
  • Added PHY_SetObjectAirResistance and PHY_GetObjectAirResistance (similar to friction, but proportional to velocity).
  • Script improvements and optimizations.
Objects Physics 0.4
  • Added ColAndreas support, put #define COLANDREAS before including physics.inc to activate it. Related function: PHY_UseColAndreas(objectid, mode).
  • New callbacks: OnObjectCollideWithZBound(objectid, lowhigh), OnObjectCollideWithSAWorld(objectid, Float:cx, Float:cy, Float:cz).
  • New algorithm for object to object 3D collisions.
  • Added a new demo gamemode (colandreas.pwn), that uses ColAndreas and implements a little dog system.
physics.inc v0.5
  • More realistic rolling of the objects, through calculations made by using quaternions instead of Euler angles. This can be activated by selecting PHY_ROLLING_MODE_ADVANCED in PHY_RollObject.

Documentation:
pawn Code:
PHY_InitObject(objectid, modelid = 0, Float:mass = 1.0, Float:size = FLOAT_NAN, mode = PHY_MODE_3D)
/*Starts using physics for objectid.
modelid - object's modelid, used to get its size with modelsizes include.
mass - object's mass, it is like its weight and is used in collisions.
size - object's sphere radius, taken from modelsizes.inc by default.
mode - PHY_MODE_3D or PHY_MODE_2D.*/


PHY_GetObjectMode(objectid)

PHY_DeleteObject(objectid)
/*Stops using physics for objectid (doesn't destroy it).*/

PHY_SetObjectVelocity(objectid, Float:vx, Float:vy, Float:vz = 0.0)
/*Moves the object with vx, vy, vz velocities.*/

PHY_GetObjectVelocity(objectid, &Float:vx, &Float:vy, &Float:vz)

PHY_IsObjectMoving(objectid)

PHY_SetObjectAcceleration(objectid, Float:ax, Float:ay, Float:az = 0.0)
/*Sets the object's acceleration.*/

PHY_GetObjectAcceleration(objectid, &Float:ax, &Float:ay, &Float:az)

PHY_GetObjectSpeed(objectid, &Float:speed, _3D = 0)

PHY_GetObjectMoveAngle(objectid, &Float:moveangle)

stock PHY_RollObject(objectid, toggle = 1, rollingmode = PHY_ROLLING_MODE_DEFAULT)
/* Starts rolling the object when it moves of toggle = 1 or stops if toggle = 0.
rollingmode = PHY_ROLLING_MODE_DEFAULT (Euler angles) or PHY_ROLLING_MODE_ADVANCED (Quaternions)
When using advanced rolling mode, if you manually use SetObjectRot in your script, it is adviced to
call PHY_RollObject again, in order to recalculate its quaternion angles.*/


PHY_IsObjectRolling(objectid)

PHY_SetObjectFriction(objectid, Float:friction)
/*Applies friction to the object when it moves on the floor (at its lowest Z). If friction is applied, the object gradually slows down.*/

Float:PHY_GetObjectFriction(objectid)

PHY_SetObjectAirResistance(objectid, Float:resistance)
/*Applies air resistance to the object when it moves. The difference between friction and air resistance is that the former works only if the object is on the floor and the letter is also slows down the object proportionally to its velocity.*/

Float:PHY_GetObjectAirResistance(objectid)

PHY_SetObjectZBound(objectid, Float:low = FLOAT_NAN, Float:high = FLOAT_NAN, Float:constant = 0.0)
/*Limits the object's Z position.
low - The lowest Z that the object can have (you can use FLOAT_NEG_INFINITY). If it is set to NaN it doesn't change.
high - The highest Z that the object can have (you can use FLOAT_INFINITY). If it is set to NaN it doesn't change.
(When you use PHY_InitObject lowest Z is set to the current object's Z and highest Z to FLOAT_INFINITY.
constant - It should be from 0.0 to 1.0. If it is 1.0 the object doesn't lose velocity,
if it is 0.0 the object stops when it bounces. It could be a middle ground.*/


PHY_SetObjectGravity(objectid, Float:gravity)
/*Sets the gravity's acceleration that the object is subjected to.*/

Float:PHY_GetObjectGravity(objectid)

PHY_SetObjectWorld(objectid, world)
/*Object and walls collide only if the are in the same world or one of them is in the world 0 (default).*/

PHY_ToggleObjectPlayerColls(objectid, toggle = 1, Float:constant = 1.0, Float:distoffset = 0.8, Float:zoffsetlow = 1.0, Float:zoffsethigh = 1.0)
/*Toggles object's collisions with players.
- constant - It should be from 0.0 to 1.0. If it is 1.0 the object doesn't lose velocity,
if it is 0.0 the object stops when it bounces. It could be a middle ground.
- distoffset - The distance at which the object collides with the player.
- zoffsetlow/zoffsethigh - The max Z distance (downward/upward) at which the object collides with the player.*/


PHY_ApplyRotation(objectid, Float:speed, Float:moveangle)
/*Function used internally to rotate the objects.*/

PHY_CreateWall(Float:x1, Float:y1, Float:x2, Float:y2, Float:constant = 1.0, Float:low = FLOAT_NEG_INFINITY, Float:high = FLOAT_INFINITY)
/*Creates a collision wall (straight line) from A(x1, y1) to B(x2, y2).
constant should be from 0.0 to 1.0. If it is 1.0 the object doesn't lose velocity,
if it is 0.0 the object stops when it collides.
low is the lowest wall's Z, high is the highest. If they're set to default the wall is like infinitely high. */


PHY_CreateArea(Float:minX, Float:minY, Float:maxX, Float:maxY, Float:constant = 1.0, Float:low = FLOAT_NEG_INFINITY, Float:high = FLOAT_INFINITY)
/*Creates four walls that form an area. Works like IsPlayerInArea.*/

PHY_DestroyWall(wallid)

PHY_SetWallWorld(wallid, world)

PHY_CreateCylinder(Float:x, Float:y, Float:size, Float:constant = 1.0, Float:low = FLOAT_NEG_INFINITY, Float:high = FLOAT_INFINITY)
/*Creates a collision cylinder at position x, y.
constant should be from 0.0 to 1.0. If it is 1.0 the object doesn't lose velocity,
if it is 0.0 the object stops when it collides.
low is the lowest cylinder's Z, high is the highest. If they're set to default the cylinder is like infinitely high.*/


PHY_DestroyCylinder(cylinderid)

PHY_SetCylinderWorld(cylinderid, world)

PHY_SetPlayerWorld(playerid, world)

PHY_UseColAndreas(objectid, mode = 1)
/* Sets ColAndreas mode for the object. Modes: 0 none, 1 collisions + z bounds, 2 collisions only, 3 z bounds only */

/* Callbacks */
forward PHY_OnObjectUpdate(objectid);
forward PHY_OnObjectCollideWithObject(object1, object2);
forward PHY_OnObjectCollideWithZBound(objectid, lowhigh); // low bound = 0, high bound = 1
forward PHY_OnObjectCollideWithSAWorld(objectid, Float:cx, Float:cy, Float:cz); // Only with ColAndreas
forward PHY_OnObjectCollideWithWall(objectid, wallid);
forward PHY_OnObjectCollideWithCylinder(objectid, cylinderid);
forward PHY_OnObjectCollideWithPlayer(objectid, playerid);

ColAndreas support:
ColAndreas plugin by Pottus allows to calculate collisions with every object in San Andreas map and to constantly update the low and high Z bound of the object that uses physics.
If you want to use ColAndreas features (PHY_UseColAndreas and PHY_OnObjectCollideWithSAWorld), put "#define COLANDREAS" before including "physics.inc".

Possible usages:
  • Creating a game played with balls, like pool, soccer, volleyball, tennis, etc.
  • Throwing an object with 3D physics.

Videos:

TheArcher 25/06/2013 12:28 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Ohooo, really useful, after I've seen the video, I was thinking about the pool. Really good job! :)

IstuntmanI 25/06/2013 02:11 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Great work !

Dan.. 25/06/2013 02:25 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Wonderful!

I noticed a small bug. In your movie, at 0:31 in the left part of the screen there is a ball colliding with a stationary ball and the second ball starts rotating awkwardly.

EDIT: It looks like there are more balls that are not moving, but rotating.

Sugestion: Make objects collide with players and vehicles.

Littlehelper 25/06/2013 02:27 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Nice!

Black Wolf 25/06/2013 02:29 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
OMG Awesome Awesome Awesome.

PeppeAC 25/06/2013 02:35 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Quote:

Originally Posted by Dan.. (Post 2585029)
Wonderful!

I noticed a small bug. In your movie, at 0:31 in the left part of the screen there is a ball colliding with a stationary ball and the second ball starts rotating awkwardly.

EDIT: It looks like there are more balls that are not moving, but rotating.

Sugestion: Make objects collide with players and vehicles.

Thanks to all.

However this happens due to an inappropriate rotation system. That should use more complex maths (like matrixes), but I don't know how.

Lordzy 25/06/2013 02:39 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Great work.

KingHual 25/06/2013 02:39 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Really good work!

IPrototypeI 25/06/2013 02:45 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
great work thanks for the release

kristo 25/06/2013 02:54 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Waiting for gravity + optional bouncing when falling on the ground. Very nice job :p

[DOG]irinel1996 25/06/2013 03:18 PM

Respuesta: [alpha] Objects Physics - Handle objects collisions and more.
 
This really rocks, nice.

vyper 25/06/2013 03:33 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Great include, good job !

Stylock 25/06/2013 06:16 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Very impressive!

Johnson_boy 25/06/2013 08:35 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Thumbs up, great release

n0minal 25/06/2013 08:41 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Very good job, I love it! +REP

Kar 26/06/2013 12:40 AM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
I'm going haywire! Awesome!

QuaTTrO 26/06/2013 07:33 AM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
WOW ! This is awesome Good Job! :D

PeppeAC 26/06/2013 11:43 AM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
[ame]http://www.youtube.com/watch?v=Kbkm4fXaB7Q[/ame]

Version 0.1 released
Added support to 3D physics.
Some functions edited tu support 3D features.
Some improvements and bug fixes.

I created a Github repository and uploaded there the new version of the include and a demo gamemode.

Black Wolf 26/06/2013 12:14 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Thanks for the demo gamemode.

davve95 26/06/2013 12:36 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Very nice!

Flyfishes 26/06/2013 12:36 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
This is sick! As someone else mentioned, I was thinking about pool and bowling (with fences) when watching this.

PeppeAC 26/06/2013 01:12 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
I added to Github repository a demo gamemode: pool game.
It is very very basic, only to show the potential of the include.
It spawns out of a bar in Las Venturas. Enter it and enjoy.
Use: /movefirst <speed> to move the first ball, /move <id> <speed> <angle> to move another ball.
Obviously you can edit this gamemode to create a proper and fun pool game.

Memoryz 26/06/2013 02:10 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
This is looking good, do you expect to add automatic support for CreatePlayerObject?

PeppeAC 26/06/2013 02:17 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Quote:

Originally Posted by Memoryz (Post 2586570)
This is looking good, do you expect to add automatic support for CreatePlayerObject?

I should, despite it isn't hard to do.

PeppeAC 26/06/2013 02:49 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Pool game demo:
[ame]http://www.youtube.com/watch?v=G_zYQBLI6ow[/ame]

Script available on Github.
As I said it is very basic, only to show the potential of the include. I know that a pool script with commands sucks, in fact I did for my server a better one, more interactive and with more features, similar to the Single player one.

Maran3llo 26/06/2013 03:17 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Very good, Peppe.

Excel™ 26/06/2013 03:47 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Brilliant, thats really useful.

PeppeAC 27/06/2013 11:02 AM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Pool game video was set to private and I didn't know it.
Now I fixed it.
[ame]http://www.youtube.com/watch?v=G_zYQBLI6ow[/ame]

Niko_boy 27/06/2013 11:12 AM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
really amazing ! ,nice work very very nice work!

Admigo 27/06/2013 05:48 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Sweet. Great work.

WillyP 27/06/2013 05:52 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Pretty awesome, can think of a lot of uses for this :)

thefatshizms 27/06/2013 07:11 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Thanks for this! Not only is it a great include but its something for me to try and understand when I have free time ;)

PeppeAC 28/06/2013 02:38 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Is there a Quaternion library in SA-MP? I need it to improve the script.

n0minal 28/06/2013 02:57 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Just waiting a collision with players for start release my sports scripts on my RP gamemode, you're a pussy fucker! I love your Job :)

-Jojo- 28/06/2013 04:20 PM

Re : [alpha] Objects Physics - Handle objects collisions and more.
 
Good Job, really nice work

Kyle 28/06/2013 04:24 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Quote:

Originally Posted by PeppeAC (Post 2589665)
Is there a Quaternion library in SA-MP? I need it to improve the script.

I don't think so, try porting it to a plugin and you can have as many libraries as you want.

Also try JernejL's math plugin?

PeppeAC 28/06/2013 05:24 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Quote:

Originally Posted by KyleSmith (Post 2589817)
I don't think so, try porting it to a plugin and you can have as many libraries as you want.

Also try JernejL's math plugin?


I don't think I need JernejL's plugin. However I ported sa-mp quaternion functions and made a better rolling script, but it doesn't work well.

If someone has experience with quaternions please pm me.

OrMisicL 28/06/2013 06:31 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Very impressive work !

TheChimpJr 28/06/2013 06:37 PM

Re: [alpha] Objects Physics - Handle objects collisions and more.
 
Good Job!


All times are GMT. The time now is 04:16 AM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.