Merge 7999bbafa8 into 27ba7de1e4
This commit is contained in:
commit
94dac41794
|
|
@ -13,10 +13,13 @@
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#define MOVE_VELOCITY 5
|
#define MOVE_VELOCITY 5
|
||||||
#define JUMP_VELOCITY 30
|
#define JUMP_VELOCITY 30
|
||||||
|
|
||||||
|
void* PhysicsThread(void *arg);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
|
|
@ -53,6 +56,10 @@ int main()
|
||||||
// Create pplatform physic object
|
// Create pplatform physic object
|
||||||
PhysicBody platform = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.7f }, 0.0f, (Vector2){ screenWidth*0.25f, 20 });
|
PhysicBody platform = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.7f }, 0.0f, (Vector2){ screenWidth*0.25f, 20 });
|
||||||
|
|
||||||
|
// Create physics thread
|
||||||
|
pthread_t tid;
|
||||||
|
pthread_create(&tid, NULL, &PhysicsThread, NULL);
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -61,10 +68,9 @@ int main()
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
UpdatePhysics(); // Update all created physic objects
|
|
||||||
|
|
||||||
// Check rectangle movement inputs
|
// 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;
|
if (IsKeyDown('A')) rectangle->rigidbody.velocity.x = -MOVE_VELOCITY;
|
||||||
else if (IsKeyDown('D')) rectangle->rigidbody.velocity.x = MOVE_VELOCITY;
|
else if (IsKeyDown('D')) rectangle->rigidbody.velocity.x = MOVE_VELOCITY;
|
||||||
|
|
||||||
|
|
@ -117,6 +123,8 @@ int main()
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
pthread_cancel(tid); // Destroy physics thread
|
||||||
|
|
||||||
ClosePhysics(); // Unitialize physics (including all loaded objects)
|
ClosePhysics(); // Unitialize physics (including all loaded objects)
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
|
@ -124,3 +132,23 @@ int main()
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* PhysicsThread(void *arg)
|
||||||
|
{
|
||||||
|
// Initialize time variables
|
||||||
|
double currentTime = GetTime();
|
||||||
|
double previousTime = currentTime;
|
||||||
|
|
||||||
|
// Physics update loop
|
||||||
|
while (!WindowShouldClose())
|
||||||
|
{
|
||||||
|
currentTime = GetTime();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
@ -13,12 +13,15 @@
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#define FORCE_AMOUNT 5.0f
|
#define FORCE_AMOUNT 5.0f
|
||||||
#define FORCE_RADIUS 150
|
#define FORCE_RADIUS 150
|
||||||
#define LINE_LENGTH 75
|
#define LINE_LENGTH 75
|
||||||
#define TRIANGLE_LENGTH 12
|
#define TRIANGLE_LENGTH 12
|
||||||
|
|
||||||
|
void* PhysicsThread(void *arg);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
|
|
@ -61,6 +64,10 @@ int main()
|
||||||
PhysicBody topWall = CreatePhysicBody((Vector2){ screenWidth/2, -25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
PhysicBody topWall = CreatePhysicBody((Vector2){ screenWidth/2, -25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
||||||
PhysicBody bottomWall = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight + 25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
PhysicBody bottomWall = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight + 25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
||||||
|
|
||||||
|
// Create physics thread
|
||||||
|
pthread_t tid;
|
||||||
|
pthread_create(&tid, NULL, &PhysicsThread, NULL);
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -69,7 +76,6 @@ int main()
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
UpdatePhysics(); // Update all created physic objects
|
|
||||||
|
|
||||||
// Update mouse position value
|
// Update mouse position value
|
||||||
mousePosition = GetMousePosition();
|
mousePosition = GetMousePosition();
|
||||||
|
|
@ -174,6 +180,8 @@ int main()
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
pthread_cancel(tid); // Destroy physics thread
|
||||||
|
|
||||||
ClosePhysics(); // Unitialize physics module
|
ClosePhysics(); // Unitialize physics module
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
|
@ -181,3 +189,23 @@ int main()
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* PhysicsThread(void *arg)
|
||||||
|
{
|
||||||
|
// Initialize time variables
|
||||||
|
double currentTime = GetTime();
|
||||||
|
double previousTime = currentTime;
|
||||||
|
|
||||||
|
// Physics update loop
|
||||||
|
while (!WindowShouldClose())
|
||||||
|
{
|
||||||
|
currentTime = GetTime();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
@ -290,7 +290,6 @@ static void InitDisplay(int width, int height); // Initialize display de
|
||||||
static void InitGraphics(void); // Initialize OpenGL graphics
|
static void InitGraphics(void); // Initialize OpenGL graphics
|
||||||
static void SetupFramebufferSize(int displayWidth, int displayHeight);
|
static void SetupFramebufferSize(int displayWidth, int displayHeight);
|
||||||
static void InitTimer(void); // Initialize timer
|
static void InitTimer(void); // Initialize timer
|
||||||
static double GetTime(void); // Returns time since InitTimer() was run
|
|
||||||
static bool GetKeyStatus(int key); // Returns if a key has been pressed
|
static bool GetKeyStatus(int key); // Returns if a key has been pressed
|
||||||
static bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed
|
static bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed
|
||||||
static void PollInputEvents(void); // Register user events
|
static void PollInputEvents(void); // Register user events
|
||||||
|
|
@ -2039,7 +2038,7 @@ static void InitTimer(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current time measure (in seconds) since InitTimer()
|
// Get current time measure (in seconds) since InitTimer()
|
||||||
static double GetTime(void)
|
double GetTime(void)
|
||||||
{
|
{
|
||||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||||
return glfwGetTime();
|
return glfwGetTime();
|
||||||
|
|
|
||||||
45
src/physac.h
45
src/physac.h
|
|
@ -146,7 +146,7 @@ typedef struct PhysicBodyData {
|
||||||
// Module Functions Declaration
|
// Module Functions Declaration
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
PHYSACDEF void InitPhysics(Vector2 gravity); // Initializes pointers array (just pointers, fixed size)
|
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 UpdatePhysics(double deltaTime); // Update physic objects, calculating physic behaviours and collisions detection
|
||||||
PHYSACDEF void ClosePhysics(); // Unitialize all physic objects and empty the objects pool
|
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
|
PHYSACDEF PhysicBody CreatePhysicBody(Vector2 position, float rotation, Vector2 scale); // Create a new physic body dinamically, initialize it and add to pool
|
||||||
|
|
@ -182,7 +182,7 @@ PHYSACDEF Rectangle TransformToRectangle(Transform transform);
|
||||||
// Defines and Macros
|
// Defines and Macros
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
#define MAX_PHYSIC_BODIES 256 // Maximum available physic bodies slots in bodies pool
|
#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_ACCURACY 0.0001f // Velocity subtract operations round filter (friction)
|
||||||
#define PHYSICS_ERRORPERCENT 0.001f // Collision resolve position fix
|
#define PHYSICS_ERRORPERCENT 0.001f // Collision resolve position fix
|
||||||
|
|
||||||
|
|
@ -218,12 +218,7 @@ PHYSACDEF void InitPhysics(Vector2 gravity)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update physic objects, calculating physic behaviours and collisions detection
|
// Update physic objects, calculating physic behaviours and collisions detection
|
||||||
PHYSACDEF void UpdatePhysics()
|
PHYSACDEF void UpdatePhysics(double deltaTime)
|
||||||
{
|
|
||||||
// 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++)
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < physicBodiesCount; i++)
|
for (int i = 0; i < physicBodiesCount; i++)
|
||||||
{
|
{
|
||||||
|
|
@ -233,39 +228,39 @@ PHYSACDEF void UpdatePhysics()
|
||||||
if (physicBodies[i]->rigidbody.enabled)
|
if (physicBodies[i]->rigidbody.enabled)
|
||||||
{
|
{
|
||||||
// Apply friction to acceleration in X axis
|
// 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;
|
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/PHYSICS_STEPS;
|
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;
|
else physicBodies[i]->rigidbody.acceleration.x = 0.0f;
|
||||||
|
|
||||||
// Apply friction to acceleration in Y axis
|
// 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;
|
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/PHYSICS_STEPS;
|
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;
|
else physicBodies[i]->rigidbody.acceleration.y = 0.0f;
|
||||||
|
|
||||||
// Apply friction to velocity in X axis
|
// 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;
|
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/PHYSICS_STEPS;
|
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;
|
else physicBodies[i]->rigidbody.velocity.x = 0.0f;
|
||||||
|
|
||||||
// Apply friction to velocity in Y axis
|
// 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;
|
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/PHYSICS_STEPS;
|
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;
|
else physicBodies[i]->rigidbody.velocity.y = 0.0f;
|
||||||
|
|
||||||
// Apply gravity to velocity
|
// Apply gravity to velocity
|
||||||
if (physicBodies[i]->rigidbody.applyGravity)
|
if (physicBodies[i]->rigidbody.applyGravity)
|
||||||
{
|
{
|
||||||
physicBodies[i]->rigidbody.velocity.x += gravityForce.x/PHYSICS_STEPS;
|
physicBodies[i]->rigidbody.velocity.x += gravityForce.x*deltaTime;
|
||||||
physicBodies[i]->rigidbody.velocity.y += gravityForce.y/PHYSICS_STEPS;
|
physicBodies[i]->rigidbody.velocity.y += gravityForce.y*deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply acceleration to velocity
|
// Apply acceleration to velocity
|
||||||
physicBodies[i]->rigidbody.velocity.x += physicBodies[i]->rigidbody.acceleration.x/PHYSICS_STEPS;
|
physicBodies[i]->rigidbody.velocity.x += physicBodies[i]->rigidbody.acceleration.x*deltaTime;
|
||||||
physicBodies[i]->rigidbody.velocity.y += physicBodies[i]->rigidbody.acceleration.y/PHYSICS_STEPS;
|
physicBodies[i]->rigidbody.velocity.y += physicBodies[i]->rigidbody.acceleration.y*deltaTime;
|
||||||
|
|
||||||
// Apply velocity to position
|
// Apply velocity to position
|
||||||
physicBodies[i]->transform.position.x += physicBodies[i]->rigidbody.velocity.x/PHYSICS_STEPS;
|
physicBodies[i]->transform.position.x += physicBodies[i]->rigidbody.velocity.x*deltaTime;
|
||||||
physicBodies[i]->transform.position.y -= physicBodies[i]->rigidbody.velocity.y/PHYSICS_STEPS;
|
physicBodies[i]->transform.position.y -= physicBodies[i]->rigidbody.velocity.y*deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update collision detection
|
// Update collision detection
|
||||||
|
|
@ -507,10 +502,7 @@ PHYSACDEF void UpdatePhysics()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update rigidbody grounded state
|
// Update rigidbody grounded state
|
||||||
if (physicBodies[i]->rigidbody.enabled)
|
if (physicBodies[i]->rigidbody.enabled) physicBodies[i]->rigidbody.isGrounded = (contactNormal.y < 0.0f);
|
||||||
{
|
|
||||||
if (contactNormal.y < 0.0f) physicBodies[i]->rigidbody.isGrounded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Calculate collision impulse
|
// 2. Calculate collision impulse
|
||||||
// -------------------------------------------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
@ -596,7 +588,6 @@ PHYSACDEF void UpdatePhysics()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Unitialize all physic objects and empty the objects pool
|
// Unitialize all physic objects and empty the objects pool
|
||||||
PHYSACDEF void ClosePhysics()
|
PHYSACDEF void ClosePhysics()
|
||||||
|
|
|
||||||
|
|
@ -582,6 +582,7 @@ Matrix GetCameraMatrix(Camera camera); // Returns camera tr
|
||||||
void SetTargetFPS(int fps); // Set target FPS (maximum)
|
void SetTargetFPS(int fps); // Set target FPS (maximum)
|
||||||
float GetFPS(void); // Returns current FPS
|
float GetFPS(void); // Returns current FPS
|
||||||
float GetFrameTime(void); // Returns time in seconds for one frame
|
float GetFrameTime(void); // Returns time in seconds for one frame
|
||||||
|
double GetTime(void); // Returns time since InitTimer() was run internally
|
||||||
|
|
||||||
Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value
|
Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value
|
||||||
int GetHexValue(Color color); // Returns hexadecimal value for a Color
|
int GetHexValue(Color color); // Returns hexadecimal value for a Color
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user