commit 7e76887fcbdb19c3d0bbc893a1b320d56ecb51d2
parent 6e1613e4a1712c9b3b8dc9aea3206e0a236e1a9d
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 2 Jan 2023 18:49:28 +0900
change order of handling collision
Diffstat:
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/ex4/ex4.c b/ex4/ex4.c
@@ -16,7 +16,7 @@
#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,
@@ -313,8 +313,10 @@ handle_collision_mm(struct square *s1, struct square *s2)
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);
+ float rel_vx = max(s1->vx - s2->vx, s2->vx - s1->vx);
+ float rel_vy = max(s1->vy - s2->vy, s2->vy - s1->vy);
- if (lapx < lapy) {
+ if (lapx / rel_vx < lapy / rel_vy) {
if (s1->px + s1->w < s2->px + s2->w / 2) {
s1->px -= lapx / 2;
s2->px += lapx / 2;
@@ -339,8 +341,6 @@ 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;
@@ -359,6 +359,8 @@ handle_collision_elastic(struct square *s1, struct square *s2)
s1->vy = 2*m2/(m1+m2)*v2 + (m1-m2)/(m1+m2)*v1;
s2->vy = 2*m1/(m1+m2)*v1 + (m2-m1)/(m1+m2)*v2;
}
+
+ handle_collision_mm(s1, s2);
}
void