1 /*******************************************************************************************
2 *
3 * raylib [physac] example - physics friction
4 *
5 * This example has been created using raylib 1.5 (www.raylib.com)
6 * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7 *
8 * This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
9 *
10 * Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
11 *
12 ********************************************************************************************/
13
14 #include "raylib.h"
15
16 #define PHYSAC_IMPLEMENTATION
17 #include "physac.h"
18
main(void)19 int main(void)
20 {
21 // Initialization
22 //--------------------------------------------------------------------------------------
23 const int screenWidth = 800;
24 const int screenHeight = 450;
25
26 SetConfigFlags(FLAG_MSAA_4X_HINT);
27 InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics friction");
28
29 // Physac logo drawing position
30 int logoX = screenWidth - MeasureText("Physac", 30) - 10;
31 int logoY = 15;
32
33 // Initialize physics and default physics bodies
34 InitPhysics();
35
36 // Create floor rectangle physics body
37 PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
38 floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
39 PhysicsBody wall = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight*0.8f }, 10, 80, 10);
40 wall->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
41
42 // Create left ramp physics body
43 PhysicsBody rectLeft = CreatePhysicsBodyRectangle((Vector2){ 25, screenHeight - 5 }, 250, 250, 10);
44 rectLeft->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
45 SetPhysicsBodyRotation(rectLeft, 30*DEG2RAD);
46
47 // Create right ramp physics body
48 PhysicsBody rectRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 25, screenHeight - 5 }, 250, 250, 10);
49 rectRight->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
50 SetPhysicsBodyRotation(rectRight, 330*DEG2RAD);
51
52 // Create dynamic physics bodies
53 PhysicsBody bodyA = CreatePhysicsBodyRectangle((Vector2){ 35, screenHeight*0.6f }, 40, 40, 10);
54 bodyA->staticFriction = 0.1f;
55 bodyA->dynamicFriction = 0.1f;
56 SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
57
58 PhysicsBody bodyB = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 35, screenHeight*0.6f }, 40, 40, 10);
59 bodyB->staticFriction = 1.0f;
60 bodyB->dynamicFriction = 1.0f;
61 SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
62
63 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
64 //--------------------------------------------------------------------------------------
65
66 // Main game loop
67 while (!WindowShouldClose()) // Detect window close button or ESC key
68 {
69 // Update
70 //----------------------------------------------------------------------------------
71 UpdatePhysics(); // Update physics system
72
73 if (IsKeyPressed(KEY_R)) // Reset physics system
74 {
75 // Reset dynamic physics bodies position, velocity and rotation
76 bodyA->position = (Vector2){ 35, screenHeight*0.6f };
77 bodyA->velocity = (Vector2){ 0, 0 };
78 bodyA->angularVelocity = 0;
79 SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
80
81 bodyB->position = (Vector2){ screenWidth - 35, screenHeight*0.6f };
82 bodyB->velocity = (Vector2){ 0, 0 };
83 bodyB->angularVelocity = 0;
84 SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
85 }
86 //----------------------------------------------------------------------------------
87
88 // Draw
89 //----------------------------------------------------------------------------------
90 BeginDrawing();
91
92 ClearBackground(BLACK);
93
94 DrawFPS(screenWidth - 90, screenHeight - 30);
95
96 // Draw created physics bodies
97 int bodiesCount = GetPhysicsBodiesCount();
98 for (int i = 0; i < bodiesCount; i++)
99 {
100 PhysicsBody body = GetPhysicsBody(i);
101
102 if (body != NULL)
103 {
104 int vertexCount = GetPhysicsShapeVerticesCount(i);
105 for (int j = 0; j < vertexCount; j++)
106 {
107 // Get physics bodies shape vertices to draw lines
108 // Note: GetPhysicsShapeVertex() already calculates rotation transformations
109 Vector2 vertexA = GetPhysicsShapeVertex(body, j);
110
111 int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
112 Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
113
114 DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
115 }
116 }
117 }
118
119 DrawRectangle(0, screenHeight - 49, screenWidth, 49, BLACK);
120
121 DrawText("Friction amount", (screenWidth - MeasureText("Friction amount", 30))/2, 75, 30, WHITE);
122 DrawText("0.1", bodyA->position.x - MeasureText("0.1", 20)/2, bodyA->position.y - 7, 20, WHITE);
123 DrawText("1", bodyB->position.x - MeasureText("1", 20)/2, bodyB->position.y - 7, 20, WHITE);
124
125 DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
126
127 DrawText("Physac", logoX, logoY, 30, WHITE);
128 DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
129
130 EndDrawing();
131 //----------------------------------------------------------------------------------
132 }
133
134 // De-Initialization
135 //--------------------------------------------------------------------------------------
136 ClosePhysics(); // Unitialize physics
137
138 CloseWindow(); // Close window and OpenGL context
139 //--------------------------------------------------------------------------------------
140
141 return 0;
142 }
143