summaryrefslogtreecommitdiff
path: root/libphysics/blockmap.c
blob: 26af02166e4b4c3a2be902360f80b99f46996f2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <vecmath.h>
#include <physics.h>

#include <stdlib.h>
#include <string.h>

#include "dat.h"

#define BUCKETS      0x10000 /* must be a power of 2 */     

#define BUCKETMASK  (BUCKETS - 1)

#define PHI   1.618033988749895 
#define HMUL  (int)(BUCKETS / PHI)

#define LENGTH(X) (sizeof (X) / sizeof (X)[0])

static int hash(int a);
static void makeblockmap(Body *b);

static void enqueuetest(TestCollision *t);
static void bodycollisionmap(Body *);
static void bodycollisionmapsub(Body *, BlockmapNode *);
static BlockmapNode *allocnode(void);

static void phxmakeblkmap(void);
static BlockmapNode *phxnodelist(float x, float y);

static BlockmapNode nodes[(1 << 20)];
static int nodesi;
static BlockmapNode *lists[BUCKETS];

static TestCollision testcollision[0x1000];
static int testcollisioncount;

void
__phxbroadphasereset(void)
{
	static int count = 0;
	if(count == 0)
		phxmakeblkmap();
	count = (count + 1) % 4;
}

void 
__phxbroadphase(int n, BodyID bid[n])
{
	testcollisioncount = 0;
	for(int i = 0; i < n; i++) {
		Body *b = phxbodypool + bid[i];
		bodycollisionmap(b);
	}
	__phxnarrowphase(testcollisioncount, testcollision);
}

void
phxmakeblkmap()
{
	nodesi = 0;

	for(int i = 0; i < BUCKETS; i++)
		lists[i] = NULL;

	for(int i = 0; i < phxbodypoolsize; i++) {
		Body *b = phxbodypool + i;
		if(!b->active)
			continue;
		makeblockmap(b);
	}
}

void
makeblockmap(Body *b)
{
	BlockmapNode *n;
	if((n = allocnode()) == NULL)
		return;

	n->id = b - phxbodypool;
	n->next = NULL;

	int mapx = (int)floorf(b->pos[0] / BLOCK_SIZE);
	int mapy = (int)floorf(b->pos[1] / BLOCK_SIZE);
	int h = hash(mapx + hash(mapy));

	n->next = lists[h];
	lists[h] = n;
}

int
hash(int a)
{
	return (a * HMUL) & BUCKETMASK;
}

BlockmapNode *
allocnode(void)
{
	if(nodesi == (sizeof(nodes) / sizeof(nodes[0])))
		return NULL;
	return nodes + nodesi++;
}

BlockmapNode *
phxnodelist(float x, float y)
{
	int mapx = (int)floorf(x / BLOCK_SIZE);
	int mapy = (int)floorf(y / BLOCK_SIZE);
	int h = hash(mapx + hash(mapy));
	return lists[h];
}

void
bodycollisionmap(Body *a)
{
	for(int y = -1; y <= 1; y++)
	for(int x = -1; x <= 1; x++) {
		bodycollisionmapsub(a, phxnodelist(
			a->pos[0] + x * BLOCK_SIZE, 
			a->pos[1] + y * BLOCK_SIZE
		));	
	}
}

void
bodycollisionmapsub(Body *a, BlockmapNode *n)
{
	while(n) {
		Body *b = phxbodypool + n->id;
		enqueuetest(&(TestCollision) {
			.a = a - phxbodypool,
			.b = b - phxbodypool
		});
		n = n->next;
	}
}

void
enqueuetest(TestCollision *t)
{
	testcollision[testcollisioncount++] = *t;
	if(testcollisioncount >= LENGTH(testcollision)) {
		__phxnarrowphase(testcollisioncount, testcollision);
		testcollisioncount = 0;
	}
}