Merge pull request #131 from victorfisac/develop
Physac 1.0 module completed
This commit is contained in:
commit
d5d1305bc0
5 changed files with 493 additions and 396 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -73,3 +73,4 @@ src/libraylib.bc
|
|||
!src/external/glfw3/lib/win32/glfw3.dll
|
||||
!src/external/openal_soft/lib/win32/OpenAL32.dll
|
||||
!src/external/OculusSDK/LibOVR/LibOVRRT32_1.dll
|
||||
!src/external/pthread/pthreadGC2.dll
|
|
@ -5,6 +5,10 @@
|
|||
* This example has been created using raylib 1.5 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
*
|
||||
* Compile example using:
|
||||
* cmd /c IF NOT EXIST pthreadGC2.dll copy C:\raylib\raylib\src\external\pthread\pthreadGC2.dll $(CURRENT_DIRECTORY) /Y
|
||||
*
|
||||
* Copyright (c) 2016 Victor Fisac and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
@ -25,7 +29,6 @@ int main()
|
|||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [physac] example - basic rigidbody");
|
||||
|
||||
InitPhysics((Vector2){ 0.0f, -9.81f/2 }); // Initialize physics module
|
||||
|
||||
// Debug variables
|
||||
|
@ -61,10 +64,8 @@ int main()
|
|||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdatePhysics(); // Update all created physic objects
|
||||
|
||||
// Check rectangle movement inputs
|
||||
if (IsKeyDown('W') && rectangle->rigidbody.isGrounded) rectangle->rigidbody.velocity.y = JUMP_VELOCITY;
|
||||
if (IsKeyPressed('W')) rectangle->rigidbody.velocity.y = JUMP_VELOCITY;
|
||||
if (IsKeyDown('A')) rectangle->rigidbody.velocity.x = -MOVE_VELOCITY;
|
||||
else if (IsKeyDown('D')) rectangle->rigidbody.velocity.x = MOVE_VELOCITY;
|
||||
|
||||
|
@ -111,6 +112,8 @@ int main()
|
|||
// Draw help message
|
||||
DrawText("Use WASD to move rectangle and ARROWS to move square", screenWidth/2 - MeasureText("Use WASD to move rectangle and ARROWS to move square", 20)/2, screenHeight*0.075f, 20, LIGHTGRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
@ -118,7 +121,6 @@ int main()
|
|||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
ClosePhysics(); // Unitialize physics (including all loaded objects)
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -5,6 +5,11 @@
|
|||
* This example has been created using raylib 1.5 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* NOTE: This example requires raylib module [rlgl]
|
||||
*
|
||||
* Compile example using:
|
||||
* cmd /c IF NOT EXIST pthreadGC2.dll copy C:\raylib\raylib\src\external\pthread\pthreadGC2.dll $(CURRENT_DIRECTORY) /Y
|
||||
*
|
||||
* Copyright (c) 2016 Victor Fisac and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
@ -27,7 +32,6 @@ int main()
|
|||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [physac] example - forces");
|
||||
|
||||
InitPhysics((Vector2){ 0.0f, -9.81f/2 }); // Initialize physics module
|
||||
|
||||
// Global variables
|
||||
|
@ -69,7 +73,6 @@ int main()
|
|||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdatePhysics(); // Update all created physic objects
|
||||
|
||||
// Update mouse position value
|
||||
mousePosition = GetMousePosition();
|
||||
|
@ -168,6 +171,8 @@ int main()
|
|||
DrawText("Use LEFT MOUSE BUTTON to apply a force", screenWidth/2 - MeasureText("Use LEFT MOUSE BUTTON to apply a force", 20)/2, screenHeight*0.075f, 20, LIGHTGRAY);
|
||||
DrawText("Use R to reset objects position", screenWidth/2 - MeasureText("Use R to reset objects position", 20)/2, screenHeight*0.875f, 20, GRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
@ -175,7 +180,6 @@ int main()
|
|||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
ClosePhysics(); // Unitialize physics module
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
|
BIN
src/external/pthread/pthreadGC2.dll
vendored
Normal file
BIN
src/external/pthread/pthreadGC2.dll
vendored
Normal file
Binary file not shown.
432
src/physac.h
432
src/physac.h
|
@ -15,6 +15,10 @@
|
|||
* The generated implementation will stay private inside implementation file and all
|
||||
* internal symbols and functions will only be visible inside that file.
|
||||
*
|
||||
* #define PHYSAC_NO_THREADS
|
||||
* The generated implementation won't include pthread library and user must create a secondary thread to call PhysicsThread().
|
||||
* It is so important that the thread where PhysicsThread() is called must not have v-sync or any other CPU limitation.
|
||||
*
|
||||
* #define PHYSAC_STANDALONE
|
||||
* Avoid raylib.h header inclusion in this file. Data types defined on raylib are defined
|
||||
* internally in the library and input management and drawing functions must be provided by
|
||||
|
@ -27,12 +31,16 @@
|
|||
*
|
||||
* LIMITATIONS:
|
||||
*
|
||||
* // TODO.
|
||||
* - There is a limit of 256 physic objects.
|
||||
* - Physics behaviour can be unexpected using bounciness or friction values out of 0.0f - 1.0f range.
|
||||
* - The module is limited to 2D axis oriented physics.
|
||||
* - Physics colliders must be rectangle or circle shapes (there is not a custom polygon collider type).
|
||||
*
|
||||
* VERSIONS:
|
||||
*
|
||||
* 1.0 (09-Jun-2016) Module names review and converted to header-only.
|
||||
* 0.9 (23-Mar-2016) Complete module redesign, steps-based for better physics resolution.
|
||||
* 1.0 (14-Jun-2016) New module defines and fixed some delta time calculation bugs.
|
||||
* 0.9 (09-Jun-2016) Module names review and converted to header-only.
|
||||
* 0.8 (23-Mar-2016) Complete module redesign, steps-based for better physics resolution.
|
||||
* 0.3 (13-Feb-2016) Reviewed to add PhysicObjects pool.
|
||||
* 0.2 (03-Jan-2016) Improved physics calculations.
|
||||
* 0.1 (30-Dec-2015) Initial release.
|
||||
|
@ -146,7 +154,7 @@ typedef struct PhysicBodyData {
|
|||
// Module Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
PHYSACDEF void InitPhysics(Vector2 gravity); // Initializes pointers array (just pointers, fixed size)
|
||||
PHYSACDEF void UpdatePhysics(); // Update physic objects, calculating physic behaviours and collisions detection
|
||||
PHYSACDEF void* PhysicsThread(void *arg); // Physics calculations thread function
|
||||
PHYSACDEF void ClosePhysics(); // Unitialize all physic objects and empty the objects pool
|
||||
|
||||
PHYSACDEF PhysicBody CreatePhysicBody(Vector2 position, float rotation, Vector2 scale); // Create a new physic body dinamically, initialize it and add to pool
|
||||
|
@ -177,12 +185,26 @@ PHYSACDEF Rectangle TransformToRectangle(Transform transform);
|
|||
#endif
|
||||
|
||||
#include <math.h> // Required for: cos(), sin(), abs(), fminf()
|
||||
#include <stdint.h> // Required for typedef unsigned long long int uint64_t, used by hi-res timer
|
||||
|
||||
#ifndef PHYSAC_NO_THREADS
|
||||
#include <pthread.h> // Required for: pthread_create()
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
// Functions required to query time on Windows
|
||||
int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
|
||||
int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency);
|
||||
#elif defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
||||
#include <sys/time.h> // Required for: timespec
|
||||
#include <time.h> // Required for: clock_gettime()
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#define MAX_PHYSIC_BODIES 256 // Maximum available physic bodies slots in bodies pool
|
||||
#define PHYSICS_STEPS 64 // Physics update steps per frame for improved collision-detection
|
||||
#define PHYSICS_TIMESTEP 0.016666 // Physics fixed time step (1/fps)
|
||||
#define PHYSICS_ACCURACY 0.0001f // Velocity subtract operations round filter (friction)
|
||||
#define PHYSICS_ERRORPERCENT 0.001f // Collision resolve position fix
|
||||
|
||||
|
@ -195,6 +217,9 @@ PHYSACDEF Rectangle TransformToRectangle(Transform transform);
|
|||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
static bool physicsThreadEnabled = false; // Physics calculations thread exit control
|
||||
static uint64_t baseTime; // Base time measure for hi-res timer
|
||||
static double currentTime, previousTime; // Used to track timmings
|
||||
static PhysicBody physicBodies[MAX_PHYSIC_BODIES]; // Physic bodies pool
|
||||
static int physicBodiesCount; // Counts current enabled physic bodies
|
||||
static Vector2 gravityForce; // Gravity force
|
||||
|
@ -202,6 +227,9 @@ static Vector2 gravityForce; // Gravity f
|
|||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
static void UpdatePhysics(double deltaTime); // Update physic objects, calculating physic behaviours and collisions detection
|
||||
static void InitTimer(void); // Initialize hi-resolution timer
|
||||
static double GetCurrentTime(void); // Time measure returned are microseconds
|
||||
static float Vector2DotProduct(Vector2 v1, Vector2 v2); // Returns the dot product of two Vector2
|
||||
static float Vector2Length(Vector2 v); // Returns the length of a Vector2
|
||||
|
||||
|
@ -215,15 +243,222 @@ PHYSACDEF void InitPhysics(Vector2 gravity)
|
|||
// Initialize physics variables
|
||||
physicBodiesCount = 0;
|
||||
gravityForce = gravity;
|
||||
|
||||
#ifndef PHYSAC_NO_THREADS // NOTE: if defined, user will need to create a thread for PhysicsThread function manually
|
||||
// Create physics thread
|
||||
pthread_t tid;
|
||||
pthread_create(&tid, NULL, &PhysicsThread, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Unitialize all physic objects and empty the objects pool
|
||||
PHYSACDEF void ClosePhysics()
|
||||
{
|
||||
// Exit physics thread loop
|
||||
physicsThreadEnabled = false;
|
||||
|
||||
// Free all dynamic memory allocations
|
||||
for (int i = 0; i < physicBodiesCount; i++) PHYSAC_FREE(physicBodies[i]);
|
||||
|
||||
// Reset enabled physic objects count
|
||||
physicBodiesCount = 0;
|
||||
}
|
||||
|
||||
// Create a new physic body dinamically, initialize it and add to pool
|
||||
PHYSACDEF PhysicBody CreatePhysicBody(Vector2 position, float rotation, Vector2 scale)
|
||||
{
|
||||
// Allocate dynamic memory
|
||||
PhysicBody obj = (PhysicBody)PHYSAC_MALLOC(sizeof(PhysicBodyData));
|
||||
|
||||
// Initialize physic body values with generic values
|
||||
obj->id = physicBodiesCount;
|
||||
obj->enabled = true;
|
||||
|
||||
obj->transform = (Transform){ (Vector2){ position.x - scale.x/2, position.y - scale.y/2 }, rotation, scale };
|
||||
|
||||
obj->rigidbody.enabled = false;
|
||||
obj->rigidbody.mass = 1.0f;
|
||||
obj->rigidbody.acceleration = (Vector2){ 0.0f, 0.0f };
|
||||
obj->rigidbody.velocity = (Vector2){ 0.0f, 0.0f };
|
||||
obj->rigidbody.applyGravity = false;
|
||||
obj->rigidbody.isGrounded = false;
|
||||
obj->rigidbody.friction = 0.0f;
|
||||
obj->rigidbody.bounciness = 0.0f;
|
||||
|
||||
obj->collider.enabled = true;
|
||||
obj->collider.type = COLLIDER_RECTANGLE;
|
||||
obj->collider.bounds = TransformToRectangle(obj->transform);
|
||||
obj->collider.radius = 0.0f;
|
||||
|
||||
// Add new physic body to the pointers array
|
||||
physicBodies[physicBodiesCount] = obj;
|
||||
|
||||
// Increase enabled physic bodies count
|
||||
physicBodiesCount++;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
// Destroy a specific physic body and take it out of the list
|
||||
PHYSACDEF void DestroyPhysicBody(PhysicBody pbody)
|
||||
{
|
||||
// Free dynamic memory allocation
|
||||
PHYSAC_FREE(physicBodies[pbody->id]);
|
||||
|
||||
// Remove *obj from the pointers array
|
||||
for (int i = pbody->id; i < physicBodiesCount; i++)
|
||||
{
|
||||
// Resort all the following pointers of the array
|
||||
if ((i + 1) < physicBodiesCount)
|
||||
{
|
||||
physicBodies[i] = physicBodies[i + 1];
|
||||
physicBodies[i]->id = physicBodies[i + 1]->id;
|
||||
}
|
||||
else PHYSAC_FREE(physicBodies[i]);
|
||||
}
|
||||
|
||||
// Decrease enabled physic bodies count
|
||||
physicBodiesCount--;
|
||||
}
|
||||
|
||||
// Apply directional force to a physic body
|
||||
PHYSACDEF void ApplyForce(PhysicBody pbody, Vector2 force)
|
||||
{
|
||||
if (pbody->rigidbody.enabled)
|
||||
{
|
||||
pbody->rigidbody.velocity.x += force.x/pbody->rigidbody.mass;
|
||||
pbody->rigidbody.velocity.y += force.y/pbody->rigidbody.mass;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply radial force to all physic objects in range
|
||||
PHYSACDEF void ApplyForceAtPosition(Vector2 position, float force, float radius)
|
||||
{
|
||||
for (int i = 0; i < physicBodiesCount; i++)
|
||||
{
|
||||
if (physicBodies[i]->rigidbody.enabled)
|
||||
{
|
||||
// Calculate direction and distance between force and physic body position
|
||||
Vector2 distance = (Vector2){ physicBodies[i]->transform.position.x - position.x, physicBodies[i]->transform.position.y - position.y };
|
||||
|
||||
if (physicBodies[i]->collider.type == COLLIDER_RECTANGLE)
|
||||
{
|
||||
distance.x += physicBodies[i]->transform.scale.x/2;
|
||||
distance.y += physicBodies[i]->transform.scale.y/2;
|
||||
}
|
||||
|
||||
float distanceLength = Vector2Length(distance);
|
||||
|
||||
// Check if physic body is in force range
|
||||
if (distanceLength <= radius)
|
||||
{
|
||||
// Normalize force direction
|
||||
distance.x /= distanceLength;
|
||||
distance.y /= -distanceLength;
|
||||
|
||||
// Calculate final force
|
||||
Vector2 finalForce = { distance.x*force, distance.y*force };
|
||||
|
||||
// Apply force to the physic body
|
||||
ApplyForce(physicBodies[i], finalForce);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert Transform data type to Rectangle (position and scale)
|
||||
PHYSACDEF Rectangle TransformToRectangle(Transform transform)
|
||||
{
|
||||
return (Rectangle){transform.position.x, transform.position.y, transform.scale.x, transform.scale.y};
|
||||
}
|
||||
|
||||
// Physics calculations thread function
|
||||
PHYSACDEF void* PhysicsThread(void *arg)
|
||||
{
|
||||
// Initialize thread loop state
|
||||
physicsThreadEnabled = true;
|
||||
|
||||
// Initialize hi-resolution timer
|
||||
InitTimer();
|
||||
|
||||
// Physics update loop
|
||||
while (physicsThreadEnabled)
|
||||
{
|
||||
currentTime = GetCurrentTime();
|
||||
double deltaTime = (double)(currentTime - previousTime);
|
||||
previousTime = currentTime;
|
||||
|
||||
// Delta time value needs to be inverse multiplied by physics time step value (1/target fps)
|
||||
UpdatePhysics(deltaTime/PHYSICS_TIMESTEP);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
// Initialize hi-resolution timer
|
||||
static void InitTimer(void)
|
||||
{
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
||||
struct timespec now;
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) // Success
|
||||
{
|
||||
baseTime = (uint64_t)now.tv_sec*1000000000LLU + (uint64_t)now.tv_nsec;
|
||||
}
|
||||
#endif
|
||||
|
||||
previousTime = GetCurrentTime(); // Get time as double
|
||||
}
|
||||
|
||||
// Time measure returned are microseconds
|
||||
static double GetCurrentTime(void)
|
||||
{
|
||||
double time;
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
unsigned long long int clockFrequency, currentTime;
|
||||
|
||||
QueryPerformanceFrequency(&clockFrequency);
|
||||
QueryPerformanceCounter(¤tTime);
|
||||
|
||||
time = (double)((double)currentTime/(double)clockFrequency);
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
uint64_t temp = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec;
|
||||
|
||||
time = (double)(temp - baseTime)*1e-9;
|
||||
#endif
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
// Returns the dot product of two Vector2
|
||||
static float Vector2DotProduct(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
float result;
|
||||
|
||||
result = v1.x*v2.x + v1.y*v2.y;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static float Vector2Length(Vector2 v)
|
||||
{
|
||||
float result;
|
||||
|
||||
result = sqrt(v.x*v.x + v.y*v.y);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Update physic objects, calculating physic behaviours and collisions detection
|
||||
PHYSACDEF void UpdatePhysics()
|
||||
{
|
||||
// Reset all physic objects is grounded state
|
||||
for (int i = 0; i < physicBodiesCount; i++) physicBodies[i]->rigidbody.isGrounded = false;
|
||||
|
||||
for (int steps = 0; steps < PHYSICS_STEPS; steps++)
|
||||
static void UpdatePhysics(double deltaTime)
|
||||
{
|
||||
for (int i = 0; i < physicBodiesCount; i++)
|
||||
{
|
||||
|
@ -233,39 +468,39 @@ PHYSACDEF void UpdatePhysics()
|
|||
if (physicBodies[i]->rigidbody.enabled)
|
||||
{
|
||||
// Apply friction to acceleration in X axis
|
||||
if (physicBodies[i]->rigidbody.acceleration.x > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.x -= physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
else if (physicBodies[i]->rigidbody.acceleration.x < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.x += physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
if (physicBodies[i]->rigidbody.acceleration.x > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.x -= physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else if (physicBodies[i]->rigidbody.acceleration.x < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.x += physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else physicBodies[i]->rigidbody.acceleration.x = 0.0f;
|
||||
|
||||
// Apply friction to acceleration in Y axis
|
||||
if (physicBodies[i]->rigidbody.acceleration.y > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.y -= physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
else if (physicBodies[i]->rigidbody.acceleration.y < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.y += physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
if (physicBodies[i]->rigidbody.acceleration.y > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.y -= physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else if (physicBodies[i]->rigidbody.acceleration.y < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.acceleration.y += physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else physicBodies[i]->rigidbody.acceleration.y = 0.0f;
|
||||
|
||||
// Apply friction to velocity in X axis
|
||||
if (physicBodies[i]->rigidbody.velocity.x > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.x -= physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
else if (physicBodies[i]->rigidbody.velocity.x < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.x += physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
if (physicBodies[i]->rigidbody.velocity.x > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.x -= physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else if (physicBodies[i]->rigidbody.velocity.x < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.x += physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else physicBodies[i]->rigidbody.velocity.x = 0.0f;
|
||||
|
||||
// Apply friction to velocity in Y axis
|
||||
if (physicBodies[i]->rigidbody.velocity.y > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.y -= physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
else if (physicBodies[i]->rigidbody.velocity.y < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.y += physicBodies[i]->rigidbody.friction/PHYSICS_STEPS;
|
||||
if (physicBodies[i]->rigidbody.velocity.y > PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.y -= physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else if (physicBodies[i]->rigidbody.velocity.y < PHYSICS_ACCURACY) physicBodies[i]->rigidbody.velocity.y += physicBodies[i]->rigidbody.friction*deltaTime;
|
||||
else physicBodies[i]->rigidbody.velocity.y = 0.0f;
|
||||
|
||||
// Apply gravity to velocity
|
||||
if (physicBodies[i]->rigidbody.applyGravity)
|
||||
{
|
||||
physicBodies[i]->rigidbody.velocity.x += gravityForce.x/PHYSICS_STEPS;
|
||||
physicBodies[i]->rigidbody.velocity.y += gravityForce.y/PHYSICS_STEPS;
|
||||
physicBodies[i]->rigidbody.velocity.x += gravityForce.x*deltaTime;
|
||||
physicBodies[i]->rigidbody.velocity.y += gravityForce.y*deltaTime;
|
||||
}
|
||||
|
||||
// Apply acceleration to velocity
|
||||
physicBodies[i]->rigidbody.velocity.x += physicBodies[i]->rigidbody.acceleration.x/PHYSICS_STEPS;
|
||||
physicBodies[i]->rigidbody.velocity.y += physicBodies[i]->rigidbody.acceleration.y/PHYSICS_STEPS;
|
||||
physicBodies[i]->rigidbody.velocity.x += physicBodies[i]->rigidbody.acceleration.x*deltaTime;
|
||||
physicBodies[i]->rigidbody.velocity.y += physicBodies[i]->rigidbody.acceleration.y*deltaTime;
|
||||
|
||||
// Apply velocity to position
|
||||
physicBodies[i]->transform.position.x += physicBodies[i]->rigidbody.velocity.x/PHYSICS_STEPS;
|
||||
physicBodies[i]->transform.position.y -= physicBodies[i]->rigidbody.velocity.y/PHYSICS_STEPS;
|
||||
physicBodies[i]->transform.position.x += physicBodies[i]->rigidbody.velocity.x*deltaTime;
|
||||
physicBodies[i]->transform.position.y -= physicBodies[i]->rigidbody.velocity.y*deltaTime;
|
||||
}
|
||||
|
||||
// Update collision detection
|
||||
|
@ -507,10 +742,7 @@ PHYSACDEF void UpdatePhysics()
|
|||
}
|
||||
|
||||
// Update rigidbody grounded state
|
||||
if (physicBodies[i]->rigidbody.enabled)
|
||||
{
|
||||
if (contactNormal.y < 0.0f) physicBodies[i]->rigidbody.isGrounded = true;
|
||||
}
|
||||
if (physicBodies[i]->rigidbody.enabled) physicBodies[i]->rigidbody.isGrounded = (contactNormal.y < 0.0f);
|
||||
|
||||
// 2. Calculate collision impulse
|
||||
// -------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
@ -596,147 +828,5 @@ PHYSACDEF void UpdatePhysics()
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Unitialize all physic objects and empty the objects pool
|
||||
PHYSACDEF void ClosePhysics()
|
||||
{
|
||||
// Free all dynamic memory allocations
|
||||
for (int i = 0; i < physicBodiesCount; i++) PHYSAC_FREE(physicBodies[i]);
|
||||
|
||||
// Reset enabled physic objects count
|
||||
physicBodiesCount = 0;
|
||||
}
|
||||
|
||||
// Create a new physic body dinamically, initialize it and add to pool
|
||||
PHYSACDEF PhysicBody CreatePhysicBody(Vector2 position, float rotation, Vector2 scale)
|
||||
{
|
||||
// Allocate dynamic memory
|
||||
PhysicBody obj = (PhysicBody)PHYSAC_MALLOC(sizeof(PhysicBodyData));
|
||||
|
||||
// Initialize physic body values with generic values
|
||||
obj->id = physicBodiesCount;
|
||||
obj->enabled = true;
|
||||
|
||||
obj->transform = (Transform){ (Vector2){ position.x - scale.x/2, position.y - scale.y/2 }, rotation, scale };
|
||||
|
||||
obj->rigidbody.enabled = false;
|
||||
obj->rigidbody.mass = 1.0f;
|
||||
obj->rigidbody.acceleration = (Vector2){ 0.0f, 0.0f };
|
||||
obj->rigidbody.velocity = (Vector2){ 0.0f, 0.0f };
|
||||
obj->rigidbody.applyGravity = false;
|
||||
obj->rigidbody.isGrounded = false;
|
||||
obj->rigidbody.friction = 0.0f;
|
||||
obj->rigidbody.bounciness = 0.0f;
|
||||
|
||||
obj->collider.enabled = true;
|
||||
obj->collider.type = COLLIDER_RECTANGLE;
|
||||
obj->collider.bounds = TransformToRectangle(obj->transform);
|
||||
obj->collider.radius = 0.0f;
|
||||
|
||||
// Add new physic body to the pointers array
|
||||
physicBodies[physicBodiesCount] = obj;
|
||||
|
||||
// Increase enabled physic bodies count
|
||||
physicBodiesCount++;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
// Destroy a specific physic body and take it out of the list
|
||||
PHYSACDEF void DestroyPhysicBody(PhysicBody pbody)
|
||||
{
|
||||
// Free dynamic memory allocation
|
||||
PHYSAC_FREE(physicBodies[pbody->id]);
|
||||
|
||||
// Remove *obj from the pointers array
|
||||
for (int i = pbody->id; i < physicBodiesCount; i++)
|
||||
{
|
||||
// Resort all the following pointers of the array
|
||||
if ((i + 1) < physicBodiesCount)
|
||||
{
|
||||
physicBodies[i] = physicBodies[i + 1];
|
||||
physicBodies[i]->id = physicBodies[i + 1]->id;
|
||||
}
|
||||
else PHYSAC_FREE(physicBodies[i]);
|
||||
}
|
||||
|
||||
// Decrease enabled physic bodies count
|
||||
physicBodiesCount--;
|
||||
}
|
||||
|
||||
// Apply directional force to a physic body
|
||||
PHYSACDEF void ApplyForce(PhysicBody pbody, Vector2 force)
|
||||
{
|
||||
if (pbody->rigidbody.enabled)
|
||||
{
|
||||
pbody->rigidbody.velocity.x += force.x/pbody->rigidbody.mass;
|
||||
pbody->rigidbody.velocity.y += force.y/pbody->rigidbody.mass;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply radial force to all physic objects in range
|
||||
PHYSACDEF void ApplyForceAtPosition(Vector2 position, float force, float radius)
|
||||
{
|
||||
for (int i = 0; i < physicBodiesCount; i++)
|
||||
{
|
||||
if (physicBodies[i]->rigidbody.enabled)
|
||||
{
|
||||
// Calculate direction and distance between force and physic body position
|
||||
Vector2 distance = (Vector2){ physicBodies[i]->transform.position.x - position.x, physicBodies[i]->transform.position.y - position.y };
|
||||
|
||||
if (physicBodies[i]->collider.type == COLLIDER_RECTANGLE)
|
||||
{
|
||||
distance.x += physicBodies[i]->transform.scale.x/2;
|
||||
distance.y += physicBodies[i]->transform.scale.y/2;
|
||||
}
|
||||
|
||||
float distanceLength = Vector2Length(distance);
|
||||
|
||||
// Check if physic body is in force range
|
||||
if (distanceLength <= radius)
|
||||
{
|
||||
// Normalize force direction
|
||||
distance.x /= distanceLength;
|
||||
distance.y /= -distanceLength;
|
||||
|
||||
// Calculate final force
|
||||
Vector2 finalForce = { distance.x*force, distance.y*force };
|
||||
|
||||
// Apply force to the physic body
|
||||
ApplyForce(physicBodies[i], finalForce);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert Transform data type to Rectangle (position and scale)
|
||||
PHYSACDEF Rectangle TransformToRectangle(Transform transform)
|
||||
{
|
||||
return (Rectangle){transform.position.x, transform.position.y, transform.scale.x, transform.scale.y};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Returns the dot product of two Vector2
|
||||
static float Vector2DotProduct(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
float result;
|
||||
|
||||
result = v1.x*v2.x + v1.y*v2.y;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static float Vector2Length(Vector2 v)
|
||||
{
|
||||
float result;
|
||||
|
||||
result = sqrt(v.x*v.x + v.y*v.y);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // PHYSAC_IMPLEMENTATION
|
Loading…
Add table
Add a link
Reference in a new issue