Reviewed physics module

A deeper revision required, not clear enough for the user
Key: Create a PhysicObjects pool
This commit is contained in:
raysan5 2016-02-13 17:09:53 +01:00
parent 94c92a58a1
commit ed19064405
5 changed files with 64 additions and 98 deletions

View file

@ -30,13 +30,12 @@
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h> // Required for: malloc(), free()
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#define MAX_ELEMENTS 1024 // Stored rigidbodies and colliders array length
#define DECIMAL_FIX 0.26f // Decimal margin for collision checks (avoid rigidbodies shake)
#define DECIMAL_FIX 0.26f // Decimal margin for collision checks (avoid rigidbodies shake)
//----------------------------------------------------------------------------------
// Types and Structures Definition
@ -46,10 +45,13 @@
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static Physics physics;
static Collider colliders[MAX_ELEMENTS];
static Rigidbody rigidbodies[MAX_ELEMENTS];
static bool collisionChecker = false;
static Collider *colliders; // Colliders array, dynamically allocated at runtime
static Rigidbody *rigidbodies; // Rigitbody array, dynamically allocated at runtime
static bool collisionChecker;
static int maxElements; // Max physic elements to compute
static bool enabled; // Physics enabled? (true by default)
static Vector2 gravity; // Gravity value used for physic calculations
//----------------------------------------------------------------------------------
// Module specific Functions Declarations
@ -61,30 +63,39 @@ static void Vector2Normalize(Vector2 *vector);
//----------------------------------------------------------------------------------
// Module Functions Definitions
//----------------------------------------------------------------------------------
void InitPhysics(void)
{
for (int i = 0; i < MAX_ELEMENTS; i++)
void InitPhysics(int maxPhysicElements)
{
maxElements = maxPhysicElements;
colliders = (Collider *)malloc(maxElements*sizeof(Collider));
rigidbodies = (Rigidbody *)malloc(maxElements*sizeof(Rigidbody));
for (int i = 0; i < maxElements; i++)
{
colliders[i].enabled = false;
colliders[i].bounds = (Rectangle){ 0, 0, 0, 0 };
colliders[i].radius = 0;
rigidbodies[i].enabled = false;
rigidbodies[i].mass = 0.0f;
rigidbodies[i].velocity = (Vector2){0, 0};
rigidbodies[i].acceleration = (Vector2){0, 0};
rigidbodies[i].velocity = (Vector2){ 0.0f, 0.0f };
rigidbodies[i].acceleration = (Vector2){ 0.0f, 0.0f };
rigidbodies[i].isGrounded = false;
rigidbodies[i].isContact = false;
rigidbodies[i].friction = 0.0f;
colliders[i].enabled = false;
colliders[i].bounds = (Rectangle){0, 0, 0, 0};
colliders[i].radius = 0;
}
collisionChecker = false;
enabled = true;
// NOTE: To get better results, gravity needs to be 1:10 from original parameter
gravity = (Vector2){ 0.0f, -9.81f/10.0f }; // By default, standard gravity
}
void SetPhysics(Physics settings)
void UnloadPhysics()
{
physics = settings;
// To get good results, gravity needs to be 1:10 from original parameter
physics.gravity = (Vector2){physics.gravity.x / 10, physics.gravity.y / 10};
free(colliders);
free(rigidbodies);
}
void AddCollider(int index, Collider collider)
@ -159,8 +170,8 @@ void ApplyPhysics(int index, Vector2 *position)
}
// Apply gravity
rigidbodies[index].velocity.y += physics.gravity.y;
rigidbodies[index].velocity.x += physics.gravity.x;
rigidbodies[index].velocity.y += gravity.y;
rigidbodies[index].velocity.x += gravity.x;
// Apply acceleration
rigidbodies[index].velocity.y += rigidbodies[index].acceleration.y;
@ -177,7 +188,7 @@ void ApplyPhysics(int index, Vector2 *position)
// Check collision with other colliders
collisionChecker = false;
rigidbodies[index].isContact = false;
for (int j = 0; j < MAX_ELEMENTS; j++)
for (int j = 0; j < maxElements; j++)
{
if (index != j)
{
@ -269,7 +280,7 @@ void AddRigidbodyForce(int index, Vector2 force)
void AddForceAtPosition(Vector2 position, float intensity, float radius)
{
for(int i = 0; i < MAX_ELEMENTS; i++)
for(int i = 0; i < maxElements; i++)
{
if(rigidbodies[i].enabled)
{

View file

@ -35,13 +35,6 @@
// Collider types
typedef enum { COLLIDER_CIRCLE, COLLIDER_RECTANGLE, COLLIDER_CAPSULE } ColliderType;
// Physics struct
typedef struct Physics {
bool enabled;
bool debug; // Should be used by programmer for testing purposes
Vector2 gravity;
} Physics;
// Transform struct
typedef struct Transform {
Vector2 position;
@ -77,8 +70,8 @@ extern "C" { // Prevents name mangling of functions
//----------------------------------------------------------------------------------
// Module Functions Declarations
//----------------------------------------------------------------------------------
void InitPhysics(void); // Initialize all internal physics values
void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings
void InitPhysics(int maxPhysicElements); // Initialize all internal physics values
void UnloadPhysics(); // Unload physic elements arrays
void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot
void AddCollider(int index, Collider collider); // Initialize a new Collider with parameters to internal index slot

View file

@ -466,13 +466,6 @@ typedef enum { CAMERA_CUSTOM = 0, CAMERA_FREE, CAMERA_ORBITAL, CAMERA_FIRST_PERS
// Collider types
typedef enum { COLLIDER_CIRCLE, COLLIDER_RECTANGLE, COLLIDER_CAPSULE } ColliderType;
// Physics struct
typedef struct Physics {
bool enabled;
bool debug; // Should be used by programmer for testing purposes
Vector2 gravity;
} Physics;
// Transform struct
typedef struct Transform {
Vector2 position;
@ -808,10 +801,10 @@ void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textur
void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied)
//----------------------------------------------------------------------------------
// Physics System Functions (engine-module: physics)
// Physics System Functions (engine-module: physac)
//----------------------------------------------------------------------------------
void InitPhysics(void); // Initialize all internal physics values
void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings
void InitPhysics(int maxPhysicElements); // Initialize all internal physics values
void UnloadPhysics(); // Unload physic elements arrays
void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot
void AddCollider(int index, Collider collider); // Initialize a new Collider with parameters to internal index slot