1717
1818#include "raylib.h"
1919
20- #include <stdlib.h>
21- #include <math.h>
20+ #include <stdlib.h> // Required for: malloc(), free()
21+ #include <math.h> // Required for: hypot()
2222
23- #define MAX_BALLS 5000 // Maximum quantity of balls
23+ #define MAX_BALLS 5000 // Maximum quantity of balls
2424
25+ //----------------------------------------------------------------------------------
26+ // Types and Structures Definition
27+ //----------------------------------------------------------------------------------
28+ // Ball data type
2529typedef struct Ball {
26- Vector2 pos ; // Position
27- Vector2 vel ; // Velocity
28- Vector2 ppos ; // Previous position
30+ Vector2 position ;
31+ Vector2 speed ;
32+ Vector2 prevPosition ;
2933 float radius ;
3034 float friction ;
3135 float elasticity ;
@@ -45,11 +49,13 @@ int main(void)
4549
4650 InitWindow (screenWidth , screenHeight , "raylib [shapes] example - ball physics" );
4751
48- Ball * balls = (Ball * )malloc (sizeof (Ball )* MAX_BALLS );
52+ Ball * balls = (Ball * )RL_MALLOC (sizeof (Ball )* MAX_BALLS );
53+
54+ // Init first ball in the array
4955 balls [0 ] = (Ball ){
50- .pos = { GetScreenWidth ()/2.0f , GetScreenHeight ()/2.0f },
51- .vel = { 200 , 200 },
52- .ppos = { 0 },
56+ .position = { GetScreenWidth ()/2.0f , GetScreenHeight ()/2.0f },
57+ .speed = { 200 , 200 },
58+ .prevPosition = { 0 },
5359 .radius = 40 ,
5460 .friction = 0.99f ,
5561 .elasticity = 0.9f ,
@@ -58,12 +64,12 @@ int main(void)
5864 };
5965
6066 int ballCount = 1 ;
61- Ball * grabbedBall = NULL ; // A pointer to the current ball that is grabbed
62- Vector2 pressOffset = { 0 }; // Mouse press offset relative to the ball that grabbedd
67+ Ball * grabbedBall = NULL ; // A pointer to the current ball that is grabbed
68+ Vector2 pressOffset = { 0 }; // Mouse press offset relative to the ball that grabbedd
6369
64- float gravity = 100 ; // World gravity
70+ float gravity = 100 ; // World gravity
6571
66- SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
72+ SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
6773 //---------------------------------------------------------------------------------------
6874
6975 // Main game loop
@@ -80,8 +86,8 @@ int main(void)
8086 for (int i = ballCount - 1 ; i >= 0 ; i -- )
8187 {
8288 Ball * ball = & balls [i ];
83- pressOffset .x = mousePos .x - ball -> pos .x ;
84- pressOffset .y = mousePos .y - ball -> pos .y ;
89+ pressOffset .x = mousePos .x - ball -> position .x ;
90+ pressOffset .y = mousePos .y - ball -> position .y ;
8591
8692 // If the distance between the ball position and the mouse press position
8793 // is less than or equal to the ball radius, the event occurred inside the ball
@@ -110,9 +116,9 @@ int main(void)
110116 if (ballCount < MAX_BALLS )
111117 {
112118 balls [ballCount ++ ] = (Ball ){
113- .pos = mousePos ,
114- .vel = { (float )GetRandomValue (-300 , 300 ), (float )GetRandomValue (-300 , 300 ) },
115- .ppos = { 0 },
119+ .position = mousePos ,
120+ .speed = { (float )GetRandomValue (-300 , 300 ), (float )GetRandomValue (-300 , 300 ) },
121+ .prevPosition = { 0 },
116122 .radius = 20.0f + (float )GetRandomValue (0 , 30 ),
117123 .friction = 0.99f ,
118124 .elasticity = 0.9f ,
@@ -127,7 +133,7 @@ int main(void)
127133 {
128134 for (int i = 0 ; i < ballCount ; i ++ )
129135 {
130- if (!balls [i ].grabbed ) balls [i ].vel = (Vector2 ){ (float )GetRandomValue (-2000 , 2000 ), (float )GetRandomValue (-2000 , 2000 ) };
136+ if (!balls [i ].grabbed ) balls [i ].speed = (Vector2 ){ (float )GetRandomValue (-2000 , 2000 ), (float )GetRandomValue (-2000 , 2000 ) };
131137 }
132138 }
133139
@@ -143,49 +149,49 @@ int main(void)
143149 if (!ball -> grabbed )
144150 {
145151 // Ball repositioning using the velocity
146- ball -> pos .x += ball -> vel .x * delta ;
147- ball -> pos .y += ball -> vel .y * delta ;
152+ ball -> position .x += ball -> speed .x * delta ;
153+ ball -> position .y += ball -> speed .y * delta ;
148154
149155 // Does the ball hit the screen right boundary?
150- if ((ball -> pos .x + ball -> radius ) >= screenWidth )
156+ if ((ball -> position .x + ball -> radius ) >= screenWidth )
151157 {
152- ball -> pos .x = screenWidth - ball -> radius ; // Ball repositioning
153- ball -> vel .x = - ball -> vel .x * ball -> elasticity ; // Elasticity makes the ball lose 10% of its velocity on hit
158+ ball -> position .x = screenWidth - ball -> radius ; // Ball repositioning
159+ ball -> speed .x = - ball -> speed .x * ball -> elasticity ; // Elasticity makes the ball lose 10% of its velocity on hit
154160 }
155161 // Does the ball hit the screen left boundary?
156- else if ((ball -> pos .x - ball -> radius ) <= 0 )
162+ else if ((ball -> position .x - ball -> radius ) <= 0 )
157163 {
158- ball -> pos .x = ball -> radius ;
159- ball -> vel .x = - ball -> vel .x * ball -> elasticity ;
164+ ball -> position .x = ball -> radius ;
165+ ball -> speed .x = - ball -> speed .x * ball -> elasticity ;
160166 }
161167
162168 // The same for y axis
163- if ((ball -> pos .y + ball -> radius ) >= screenHeight )
169+ if ((ball -> position .y + ball -> radius ) >= screenHeight )
164170 {
165- ball -> pos .y = screenHeight - ball -> radius ;
166- ball -> vel .y = - ball -> vel .y * ball -> elasticity ;
171+ ball -> position .y = screenHeight - ball -> radius ;
172+ ball -> speed .y = - ball -> speed .y * ball -> elasticity ;
167173 }
168- else if ((ball -> pos .y - ball -> radius ) <= 0 )
174+ else if ((ball -> position .y - ball -> radius ) <= 0 )
169175 {
170- ball -> pos .y = ball -> radius ;
171- ball -> vel .y = - ball -> vel .y * ball -> elasticity ;
176+ ball -> position .y = ball -> radius ;
177+ ball -> speed .y = - ball -> speed .y * ball -> elasticity ;
172178 }
173179
174180 // Friction makes the ball lose 1% of its velocity each frame
175- ball -> vel .x = ball -> vel .x * ball -> friction ;
181+ ball -> speed .x = ball -> speed .x * ball -> friction ;
176182 // Gravity affects only the y axis
177- ball -> vel .y = ball -> vel .y * ball -> friction + gravity ;
183+ ball -> speed .y = ball -> speed .y * ball -> friction + gravity ;
178184 }
179185 else
180186 {
181187 // Ball repositioning using the mouse position
182- ball -> pos .x = mousePos .x - pressOffset .x ;
183- ball -> pos .y = mousePos .y - pressOffset .y ;
188+ ball -> position .x = mousePos .x - pressOffset .x ;
189+ ball -> position .y = mousePos .y - pressOffset .y ;
184190
185191 // While the ball is grabbed, recalculates its velocity
186- ball -> vel .x = (ball -> pos .x - ball -> ppos .x )/delta ;
187- ball -> vel .y = (ball -> pos .y - ball -> ppos .y )/delta ;
188- ball -> ppos = ball -> pos ;
192+ ball -> speed .x = (ball -> position .x - ball -> prevPosition .x )/delta ;
193+ ball -> speed .y = (ball -> position .y - ball -> prevPosition .y )/delta ;
194+ ball -> prevPosition = ball -> position ;
189195 }
190196 }
191197 //----------------------------------------------------------------------------------
@@ -198,8 +204,8 @@ int main(void)
198204
199205 for (int i = 0 ; i < ballCount ; i ++ )
200206 {
201- DrawCircleV (balls [i ].pos , balls [i ].radius , balls [i ].color );
202- DrawCircleLinesV (balls [i ].pos , balls [i ].radius , BLACK );
207+ DrawCircleV (balls [i ].position , balls [i ].radius , balls [i ].color );
208+ DrawCircleLinesV (balls [i ].position , balls [i ].radius , BLACK );
203209 }
204210
205211 DrawText ("grab a ball by pressing with the mouse and throw it by releasing" , 10 , 10 , 10 , DARKGRAY );
@@ -215,7 +221,7 @@ int main(void)
215221
216222 // De-Initialization
217223 //--------------------------------------------------------------------------------------
218- free (balls );
224+ RL_FREE (balls );
219225 CloseWindow (); // Close window and OpenGL context
220226 //--------------------------------------------------------------------------------------
221227
0 commit comments