summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libphysics/Makefile3
-rw-r--r--libphysics/dat.h7
-rw-r--r--libphysics/narrow.c36
-rw-r--r--libphysics/tick.c45
4 files changed, 51 insertions, 40 deletions
diff --git a/libphysics/Makefile b/libphysics/Makefile
index 6cbabaf..d951ff6 100644
--- a/libphysics/Makefile
+++ b/libphysics/Makefile
@@ -3,7 +3,8 @@ OBJ=aabb.o\
event.o\
_map.o\
tick.o\
- blockmap.o
+ blockmap.o\
+ narrow.o
CFLAGS=-I../include -g -O3 -flto
diff --git a/libphysics/dat.h b/libphysics/dat.h
index cabe7f5..da858c3 100644
--- a/libphysics/dat.h
+++ b/libphysics/dat.h
@@ -15,6 +15,11 @@ struct BlockmapNode {
BlockmapNode *next;
};
+typedef struct TestCollision TestCollision;
+struct TestCollision {
+ BodyID a, b;
+};
+
void phxmapcollision(Body *);
void phxbodycollision(Body *, Body *);
@@ -28,6 +33,8 @@ int phxdeqevent(CollisionEvent *ev);
void phxmakeblkmap(void);
BlockmapNode *phxnodelist(float x, float y);
+void __phxnarrowphase(int n, TestCollision coll[n]);
+
extern int phxmapwidth, phxmapheight;
extern int *phxmapbuffer;
diff --git a/libphysics/narrow.c b/libphysics/narrow.c
new file mode 100644
index 0000000..14575be
--- /dev/null
+++ b/libphysics/narrow.c
@@ -0,0 +1,36 @@
+#include <vecmath.h>
+#include <physics.h>
+#include <stdlib.h>
+
+#include "dat.h"
+
+void
+__phxnarrowphase(int n, TestCollision tests[n])
+{
+ CollisionEvent event;
+
+ for(int i = 0; i < n; i++) {
+ TestCollision *c = tests + i;
+ Body *a = c->a + phxbodypool;
+ Body *b = c->b + phxbodypool;
+
+ if(a < b && phxaabbcheck(a, b)) {
+ float p[2], n[2], rvel[2], j;
+ phxaabbresolv(a, b, p, n);
+
+ vec2_sub(rvel, a->vel, b->vel);
+ j = vec2_dot(rvel, n);
+
+ vec2_sub_scaled(a->vel, a->vel, n, 0.5);
+ vec2_sub_scaled(b->vel, b->vel, n, -0.5);
+
+ vec2_sub_scaled(a->pos, a->pos, p, 0.5);
+ vec2_sub_scaled(b->pos, b->pos, p, -0.5);
+
+ event.body1 = c->a;
+ event.body2 = c->b;
+ phxenqevent(&event);
+ }
+ }
+}
+
diff --git a/libphysics/tick.c b/libphysics/tick.c
index a875915..3ba60f8 100644
--- a/libphysics/tick.c
+++ b/libphysics/tick.c
@@ -7,11 +7,6 @@
#define TILE_SIZE 16
#define TEST_BUFFER_SIZE 0x10000
-typedef struct TestCollision TestCollision;
-struct TestCollision {
- BodyID a, b;
-};
-
static void tick(void);
static void mapcollision(Body *);
static void bodycollision(Body *);
@@ -20,7 +15,6 @@ static void bodycollisionmap(Body *);
static void bodycollisionmapsub(Body *, BlockmapNode *);
static void enqueuetest(TestCollision *t);
-static void resolve(void);
static TestCollision testcollision[TEST_BUFFER_SIZE];
static int testcollisioncount;
@@ -87,13 +81,14 @@ tick(void)
mapcollision(b);
}
+ testcollisioncount = 0;
for(int i = 0; i < phxbodypoolsize; i++) {
Body *b = phxbodypool + i;
if(!b->active)
continue;
bodycollisionmap(b);
}
- resolve();
+ __phxnarrowphase(testcollisioncount, testcollision);
for(int i = 0; i < phxbodypoolsize; i++) {
Body *b = phxbodypool + i;
@@ -179,37 +174,9 @@ void
enqueuetest(TestCollision *t)
{
testcollision[testcollisioncount++] = *t;
- if(testcollisioncount >= TEST_BUFFER_SIZE)
- resolve();
-}
-
-void
-resolve(void)
-{
- CollisionEvent event;
-
- for(int i = 0; i < testcollisioncount; i++) {
- TestCollision *c = testcollision + i;
- Body *a = c->a + phxbodypool;
- Body *b = c->b + phxbodypool;
-
- if(a < b && phxaabbcheck(a, b)) {
- float p[2], n[2], rvel[2], j;
- phxaabbresolv(a, b, p, n);
-
- vec2_sub(rvel, a->vel, b->vel);
- j = vec2_dot(rvel, n);
-
- vec2_sub_scaled(a->vel, a->vel, n, 0.5);
- vec2_sub_scaled(b->vel, b->vel, n, -0.5);
-
- vec2_sub_scaled(a->pos, a->pos, p, 0.5);
- vec2_sub_scaled(b->pos, b->pos, p, -0.5);
-
- event.body1 = c->a;
- event.body2 = c->b;
- phxenqevent(&event);
- }
+ if(testcollisioncount >= TEST_BUFFER_SIZE) {
+ __phxnarrowphase(testcollisioncount, testcollision);
+ testcollisioncount = 0;
}
- testcollisioncount = 0;
}
+