xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 170043ec7675acc7c9042ba28cc4277d751391aa
parent bf449b2ef5ff9b1f59c2cc9393bb44c16c4177df
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 31 Dec 2022 14:46:30 +0900

rename

Diffstat:
Mex8/main.h | 2+-
Dex8/physics.c | 153-------------------------------------------------------------------------------
2 files changed, 1 insertion(+), 154 deletions(-)

diff --git a/ex8/main.h b/ex8/main.h @@ -1,5 +1,5 @@ /* - * physics.c + * util.c */ enum object_shape { SRECTANGLE, diff --git a/ex8/physics.c b/ex8/physics.c @@ -1,153 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -#include "main.h" - -static int test_collision_rr(struct Object *, struct Object *); -static void rect_next_tick(struct Object *, long); -static void rect_handle_collision_mf(struct Object *, struct Object *); - -struct Object * -create_object(float px, float py, float vx, float vy, float ax, float ay, - int w, int h, int m) -{ - struct Object *o; - o = (struct Object *)malloc(sizeof(struct Object)); - o->pp.x = o->p.x = px; - o->pp.y = o->p.y = py; - o->v.x = vx; - o->v.y = vy; - o->a.x = ax; - o->a.y = ay; - o->shape = SRECTANGLE; - o->body.rectangle.w = w; - o->body.rectangle.h = h; - o->m = m; - return o; -} - -/* - * 1 if collide, 0 if not - */ -int -test_collision(struct Object *o1, struct Object *o2) -{ - if (o1->shape == SRECTANGLE && o2->shape == SRECTANGLE) - return test_collision_rr(o1, o2); - else { - fprintf(stderr, "test_collision for other shapes is not implemented yet\n"); - return -1; - } -} - -static int -test_collision_rr(struct Object *o1, struct Object *o2) -{ - if (o1->shape != SRECTANGLE || o2->shape != SRECTANGLE) { - fprintf(stderr, "test_collision_rr: invalid objects\n"); - return -1; - } - return o1->p.x < o2->p.x + o2->body.rectangle.w && - o2->p.x < o1->p.x + o1->body.rectangle.w && - o2->p.y < o1->p.y + o1->body.rectangle.h && - o1->p.y < o2->p.y + o2->body.rectangle.h; -} - -void -next_tick(struct Object *o, long ndt) -{ - switch (o->shape) { - case SRECTANGLE: - rect_next_tick(o, ndt); - break; - default: - fprintf(stderr, "next_tick: not implemented for other shapes\n"); - break; - } -} - -static void -rect_next_tick(struct Object *o, long ndt) -{ - if (o->shape != SRECTANGLE) { - fprintf(stderr, "rect_next_tick: invalid object shape\n"); - return; - } - o->pp.x = o->p.x; - o->pp.y = o->p.y; - o->v.x += o->a.x * ndt / 1e9; - o->v.y += o->a.y * ndt / 1e9; - o->p.x += o->v.x * ndt / 1e9; - o->p.y += o->v.y * ndt / 1e9; -} - -void -handle_collision_mf(struct Object *om, struct Object *of) -{ - // TODO: too many testing? - if (!test_collision(om, of)) - return; - if (om->shape == SRECTANGLE && of->shape == SRECTANGLE) - rect_handle_collision_mf(om, of); - else { - fprintf(stderr, "handle_collision_mf: not implemented for other shapes\n"); - return; - } -} - -/* - * Handle collision of a moving rect against fixed rect - */ -static void -rect_handle_collision_mf(struct Object *om, struct Object *of) -{ - // TODO: too many testing? - if (!test_collision(om, of)) - return; - if (om->pp.x + om->body.rectangle.w <= of->pp.x && - of->p.x < om->p.x + om->body.rectangle.w) { - // collisioin from left to right - om->p.x = of->p.x - om->body.rectangle.w; - om->v.x = 0; - } - if (of->pp.x + of->body.rectangle.w <= om->pp.x && - om->p.x < of->p.x + of->body.rectangle.w) { - // collision from right to left - om->p.x = of->p.x + of->body.rectangle.w; - om->v.x = 0; - } - - if (om->pp.y + om->body.rectangle.h <= of->pp.y && - of->p.y < om->p.y + om->body.rectangle.h) { - // collision from up to down - om->p.y = of->p.y - om->body.rectangle.h; - om->v.y = 0; - } - if (of->pp.y + of->body.rectangle.h <= om->pp.y && - om->p.y < of->p.y + of->body.rectangle.h) { - // collision from down to up - om->p.y = of->p.y + of->body.rectangle.h; - om->v.y = 0; - } - -} - -/* - * Test if object o is not on top of one of fb (floor blocks). - * o: object to be tested, fb: floor blocks, num_f: count of fb. - */ -int -object_is_falling(struct Object *o, struct Object *fb, int num_f) -{ - struct Object p = *o; - - p.v.x = 0; p.v.y = 1; - p.a.x = 0; p.a.y = 0; - - next_tick(&p, 1e9); - - for (int i = 0; i < num_f; i++) - if (test_collision(&p, &fb[i])) - return 0; - return 1; -}