commit 4487faf9bbacc1c704f105db70f0060175d80339
parent a6e39068f5c5760c4738b1eeff3e791d17f784c4
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 24 Dec 2022 16:24:51 +0900
add bound in boundery
Diffstat:
M | ex4/ex4.c | | | 65 | +++++++++++++++++++++++++++++++++++++++++++++++++++++------------ |
1 file changed, 53 insertions(+), 12 deletions(-)
diff --git a/ex4/ex4.c b/ex4/ex4.c
@@ -12,11 +12,11 @@
#define INIT_HEIGHT (600)
#define FPS (60)
#define SUB_TICK (4)
-#define NUM_SQUARE (30)
+#define NUM_SQUARE (500)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
-//#define COUNT_FPS
+// #define COUNT_FPS
enum keys {
KEY_D,
@@ -66,6 +66,7 @@ void next_tick(struct square *, long);
int test_collision(struct square *, struct square *);
void handle_collision_mf(struct square *, struct square *);
void handle_collision(struct square *, struct square *);
+void handle_collision_elastic(struct square *, struct square *);
void game_over(void);
@@ -231,6 +232,7 @@ handle_inputs(int key_state[])
next_menu = GAME_OVER;
return;
}
+ /*
square[0].vx = square[0].vy = 0;
if (key_state[KEY_D] == KEY_DOWN)
square[0].vx += 300;
@@ -240,6 +242,7 @@ handle_inputs(int key_state[])
square[0].vy += 300;
if (key_state[KEY_W] == KEY_DOWN)
square[0].vy += -300;
+ */
}
void
@@ -251,14 +254,22 @@ next_tick(struct square *s, long ndt) // nano second
s->py = s->py + s->vy * ndt / 1000 / 1000 / 1000;
// bind within the window
- if (s->px < 0)
+ if (s->px < 0) {
s->px = 0;
- if (win_width < s->px + s->w)
+ s->vx *= -1;
+ }
+ if (win_width < s->px + s->w) {
s->px = win_width - s->w;
- if (s->py < 0)
+ s->vx *= -1;
+ }
+ if (s->py < 0) {
s->py = 0;
- if (win_height < s->py + s->h)
+ s->vy *= -1;
+ }
+ if (win_height < s->py + s->h) {
s->py = win_height - s->h;
+ s->vy *= -1;
+ }
}
int
@@ -295,7 +306,7 @@ handle_collision_mf(struct square *sm, struct square *sf)
* Handle collision of a moving square against another moving square
*/
void
-handle_collision_mm(struct square *s1, struct square* s2)
+handle_collision_mm(struct square *s1, struct square *s2)
{
if (!test_collision(s1, s2))
return;
@@ -323,6 +334,34 @@ handle_collision_mm(struct square *s1, struct square* s2)
}
void
+handle_collision_elastic(struct square *s1, struct square *s2)
+{
+ if(!test_collision(s1, s2))
+ return;
+
+ handle_collision_mm(s1, s2);
+
+ float v1, v2;
+ float m1 = s1->w * s1->h;
+ float m2 = s2->w * s2->h;
+
+ float lapx = min(s1->px + s1->w, s2->px + s2->w) - max(s1->px, s2->px);
+ float lapy = min(s1->py + s1->h, s2->py + s2->h) - max(s1->py, s2->py);
+
+ if (lapx < lapy) {
+ v1 = s1->vx;
+ v2 = s2->vx;
+ s1->vx = 2*m2/(m1+m2)*v2 + (m1-m2)/(m1+m2)*v1;
+ s2->vx = 2*m1/(m1+m2)*v1 + (m2-m1)/(m1+m2)*v2;
+ } else {
+ v1 = s1->vy;
+ v2 = s2->vy;
+ s1->vy = 2*m2/(m1+m2)*v2 + (m1-m2)/(m1+m2)*v1;
+ s2->vy = 2*m1/(m1+m2)*v1 + (m2-m1)/(m1+m2)*v2;
+ }
+}
+
+void
game_play(void)
{
int key_state[NUM_KEY];
@@ -333,12 +372,14 @@ game_play(void)
struct timespec ts;
for(int i = 0; i < NUM_SQUARE; i++){
- square[i].ppx = square[i].px = 20 * (i + 1);
- square[i].ppy = square[i].py = 100;
- square[i].vx = square[i].vy = 0;
- square[i].w = square[i].h = 20;
+ square[i].ppx = square[i].px = (float)rand() * win_width / RAND_MAX;
+ square[i].ppy = square[i].py = (float)rand() * win_height / RAND_MAX;
+ square[i].vx = (float)rand() * 300 / RAND_MAX - 150;
+ square[i].vy = (float)rand() * 300 / RAND_MAX - 150;
+ square[i].w = square[i].h = (float)rand() * 30 / RAND_MAX;
}
+
while (next_menu == GAME_PLAY){
clock_gettime(CLOCK_MONOTONIC, &ts);
t0 = ts.tv_nsec;
@@ -356,7 +397,7 @@ game_play(void)
for (int i = 0; i < NUM_SQUARE; i++)
for (int j = i + 1; j < NUM_SQUARE; j++) {
- handle_collision_mm(&square[i], &square[j]);
+ handle_collision_elastic(&square[i], &square[j]);
if (test_collision(&square[i], &square[j]))
collision[i] = collision[j] = 1;
}