Skip to content

Commit

Permalink
Updated to latest raylib version of physac
Browse files Browse the repository at this point in the history
  • Loading branch information
raysan5 committed Jun 6, 2022
1 parent c9c6b3e commit 4a8e17f
Show file tree
Hide file tree
Showing 11 changed files with 973 additions and 1,092 deletions.
Binary file removed examples/binaries/physics_demo.exe
Binary file not shown.
Binary file removed examples/binaries/physics_friction.exe
Binary file not shown.
Binary file removed examples/binaries/physics_movement.exe
Binary file not shown.
Binary file removed examples/binaries/physics_restitution.exe
Binary file not shown.
Binary file removed examples/binaries/physics_shatter.exe
Binary file not shown.
68 changes: 37 additions & 31 deletions examples/physics_demo.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
/*******************************************************************************************
*
* Physac - Physics demo
* raylib [physac] example - physics demo
*
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread
* is created to manage physics calculations.
* NOTE 2: Physac requires static C library linkage to avoid dependency
* on MinGW DLL (-static -lpthread)
* 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 this program using:
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
*
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
*
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#define PHYSAC_IMPLEMENTATION
#include "physac.h"
#include "extras/physac.h"

int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;

SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "[physac] Basic demo");
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics demo");

// Physac logo drawing position
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
Expand All @@ -38,35 +34,44 @@ int main()
InitPhysics();

// Create floor rectangle physics body
PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2.0f, (float)screenHeight }, 500, 100, 10);
floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)

// Create obstacle circle physics body
PhysicsBody circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
circle->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
SetTargetFPS(60);
PhysicsBody circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2.0f, screenHeight/2.0f }, 45, 10);
circle->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)

SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdatePhysics(); // Update physics system

if (IsKeyPressed(KEY_R)) // Reset physics system
{
ResetPhysics();

floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2.0f, (float)screenHeight }, 500, 100, 10);
floor->enabled = false;

circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2.0f, screenHeight/2.0f }, 45, 10);
circle->enabled = false;
}

// Physics body creation inputs
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))
CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) CreatePhysicsBodyPolygon(GetMousePosition(), (float)GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) CreatePhysicsBodyCircle(GetMousePosition(), (float)GetRandomValue(10, 45), 10);

// Destroy falling physics bodies
int bodiesCount = GetPhysicsBodiesCount();
for (int i = bodiesCount - 1; i >= 0; i--)
{
PhysicsBody body = GetPhysicsBody(i);

if ((body != NULL) && (body->position.y > screenHeight*2))
DestroyPhysicsBody(body);
if (body != NULL && (body->position.y > screenHeight*2)) DestroyPhysicsBody(body);
}
//----------------------------------------------------------------------------------

Expand Down Expand Up @@ -103,6 +108,7 @@ int main()

DrawText("Left mouse button to create a polygon", 10, 10, 10, WHITE);
DrawText("Right mouse button to create a circle", 10, 25, 10, WHITE);
DrawText("Press 'R' to reset example", 10, 40, 10, WHITE);

DrawText("Physac", logoX, logoY, 30, WHITE);
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
Expand All @@ -112,11 +118,11 @@ int main()
}

// De-Initialization
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
ClosePhysics(); // Unitialize physics

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}
}
72 changes: 42 additions & 30 deletions examples/physics_friction.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
/*******************************************************************************************
*
* Physac - Physics friction
* raylib [physac] example - physics friction
*
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread
* is created to manage physics calculations.
* NOTE 2: Physac requires static C library linkage to avoid dependency
* on MinGW DLL (-static -lpthread)
* 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 this program using:
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
*
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
*
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#define PHYSAC_IMPLEMENTATION
#include "physac.h"
#include "extras/physac.h"

int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;

SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "[physac] Friction demo");
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics friction");

// Physac logo drawing position
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
Expand All @@ -38,18 +34,18 @@ int main()
InitPhysics();

// Create floor rectangle physics body
PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2.0f, (float)screenHeight }, (float)screenWidth, 100, 10);
floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
PhysicsBody wall = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight*0.8f }, 10, 80, 10);
PhysicsBody wall = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2.0f, screenHeight*0.8f }, 10, 80, 10);
wall->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)

// Create left ramp physics body
PhysicsBody rectLeft = CreatePhysicsBodyRectangle((Vector2){ 25, screenHeight - 5 }, 250, 250, 10);
PhysicsBody rectLeft = CreatePhysicsBodyRectangle((Vector2){ 25, (float)screenHeight - 5 }, 250, 250, 10);
rectLeft->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
SetPhysicsBodyRotation(rectLeft, 30*DEG2RAD);

// Create right ramp physics body
PhysicsBody rectRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 25, screenHeight - 5 }, 250, 250, 10);
PhysicsBody rectRight = CreatePhysicsBodyRectangle((Vector2){ (float)screenWidth - 25, (float)screenHeight - 5 }, 250, 250, 10);
rectRight->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
SetPhysicsBodyRotation(rectRight, 330*DEG2RAD);

Expand All @@ -59,20 +55,34 @@ int main()
bodyA->dynamicFriction = 0.1f;
SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);

PhysicsBody bodyB = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 35, screenHeight*0.6f }, 40, 40, 10);
bodyB->staticFriction = 1;
bodyB->dynamicFriction = 1;
PhysicsBody bodyB = CreatePhysicsBodyRectangle((Vector2){ (float)screenWidth - 35, (float)screenHeight*0.6f }, 40, 40, 10);
bodyB->staticFriction = 1.0f;
bodyB->dynamicFriction = 1.0f;
SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);

SetTargetFPS(60);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// ...
UpdatePhysics(); // Update physics system

if (IsKeyPressed(KEY_R)) // Reset physics system
{
// Reset dynamic physics bodies position, velocity and rotation
bodyA->position = (Vector2){ 35, screenHeight*0.6f };
bodyA->velocity = (Vector2){ 0, 0 };
bodyA->angularVelocity = 0;
SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);

bodyB->position = (Vector2){ (float)screenWidth - 35, screenHeight * 0.6f };
bodyB->velocity = (Vector2){ 0, 0 };
bodyB->angularVelocity = 0;
SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
}
//----------------------------------------------------------------------------------

// Draw
Expand Down Expand Up @@ -108,9 +118,11 @@ int main()

DrawRectangle(0, screenHeight - 49, screenWidth, 49, BLACK);

DrawText("Friction amount", (screenWidth - MeasureText("Friction amount", 30))/2, 75, 30, WHITE);
DrawText("0.1", bodyA->position.x - MeasureText("0.1", 20)/2, bodyA->position.y - 7, 20, WHITE);
DrawText("1", bodyB->position.x - MeasureText("1", 20)/2, bodyB->position.y - 7, 20, WHITE);
DrawText("Friction amount", (screenWidth - MeasureText("Friction amount", 30))/2.0f, 75, 30, WHITE);
DrawText("0.1", (int)bodyA->position.x - MeasureText("0.1", 20)/2, (int)bodyA->position.y - 7, 20, WHITE);
DrawText("1", (int)bodyB->position.x - MeasureText("1", 20)/2, (int)bodyB->position.y - 7, 20, WHITE);

DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);

DrawText("Physac", logoX, logoY, 30, WHITE);
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
Expand All @@ -120,11 +132,11 @@ int main()
}

// De-Initialization
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
ClosePhysics(); // Unitialize physics

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}
}
66 changes: 35 additions & 31 deletions examples/physics_movement.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
/*******************************************************************************************
*
* Physac - Physics movement
* raylib [physac] example - physics movement
*
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread
* is created to manage physics calculations.
* NOTE 2: Physac requires static C library linkage to avoid dependency
* on MinGW DLL (-static -lpthread)
* 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 this program using:
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
*
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
*
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#define PHYSAC_IMPLEMENTATION
#include "physac.h"
#include "extras/physac.h"

#define VELOCITY 0.5f

int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;

SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "[physac] - Body controller demo");
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics movement");

// Physac logo drawing position
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
Expand All @@ -40,11 +36,11 @@ int main()
InitPhysics();

// Create floor and walls rectangle physics body
PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2.0f, (float)screenHeight }, (float)screenWidth, 100, 10);
PhysicsBody platformLeft = CreatePhysicsBodyRectangle((Vector2){ screenWidth*0.25f, screenHeight*0.6f }, screenWidth*0.25f, 10, 10);
PhysicsBody platformRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth*0.75f, screenHeight*0.6f }, screenWidth*0.25f, 10, 10);
PhysicsBody wallLeft = CreatePhysicsBodyRectangle((Vector2){ -5, screenHeight/2 }, 10, screenHeight, 10);
PhysicsBody wallRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth + 5, screenHeight/2 }, 10, screenHeight, 10);
PhysicsBody wallLeft = CreatePhysicsBodyRectangle((Vector2){ -5, screenHeight/2.0f }, 10, (float)screenHeight, 10);
PhysicsBody wallRight = CreatePhysicsBodyRectangle((Vector2){ (float)screenWidth + 5, screenHeight/2.0f }, 10, (float)screenHeight, 10);

// Disable dynamics to floor and walls physics bodies
floor->enabled = false;
Expand All @@ -54,26 +50,33 @@ int main()
wallRight->enabled = false;

// Create movement physics body
PhysicsBody body = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight/2 }, 50, 50, 1);
body->freezeOrient = true; // Constrain body rotation to avoid little collision torque amounts
SetTargetFPS(60);
PhysicsBody body = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2.0f, screenHeight/2.0f }, 50, 50, 1);
body->freezeOrient = true; // Constrain body rotation to avoid little collision torque amounts

SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdatePhysics(); // Update physics system

if (IsKeyPressed(KEY_R)) // Reset physics input
{
// Reset movement physics body position, velocity and rotation
body->position = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
body->velocity = (Vector2){ 0, 0 };
SetPhysicsBodyRotation(body, 0);
}

// Horizontal movement input
if (IsKeyDown(KEY_RIGHT))
body->velocity.x = VELOCITY;
else if (IsKeyDown(KEY_LEFT))
body->velocity.x = -VELOCITY;
if (IsKeyDown(KEY_RIGHT)) body->velocity.x = VELOCITY;
else if (IsKeyDown(KEY_LEFT)) body->velocity.x = -VELOCITY;

// Vertical movement input checking if player physics body is grounded
if (IsKeyDown(KEY_UP) && body->isGrounded)
body->velocity.y = -VELOCITY*4;
if (IsKeyDown(KEY_UP) && body->isGrounded) body->velocity.y = -VELOCITY*4;
//----------------------------------------------------------------------------------

// Draw
Expand Down Expand Up @@ -105,6 +108,7 @@ int main()
}

DrawText("Use 'ARROWS' to move player", 10, 10, 10, WHITE);
DrawText("Press 'R' to reset example", 10, 30, 10, WHITE);

DrawText("Physac", logoX, logoY, 30, WHITE);
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
Expand All @@ -114,11 +118,11 @@ int main()
}

// De-Initialization
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
ClosePhysics(); // Unitialize physics

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}
}
Loading

0 comments on commit 4a8e17f

Please sign in to comment.