Skip to content

Commit 2fd40de

Browse files
authored
Fixed ResolveCircleToPolygon and code format (#39)
* Reviewed ResolveCircleToPolygon and code format * Improved examples * Updated examples binaries * Updated readme file
1 parent e95eae2 commit 2fd40de

12 files changed

+390
-333
lines changed

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<img src="https://github.com/victorfisac/Physac/blob/master/icon/physac_256x256.png">
22

33
# Physac
4-
_Created by Víctor Fisac [www.victorfisac.com]_
54

65
Physac is a small 2D physics engine written in pure C. The engine uses a fixed time-step thread loop to simluate physics.
76
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.
@@ -45,11 +44,14 @@ typedef struct *PhysicsBody {
4544
The header contains a few customizable define values. I set the values that gived me the best results.
4645

4746
```c
48-
#define DESIRED_DELTATIME 1.0/60.0
49-
#define MAX_TIMESTEP 0.02
50-
#define COLLISION_ITERATIONS 100
51-
#define PENETRATION_ALLOWANCE 0.05f
52-
#define PENETRATION_CORRECTION 0.4f
47+
#define PHYSAC_MAX_BODIES 64
48+
#define PHYSAC_MAX_MANIFOLDS 4096
49+
#define PHYSAC_MAX_VERTICES 24
50+
#define PHYSAC_CIRCLE_VERTICES 24
51+
52+
#define PHYSAC_COLLISION_ITERATIONS 100
53+
#define PHYSAC_PENETRATION_ALLOWANCE 0.05f
54+
#define PHYSAC_PENETRATION_CORRECTION 0.4f
5355
```
5456
5557
Physac contains defines for memory management functions (malloc, free) to bring the user the opportunity to implement its own memory functions:
@@ -110,9 +112,6 @@ void SetPhysicsBodyRotation(PhysicsBody body, float radians);
110112
// Unitializes and destroy a physics body
111113
void DestroyPhysicsBody(PhysicsBody body);
112114

113-
// Destroys created physics bodies and manifolds and resets global values
114-
void ResetPhysics(void);
115-
116115
// Unitializes physics pointers and closes physics loop thread
117116
void ClosePhysics(void);
118117
```

examples/binaries/physics_demo.exe

193 KB
Binary file not shown.
193 KB
Binary file not shown.
193 KB
Binary file not shown.
193 KB
Binary file not shown.

examples/binaries/physics_shatter.exe

193 KB
Binary file not shown.

examples/physics_demo.c

+10-19
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
1212
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
1313
*
14-
* Copyright (c) 2016-2018 Victor Fisac (github: @victorfisac)
14+
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
1515
*
1616
********************************************************************************************/
1717

@@ -28,7 +28,7 @@ int main()
2828
int screenHeight = 450;
2929

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

3333
// Physac logo drawing position
3434
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
@@ -53,27 +53,20 @@ int main()
5353
{
5454
// Update
5555
//----------------------------------------------------------------------------------
56-
if (IsKeyPressed('R')) // Reset physics input
57-
{
58-
ResetPhysics();
59-
60-
floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
61-
floor->enabled = false;
62-
63-
circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
64-
circle->enabled = false;
65-
}
66-
6756
// Physics body creation inputs
68-
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
69-
else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);
57+
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
58+
CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
59+
else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))
60+
CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);
7061

7162
// Destroy falling physics bodies
7263
int bodiesCount = GetPhysicsBodiesCount();
7364
for (int i = bodiesCount - 1; i >= 0; i--)
7465
{
7566
PhysicsBody body = GetPhysicsBody(i);
76-
if (body != NULL && (body->position.y > screenHeight*2)) DestroyPhysicsBody(body);
67+
68+
if ((body != NULL) && (body->position.y > screenHeight*2))
69+
DestroyPhysicsBody(body);
7770
}
7871
//----------------------------------------------------------------------------------
7972

@@ -110,7 +103,6 @@ int main()
110103

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

115107
DrawText("Physac", logoX, logoY, 30, WHITE);
116108
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
@@ -127,5 +119,4 @@ int main()
127119
//--------------------------------------------------------------------------------------
128120

129121
return 0;
130-
}
131-
122+
}

examples/physics_friction.c

+4-19
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
1212
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
1313
*
14-
* Copyright (c) 2016-2018 Victor Fisac (github: @victorfisac)
14+
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
1515
*
1616
********************************************************************************************/
1717

@@ -28,7 +28,7 @@ int main()
2828
int screenHeight = 450;
2929

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

3333
// Physac logo drawing position
3434
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
@@ -72,19 +72,7 @@ int main()
7272
{
7373
// Update
7474
//----------------------------------------------------------------------------------
75-
if (IsKeyPressed('R')) // Reset physics input
76-
{
77-
// Reset dynamic physics bodies position, velocity and rotation
78-
bodyA->position = (Vector2){ 35, screenHeight*0.6f };
79-
bodyA->velocity = (Vector2){ 0, 0 };
80-
bodyA->angularVelocity = 0;
81-
SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
82-
83-
bodyB->position = (Vector2){ screenWidth - 35, screenHeight*0.6f };
84-
bodyB->velocity = (Vector2){ 0, 0 };
85-
bodyB->angularVelocity = 0;
86-
SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
87-
}
75+
// ...
8876
//----------------------------------------------------------------------------------
8977

9078
// Draw
@@ -124,8 +112,6 @@ int main()
124112
DrawText("0.1", bodyA->position.x - MeasureText("0.1", 20)/2, bodyA->position.y - 7, 20, WHITE);
125113
DrawText("1", bodyB->position.x - MeasureText("1", 20)/2, bodyB->position.y - 7, 20, WHITE);
126114

127-
DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
128-
129115
DrawText("Physac", logoX, logoY, 30, WHITE);
130116
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
131117

@@ -141,5 +127,4 @@ int main()
141127
//--------------------------------------------------------------------------------------
142128

143129
return 0;
144-
}
145-
130+
}

examples/physics_movement.c

+9-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
1212
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
1313
*
14-
* Copyright (c) 2016-2018 Victor Fisac (github: @victorfisac)
14+
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
1515
*
1616
********************************************************************************************/
1717

@@ -30,7 +30,7 @@ int main()
3030
int screenHeight = 450;
3131

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

3535
// Physac logo drawing position
3636
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
@@ -65,20 +65,15 @@ int main()
6565
{
6666
// Update
6767
//----------------------------------------------------------------------------------
68-
if (IsKeyPressed('R')) // Reset physics input
69-
{
70-
// Reset movement physics body position, velocity and rotation
71-
body->position = (Vector2){ screenWidth/2, screenHeight/2 };
72-
body->velocity = (Vector2){ 0, 0 };
73-
SetPhysicsBodyRotation(body, 0);
74-
}
75-
7668
// Horizontal movement input
77-
if (IsKeyDown(KEY_RIGHT)) body->velocity.x = VELOCITY;
78-
else if (IsKeyDown(KEY_LEFT)) body->velocity.x = -VELOCITY;
69+
if (IsKeyDown(KEY_RIGHT))
70+
body->velocity.x = VELOCITY;
71+
else if (IsKeyDown(KEY_LEFT))
72+
body->velocity.x = -VELOCITY;
7973

8074
// Vertical movement input checking if player physics body is grounded
81-
if (IsKeyDown(KEY_UP) && body->isGrounded) body->velocity.y = -VELOCITY*4;
75+
if (IsKeyDown(KEY_UP) && body->isGrounded)
76+
body->velocity.y = -VELOCITY*4;
8277
//----------------------------------------------------------------------------------
8378

8479
// Draw
@@ -110,7 +105,6 @@ int main()
110105
}
111106

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

115109
DrawText("Physac", logoX, logoY, 30, WHITE);
116110
DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
@@ -127,5 +121,4 @@ int main()
127121
//--------------------------------------------------------------------------------------
128122

129123
return 0;
130-
}
131-
124+
}

examples/physics_restitution.c

+9-21
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
1212
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
1313
*
14-
* Copyright (c) 2016-2018 Victor Fisac (github: @victorfisac)
14+
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
1515
*
1616
********************************************************************************************/
1717

@@ -28,7 +28,7 @@ int main()
2828
int screenHeight = 450;
2929

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

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

4545
// Create circles physics body
4646
PhysicsBody circleA = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.25f, screenHeight/2 }, 30, 10);
47-
circleA->restitution = 0;
47+
circleA->restitution = 0.0f;
4848
PhysicsBody circleB = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.5f, screenHeight/2 }, 30, 10);
4949
circleB->restitution = 0.5f;
5050
PhysicsBody circleC = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.75f, screenHeight/2 }, 30, 10);
51-
circleC->restitution = 1;
51+
circleC->restitution = 0.9f;
5252

5353
SetTargetFPS(60);
5454
//--------------------------------------------------------------------------------------
@@ -58,16 +58,7 @@ int main()
5858
{
5959
// Update
6060
//----------------------------------------------------------------------------------
61-
if (IsKeyPressed('R')) // Reset physics input
62-
{
63-
// Reset circles physics bodies position and velocity
64-
circleA->position = (Vector2){ screenWidth*0.25f, screenHeight/2 };
65-
circleA->velocity = (Vector2){ 0, 0 };
66-
circleB->position = (Vector2){ screenWidth*0.5f, screenHeight/2 };
67-
circleB->velocity = (Vector2){ 0, 0 };
68-
circleC->position = (Vector2){ screenWidth*0.75f, screenHeight/2 };
69-
circleC->velocity = (Vector2){ 0, 0 };
70-
}
61+
// ...
7162
//----------------------------------------------------------------------------------
7263

7364
// Draw
@@ -99,11 +90,9 @@ int main()
9990
}
10091

10192
DrawText("Restitution amount", (screenWidth - MeasureText("Restitution amount", 30))/2, 75, 30, WHITE);
102-
DrawText("0", circleA->position.x - MeasureText("0", 20)/2, circleA->position.y - 7, 20, WHITE);
103-
DrawText("0.5", circleB->position.x - MeasureText("0.5", 20)/2, circleB->position.y - 7, 20, WHITE);
104-
DrawText("1", circleC->position.x - MeasureText("1", 20)/2, circleC->position.y - 7, 20, WHITE);
105-
106-
DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
93+
DrawText("0%", circleA->position.x - MeasureText("0%", 20)/2, circleA->position.y - 7, 20, WHITE);
94+
DrawText("50%", circleB->position.x - MeasureText("50%", 20)/2, circleB->position.y - 7, 20, WHITE);
95+
DrawText("90%", circleC->position.x - MeasureText("90%", 20)/2, circleC->position.y - 7, 20, WHITE);
10796

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

122111
return 0;
123-
}
124-
112+
}

examples/physics_shatter.c

+15-17
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s ..\icon\physac_icon -I. -I../src
1212
* -I../src/external/raylib/src -static -lraylib -lopengl32 -lgdi32 -pthread -std=c99
1313
*
14-
* Copyright (c) 2016-2018 Victor Fisac (github: @victorfisac)
14+
* Copyright (c) 2016-2020 Victor Fisac (github: @victorfisac)
1515
*
1616
********************************************************************************************/
1717

1818
#include "raylib.h"
1919

2020
#define PHYSAC_IMPLEMENTATION
21-
#include "physac.h"
21+
#include "physac.h"
22+
23+
#define SHATTER_FORCE 200.0f
2224

2325
int main()
2426
{
@@ -28,18 +30,19 @@ int main()
2830
int screenHeight = 450;
2931

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

3335
// Physac logo drawing position
3436
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
3537
int logoY = 15;
38+
bool shatter = false;
3639

3740
// Initialize physics and default physics bodies
3841
InitPhysics();
3942
SetPhysicsGravity(0, 0);
4043

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

4447
SetTargetFPS(60);
4548
//--------------------------------------------------------------------------------------
@@ -49,22 +52,18 @@ int main()
4952
{
5053
// Update
5154
//----------------------------------------------------------------------------------
52-
if (IsKeyPressed('R')) // Reset physics input
53-
{
54-
ResetPhysics();
55-
56-
// Create random polygon physics body to shatter
57-
body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
58-
}
59-
60-
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
55+
if (!shatter && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
6156
{
57+
shatter = true;
58+
6259
// Note: some values need to be stored in variables due to asynchronous changes during main thread
6360
int count = GetPhysicsBodiesCount();
6461
for (int i = count - 1; i >= 0; i--)
6562
{
6663
PhysicsBody currentBody = GetPhysicsBody(i);
67-
if (currentBody != NULL) PhysicsShatter(currentBody, GetMousePosition(), 10/currentBody->inverseMass);
64+
65+
if (currentBody != NULL)
66+
PhysicsShatter(currentBody, GetMousePosition(), SHATTER_FORCE);
6867
}
6968
}
7069
//----------------------------------------------------------------------------------
@@ -95,7 +94,7 @@ int main()
9594
}
9695
}
9796

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

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

114113
return 0;
115-
}
116-
114+
}

0 commit comments

Comments
 (0)