aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorvictorfisac <victorfisac@gmail.com>2016-06-11 19:11:30 +0200
committervictorfisac <victorfisac@gmail.com>2016-06-11 19:11:30 +0200
commit4c43a407888d516b38191b5df76e373dae6ec58e (patch)
tree61163ae995a3b49443417cb2ddb0579bb54b1d9c /examples
parentc10c49e44f31ddac4b544ddf8c973774afd288c6 (diff)
downloadraylib-4c43a407888d516b38191b5df76e373dae6ec58e.tar.gz
raylib-4c43a407888d516b38191b5df76e373dae6ec58e.zip
Update physac examples with fixed timestep method
Diffstat (limited to 'examples')
-rw-r--r--examples/physics_basic_rigidbody.c32
-rw-r--r--examples/physics_forces.c30
2 files changed, 59 insertions, 3 deletions
diff --git a/examples/physics_basic_rigidbody.c b/examples/physics_basic_rigidbody.c
index 8870c55b..24570426 100644
--- a/examples/physics_basic_rigidbody.c
+++ b/examples/physics_basic_rigidbody.c
@@ -13,10 +13,13 @@
#define PHYSAC_IMPLEMENTATION
#include "physac.h"
+#include <pthread.h>
#define MOVE_VELOCITY 5
#define JUMP_VELOCITY 30
+void* PhysicsThread(void *arg);
+
int main()
{
// Initialization
@@ -53,6 +56,10 @@ int main()
// Create pplatform physic object
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);
//--------------------------------------------------------------------------------------
@@ -61,10 +68,9 @@ 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;
@@ -117,10 +123,32 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
+ pthread_cancel(tid); // Destroy physics thread
+
ClosePhysics(); // Unitialize physics (including all loaded objects)
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
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;
} \ No newline at end of file
diff --git a/examples/physics_forces.c b/examples/physics_forces.c
index 3e90a21d..397c2331 100644
--- a/examples/physics_forces.c
+++ b/examples/physics_forces.c
@@ -13,12 +13,15 @@
#define PHYSAC_IMPLEMENTATION
#include "physac.h"
+#include <pthread.h>
#define FORCE_AMOUNT 5.0f
#define FORCE_RADIUS 150
#define LINE_LENGTH 75
#define TRIANGLE_LENGTH 12
+void* PhysicsThread(void *arg);
+
int main()
{
// Initialization
@@ -61,6 +64,10 @@ int main()
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 });
+ // Create physics thread
+ pthread_t tid;
+ pthread_create(&tid, NULL, &PhysicsThread, NULL);
+
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@@ -69,7 +76,6 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
- UpdatePhysics(); // Update all created physic objects
// Update mouse position value
mousePosition = GetMousePosition();
@@ -174,10 +180,32 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
+ pthread_cancel(tid); // Destroy physics thread
+
ClosePhysics(); // Unitialize physics module
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
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;
} \ No newline at end of file