Skip to content

Commit

Permalink
Fixed ResolveCircleToPolygon and code format (#39)
Browse files Browse the repository at this point in the history
* Reviewed ResolveCircleToPolygon and code format

* Improved examples

* Updated examples binaries

* Updated readme file
  • Loading branch information
victorfisac authored Nov 3, 2019
1 parent e95eae2 commit 2fd40de
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 333 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<img src="https://github.com/victorfisac/Physac/blob/master/icon/physac_256x256.png">

# Physac
_Created by Víctor Fisac [www.victorfisac.com]_

Physac is a small 2D physics engine written in pure C. The engine uses a fixed time-step thread loop to simluate physics.
A physics step contains the following phases: get collision information, apply dynamics, collision solving and position correction. It uses a very simple struct for physic bodies with a position vector to be used in any 3D rendering API.
Expand Down Expand Up @@ -45,11 +44,14 @@ typedef struct *PhysicsBody {
The header contains a few customizable define values. I set the values that gived me the best results.

```c
#define DESIRED_DELTATIME 1.0/60.0
#define MAX_TIMESTEP 0.02
#define COLLISION_ITERATIONS 100
#define PENETRATION_ALLOWANCE 0.05f
#define PENETRATION_CORRECTION 0.4f
#define PHYSAC_MAX_BODIES 64
#define PHYSAC_MAX_MANIFOLDS 4096
#define PHYSAC_MAX_VERTICES 24
#define PHYSAC_CIRCLE_VERTICES 24

#define PHYSAC_COLLISION_ITERATIONS 100
#define PHYSAC_PENETRATION_ALLOWANCE 0.05f
#define PHYSAC_PENETRATION_CORRECTION 0.4f
```
Physac contains defines for memory management functions (malloc, free) to bring the user the opportunity to implement its own memory functions:
Expand Down Expand Up @@ -110,9 +112,6 @@ void SetPhysicsBodyRotation(PhysicsBody body, float radians);
// Unitializes and destroy a physics body
void DestroyPhysicsBody(PhysicsBody body);

// Destroys created physics bodies and manifolds and resets global values
void ResetPhysics(void);

// Unitializes physics pointers and closes physics loop thread
void ClosePhysics(void);
```
Expand Down
Binary file modified examples/binaries/physics_demo.exe
Binary file not shown.
Binary file modified examples/binaries/physics_friction.exe
Binary file not shown.
Binary file modified examples/binaries/physics_movement.exe
Binary file not shown.
Binary file modified examples/binaries/physics_restitution.exe
Binary file not shown.
Binary file modified examples/binaries/physics_shatter.exe
Binary file not shown.
29 changes: 10 additions & 19 deletions examples/physics_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 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-2018 Victor Fisac (github: @victorfisac)
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
*
********************************************************************************************/

Expand All @@ -28,7 +28,7 @@ int main()
int screenHeight = 450;

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

// Physac logo drawing position
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
Expand All @@ -53,27 +53,20 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed('R')) // Reset physics input
{
ResetPhysics();

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

circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 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_LEFT_BUTTON))
CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))
CreatePhysicsBodyCircle(GetMousePosition(), 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 @@ -110,7 +103,6 @@ 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 @@ -127,5 +119,4 @@ int main()
//--------------------------------------------------------------------------------------

return 0;
}

}
23 changes: 4 additions & 19 deletions examples/physics_friction.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 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-2018 Victor Fisac (github: @victorfisac)
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
*
********************************************************************************************/

Expand All @@ -28,7 +28,7 @@ int main()
int screenHeight = 450;

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

// Physac logo drawing position
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
Expand Down Expand Up @@ -72,19 +72,7 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed('R')) // Reset physics input
{
// 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){ screenWidth - 35, screenHeight*0.6f };
bodyB->velocity = (Vector2){ 0, 0 };
bodyB->angularVelocity = 0;
SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
}
// ...
//----------------------------------------------------------------------------------

// Draw
Expand Down Expand Up @@ -124,8 +112,6 @@ int main()
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("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 @@ -141,5 +127,4 @@ int main()
//--------------------------------------------------------------------------------------

return 0;
}

}
25 changes: 9 additions & 16 deletions examples/physics_movement.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 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-2018 Victor Fisac (github: @victorfisac)
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
*
********************************************************************************************/

Expand All @@ -30,7 +30,7 @@ int main()
int screenHeight = 450;

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

// Physac logo drawing position
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
Expand Down Expand Up @@ -65,20 +65,15 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed('R')) // Reset physics input
{
// Reset movement physics body position, velocity and rotation
body->position = (Vector2){ screenWidth/2, screenHeight/2 };
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 @@ -110,7 +105,6 @@ 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 @@ -127,5 +121,4 @@ int main()
//--------------------------------------------------------------------------------------

return 0;
}

}
30 changes: 9 additions & 21 deletions examples/physics_restitution.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 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-2018 Victor Fisac (github: @victorfisac)
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
*
********************************************************************************************/

Expand All @@ -28,7 +28,7 @@ int main()
int screenHeight = 450;

SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics restitution");
InitWindow(screenWidth, screenHeight, "[physac] - Restitution demo");

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

// Create circles physics body
PhysicsBody circleA = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.25f, screenHeight/2 }, 30, 10);
circleA->restitution = 0;
circleA->restitution = 0.0f;
PhysicsBody circleB = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.5f, screenHeight/2 }, 30, 10);
circleB->restitution = 0.5f;
PhysicsBody circleC = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.75f, screenHeight/2 }, 30, 10);
circleC->restitution = 1;
circleC->restitution = 0.9f;

SetTargetFPS(60);
//--------------------------------------------------------------------------------------
Expand All @@ -58,16 +58,7 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed('R')) // Reset physics input
{
// Reset circles physics bodies position and velocity
circleA->position = (Vector2){ screenWidth*0.25f, screenHeight/2 };
circleA->velocity = (Vector2){ 0, 0 };
circleB->position = (Vector2){ screenWidth*0.5f, screenHeight/2 };
circleB->velocity = (Vector2){ 0, 0 };
circleC->position = (Vector2){ screenWidth*0.75f, screenHeight/2 };
circleC->velocity = (Vector2){ 0, 0 };
}
// ...
//----------------------------------------------------------------------------------

// Draw
Expand Down Expand Up @@ -99,11 +90,9 @@ int main()
}

DrawText("Restitution amount", (screenWidth - MeasureText("Restitution amount", 30))/2, 75, 30, WHITE);
DrawText("0", circleA->position.x - MeasureText("0", 20)/2, circleA->position.y - 7, 20, WHITE);
DrawText("0.5", circleB->position.x - MeasureText("0.5", 20)/2, circleB->position.y - 7, 20, WHITE);
DrawText("1", circleC->position.x - MeasureText("1", 20)/2, circleC->position.y - 7, 20, WHITE);

DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
DrawText("0%", circleA->position.x - MeasureText("0%", 20)/2, circleA->position.y - 7, 20, WHITE);
DrawText("50%", circleB->position.x - MeasureText("50%", 20)/2, circleB->position.y - 7, 20, WHITE);
DrawText("90%", circleC->position.x - MeasureText("90%", 20)/2, circleC->position.y - 7, 20, WHITE);

DrawText("Physac", logoX, logoY, 30, WHITE);
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
Expand All @@ -120,5 +109,4 @@ int main()
//--------------------------------------------------------------------------------------

return 0;
}

}
32 changes: 15 additions & 17 deletions examples/physics_shatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
* 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-2018 Victor Fisac (github: @victorfisac)
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
*
********************************************************************************************/

#include "raylib.h"

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

#define SHATTER_FORCE 200.0f

int main()
{
Expand All @@ -28,18 +30,19 @@ int main()
int screenHeight = 450;

SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "Physac [raylib] - Body shatter");
InitWindow(screenWidth, screenHeight, "[physac] - Shatter demo");

// Physac logo drawing position
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
int logoY = 15;
bool shatter = false;

// Initialize physics and default physics bodies
InitPhysics();
SetPhysicsGravity(0, 0);

// Create random polygon physics body to shatter
PhysicsBody body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);

SetTargetFPS(60);
//--------------------------------------------------------------------------------------
Expand All @@ -49,22 +52,18 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed('R')) // Reset physics input
{
ResetPhysics();

// Create random polygon physics body to shatter
body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
}

if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
if (!shatter && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
shatter = true;

// Note: some values need to be stored in variables due to asynchronous changes during main thread
int count = GetPhysicsBodiesCount();
for (int i = count - 1; i >= 0; i--)
{
PhysicsBody currentBody = GetPhysicsBody(i);
if (currentBody != NULL) PhysicsShatter(currentBody, GetMousePosition(), 10/currentBody->inverseMass);

if (currentBody != NULL)
PhysicsShatter(currentBody, GetMousePosition(), SHATTER_FORCE);
}
}
//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -95,7 +94,7 @@ int main()
}
}

DrawText("Left mouse button in polygon area to shatter body\nPress 'R' to reset example", 10, 10, 10, WHITE);
DrawText("Left mouse button in polygon area to shatter body", 10, 10, 10, WHITE);

DrawText("Physac", logoX, logoY, 30, WHITE);
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
Expand All @@ -112,5 +111,4 @@ int main()
//--------------------------------------------------------------------------------------

return 0;
}

}
Loading

0 comments on commit 2fd40de

Please sign in to comment.