commit a20b34d8e11f276e4d2c206ad94a383e97881226
parent 3e95708f12e1d914a256d850d04d630b2b043fb5
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 10 Jan 2023 14:05:16 +0900
Implement collision handler.
Implemented collision handler which handles collision of nearest
pair first and distant ones last.
The code need refactoring.
Diffstat:
M | ex9/object.c | | | 49 | +++++++++++++++++++++++++++++++++++++++++++++++-- |
1 file changed, 47 insertions(+), 2 deletions(-)
diff --git a/ex9/object.c b/ex9/object.c
@@ -220,6 +220,30 @@ olqsortx(struct List *p)
lqsort(p, (int (*)(void *, void *))&olcmpx);
}
+struct LLD {
+ struct Object *o0;
+ struct Object *o1;
+ float dist;
+};
+int
+lldcmpdst(struct LLD *p, struct LLD *q)
+{
+ if (p->dist < q->dist)
+ return -1;
+ else if (p->dist == q->dist)
+ return 0;
+ else
+ return 1;
+}
+void
+lldprint(struct LLD *p)
+{
+ printf("{");
+ oprint(p->o0);
+ printf(", ");
+ oprint(p->o1);
+ printf(", %2.0f}", p->dist);
+}
/*
* The order of list o will be changed.
*/
@@ -236,13 +260,34 @@ lhandle_collision(struct List *p)
((struct Object *)stack->item)->body.rectangle.w <=
((struct Object *)p->item)->p.x) {
// handle collision among the items in the stack.
+ // LLD: {struct Object *o0, struct Object *o1, float dist}
+ // sort LLD according to dist.
+ struct List *lld, *tmplld;
struct List *s, *t;
+ lld = NULL;
for (s = stack; s != NULL && s->next != NULL; s = s->next) {
for (t = s->next; t != NULL; t = t->next) {
- ohandle_collision((struct Object *)s->item,
- (struct Object *)t->item);
+ struct LLD *l;
+ l = (struct LLD *)malloc(sizeof(struct LLD));
+ l->o0 = (struct Object *)s->item;
+ l->o1 = (struct Object *)t->item;
+ l->dist = object_dist((struct Object *)s->item,
+ (struct Object *)t->item);
+ tmplld = laddfront(lld, l);
+ if (tmplld == NULL) {
+ fprintf(stderr, "lhandle_collision: Error. Could not"
+ "add item to lld\n");
+ exit(1);
+ }
+ lld = tmplld;
}
}
+ lqsort(lld, (int (*)(void *, void *))&lldcmpdst);
+ for (tmplld = lld; tmplld != NULL; tmplld = tmplld->next) {
+ ohandle_collision(((struct LLD *)tmplld->item)->o0,
+ ((struct LLD *)tmplld->item)->o1);
+ }
+ lfree(lld);
lfree(stack);
stack = NULL;
}