summaryrefslogtreecommitdiff
path: root/clld.c
blob: 4dd7a465647e5995d138bb1ccff56d338a3b48cd (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Main Program file.
 *
 * Platform: 	Anything with a C compiler.
 * Author:	mr b0nk 500 <b0nk@mrb0nk500.xyz>
 * Notes: 	Who would even use this kind of collision checker?
 * Created:	September 27, 2019 @ 09:25PM
 */

#include "clld.h"
#include <stdint.h>
#ifdef DEBUG
#include <stdio.h>
#endif

uint8_t get_coldir(uint8_t flt, uint8_t col_type, int hmpeak, struct colis *cursor, struct colis *floor) {
	uint8_t coldir = 0;

	/* Get the direction of a collision. */
	uint8_t isleft = 0;
	uint8_t isright = 0;
	uint8_t istop = 0;
	uint8_t isbottom = 0;

	uint8_t isinfloor_x = 0;
	uint8_t isinfloor_y = 0;
	uint8_t isinfloor_left   = 0;
	uint8_t isinfloor_right  = 0;
	uint8_t isinfloor_top    = 0;
	uint8_t isinfloor_bottom = 0;
	switch (col_type) {
		case AABB:
			if (flt) {
				isleft   = cursor->c2 > floor->c1-cursor->xvel-1;
				isright  = cursor->c1 < floor->c2-cursor->xvel+1;
				istop    = cursor->d2 > floor->d1-cursor->yvel-1;
				isbottom = cursor->d1 < floor->d2-cursor->yvel+1;

				isinfloor_left   = cursor->d2 > floor->d1;
				isinfloor_right  = cursor->d1 < floor->d2;
				isinfloor_top    = cursor->c2 > floor->c1;
				isinfloor_bottom = cursor->c1 < floor->c2;
			} else {
				isleft   = cursor->a2 > floor->a1-cursor->xvel-1;
				isright  = cursor->a1 < floor->a2-cursor->xvel+1;
				istop    = cursor->b2 > floor->b1-cursor->yvel-1;
				isbottom = cursor->b1 < floor->b2-cursor->yvel+1;

				isinfloor_left   = cursor->b2 > floor->b1;
				isinfloor_right  = cursor->b1 < floor->b2;
				isinfloor_top    = cursor->a2 > floor->a1;
				isinfloor_bottom = cursor->a1 < floor->a2;
			}
			isinfloor_x = isinfloor_left && isinfloor_right ;
			isinfloor_y = isinfloor_top  && isinfloor_bottom;
			break;
		case THM:
			if (flt) {
				/* Did the cursor collide with the floor from the left? */
				isleft   = cursor->c2 > floor->c1-cursor->xvel-1;
				/* Did the cursor collide with the floor from the right? */
				isright  = cursor->c1 < (floor->c2-cursor->w)-cursor->xvel+1;

				/* Is the floor's heightmap vertically flipped? */
				if (!floor->flp) {
					/* Did the cursor collide with the floor from the top? */
					istop    = cursor->d2 > (floor->d2-hmpeak)-cursor->yvel;
					/* Did the cursor collide with the floor from the bottom? */
					isbottom = cursor->d1 < floor->d2-cursor->yvel+1;

					/* Is the bottom of the cursor below the floor's leftmost height? */
					isinfloor_left   = floor->d2-cursor->d2 < floor->heightmap[0] && cursor->xvel > 0;
					/* Is the bottom of the cursor below the floor's rightmost height? */
					isinfloor_right  = floor->d2-cursor->d2 < floor->heightmap[floor->w-1] && cursor->xvel < 0;
					/* Is the bottom of the cursor at, or above the floor's ground? */
					isinfloor_top    = floor->d2-cursor->d2 >= 0;
					/* Is the top of the cursor at, or below the floor's ground? */
					isinfloor_bottom = floor->d2-cursor->d1 <= 0;
				} else {
					/* Did the cursor collide with the floor from the top? */
					istop    = cursor->d2 > floor->d1-cursor->yvel-1;
					/* Did the cursor collide with the floor from the bottom? */
					isbottom = cursor->d1 < (floor->d1+hmpeak)-cursor->yvel;

					/* Is the bottom of the cursor below the floor's leftmost height? */
					isinfloor_left   = floor->d1-cursor->d1 > -floor->heightmap[0] && cursor->xvel > 0;
					/* Is the bottom of the cursor below the floor's rightmost height? */
					isinfloor_right  = floor->d1-cursor->d1 > -floor->heightmap[floor->w-1] && cursor->xvel < 0;
					/* Is the bottom of the cursor at, or above the floor's height? */
					isinfloor_top    = floor->d1-cursor->d2 >= 0 && cursor->yvel > 0;
					/* Is the top of the cursor at, or above the floor's height? */
					isinfloor_bottom = cursor->d1-floor->d1 >= 0;
				}
			} else {
				/* Did the cursor collide with the floor from the left? */
				isleft   = cursor->a2 > floor->a1-cursor->xvel-1;
				/* Did the cursor collide with the floor from the right? */
				isright  = cursor->a1 < (floor->a2-cursor->w)-cursor->xvel+1;

				/* Is the floor's heightmap vertically flipped? */
				if (!floor->flp) {
					/* Did the cursor collide with the floor from the top? */
					istop    = cursor->b2 > (floor->b2-hmpeak)-cursor->yvel;
					/* Did the cursor collide with the floor from the bottom? */
					isbottom = cursor->b1 < floor->b2-cursor->yvel+1;

					/* Is the bottom of the cursor below the floor's leftmost height? */
					isinfloor_left  = floor->b2-cursor->b2 < floor->heightmap[0];
					/* Is the bottom of the cursor below the floor's rightmost height? */
					isinfloor_right = floor->b2-cursor->b2 < floor->heightmap[floor->w-1];
					/* Is the bottom of the cursor at, or above the floor's ground? */
					isinfloor_top = floor->b2-cursor->b2 >= 0;
					/* Is the top of the cursor at, or below the floor's ground? */
					isinfloor_bottom = floor->b2-cursor->b1 <= 0;
				} else {
					/* Did the cursor collide with the floor from the top? */
					istop    = cursor->b2 > floor->b1-cursor->yvel-1;
					/* Did the cursor collide with the floor from the bottom? */
					isbottom = cursor->b1 < (floor->b1+hmpeak)-cursor->yvel;

					/* Is the bottom of the cursor below the floor's leftmost height? */
					isinfloor_left   = floor->b1-cursor->b1 > -floor->heightmap[0];
					/* Is the bottom of the cursor below the floor's rightmost height? */
					isinfloor_right  = floor->b1-cursor->b1 > -floor->heightmap[floor->w-1];
					/* Is the bottom of the cursor at, or above the floor's ground? */
					isinfloor_top    = floor->b1-cursor->b2 >= 0 && cursor->yvel > 0;
					/* Is the top of the cursor at, or below the floor's ground? */
					isinfloor_bottom = cursor->b1-floor->b1 >= 0;
				}
			}
			isinfloor_x = isinfloor_left ^ isinfloor_right ;
			isinfloor_y = isinfloor_top  ^ isinfloor_bottom;
			break;
	}
	if (isinfloor_x) {
		coldir |= isleft   << 0;
		coldir |= isright  << 1;
	}
	if (isinfloor_y) {
		coldir |= istop    << 2;
		coldir |= isbottom << 3;
	}
	return coldir;
}

/* Tile Heightmap Collision. */
void thm(int hmpeak, struct colis *cursor, struct colis *floor) {
	int newy = 0;
	uint8_t coldir    = get_coldir(0, THM , hmpeak, cursor, floor);
	uint8_t isinfloor = get_coldir(0, AABB, hmpeak, cursor, floor);
	cursor->clx = (isinfloor &  3) ==  3;
	cursor->cly = (isinfloor & 12) == 12;
	if (cursor->clx && (cursor->a2-floor->a1 < 0 || cursor->a2-floor->a1 > floor->w)) {
		switch (coldir & 3) {
			case 1: cursor->pos.x = (floor->pos.x-cursor->xvel)-cursor->w-1; break;
			case 2: cursor->pos.x = (floor->a2-cursor->w)-cursor->xvel+1; break;
		}
		cursor->xvel = 0;
		floor->xvel = 0;
	}
	if (cursor->cly && cursor->a2-floor->a1 < floor->w) {
		newy = floor->heightmap[cursor->a2-floor->a1];
		switch (coldir & 12) {
			case 4:
				if (!floor->flp) {
					cursor->pos.y = ((floor->b2-cursor->h)-newy)-cursor->yvel;
				} else {
					cursor->pos.y = (floor->pos.y-cursor->h)-cursor->yvel;
				}
				break;
			case 8:
				if (!floor->flp) {
					cursor->pos.y = (floor->pos.y+floor->h)-cursor->yvel+1;
				} else {
					cursor->pos.y = (floor->pos.y+newy)-cursor->yvel;
				}
				break;
		}
		cursor->yvel = 0;
		floor->yvel = 0;
	}
	cursor->gnded = (cursor->pos.y == ((floor->b2-cursor->h)-newy)-cursor->yvel);
	#ifdef DEBUG
	printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor->clx, cursor->cly, cursor->gnded, newy, cursor->pos.x, cursor->pos.y, cursor->xvel, cursor->yvel);
	printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, newy, floor->pos.x, floor->pos.y, floor->xvel, floor->yvel);
	#endif
}

/* Floating point Tile Heightmap Collision. */
void fthm(int hmpeak, struct colis *cursor, struct colis *floor) {
	int newy = 0;
	uint8_t coldir    = get_coldir(0, THM , hmpeak, cursor, floor);
	uint8_t isinfloor = get_coldir(0, AABB, hmpeak, cursor, floor);
	cursor->clx = (isinfloor &  3) ==  3;
	cursor->cly = (isinfloor & 12) == 12;

	if (cursor->clx && cursor->c2-floor->c1 < -1 || cursor->c2-floor->c1 > floor->w) {
		switch (coldir & 3) {
			case 1: cursor->pos.fx = (floor->pos.fx-cursor->xvel)-cursor->w-1; break;
			case 2: cursor->pos.fx = (floor->c2-cursor->w)-cursor->xvel+1; break;
		}
		cursor->xvel = 0;
		cursor->xvel = 0;
	}
	if (cursor->cly && cursor->c2-floor->c1 < floor->w) {
		newy = floor->heightmap[(int)cursor->c2-(int)floor->c1];
		switch (coldir & 12) {
			case 4:
				if (!floor->flp) {
					cursor->pos.fy = ((floor->d2-cursor->h)-newy)-cursor->yvel;
				} else {
					cursor->pos.fy = (floor->pos.fy-cursor->h)-cursor->yvel;
				}
				break;
			case 8:
				if (!floor->flp) {
					cursor->pos.fy = (floor->pos.fy+floor->h)-cursor->yvel;
				} else {
					cursor->pos.fy = (floor->pos.fy+newy)-cursor->yvel;
				}
				break;
		}
		cursor->yvel = 0;
		floor->yvel = 0;
	}
	cursor->gnded = (cursor->pos.fy == ((floor->d2-cursor->h)-newy)-cursor->yvel);
	#ifdef DEBUG
	printf("%u, %u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor->clx, cursor->cly, cursor->gnded, newy, cursor->pos.fx, cursor->pos.fy, cursor->xvel, cursor->yvel);
	printf("%u, %u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, newy, floor->pos.fx, floor->pos.fy, floor->xvel, floor->yvel);
	#endif
}

/* Axis Aligned Bounding Box Collision. */
void aabb(struct colis *cursor, struct colis *floor) {
	uint8_t coldir = get_coldir(0, AABB, -1, cursor, floor);
	uint8_t left   = (cursor->xvel > 0) && (!floor->xvel || floor->xvel != 0);
	uint8_t right  = (cursor->xvel < 0) && (!floor->xvel || floor->xvel != 0);
	uint8_t top    = (cursor->yvel > 0) && (!floor->yvel || floor->yvel != 0);
	uint8_t bottom = (cursor->yvel < 0) && (!floor->yvel || floor->yvel != 0);

	cursor->clx = (coldir & 0x03) == 0x03;
	cursor->cly = (coldir & 0x0C) == 0x0C;
	/* Is the Cursor is on the ground? */
	cursor->gnded = (cursor->b2 == floor->b1-cursor->yvel-1);
	if (cursor->clx) {
		if (left) {
			cursor->pos.x = (floor->pos.x-cursor->xvel)-cursor->w-1;
		} else if (!left && floor->xvel != 0) {
			if (!right && floor->xvel < 0) {
				cursor->pos.x = (floor->pos.x-cursor->xvel)-cursor->w-1;
			}
		}
		if (right) {
			cursor->pos.x = (floor->pos.x+floor->w)-cursor->xvel+1;
		} else if (!right && floor->xvel != 0) {
			if (!left && floor->xvel > 0) {
				cursor->pos.x = (floor->pos.x+floor->w)-cursor->xvel+1;
			}
		}
		cursor->xvel = 0;
		floor->xvel = 0;
	}
	if (cursor->cly) {
		if (top) {
			cursor->pos.y = (floor->pos.y-cursor->yvel)-cursor->h-1;
		} else if (!top && floor->yvel != 0) {
			if (!bottom && floor->yvel < 0) {
				cursor->pos.y = (floor->pos.y-cursor->yvel)-cursor->h-1;
			}
		}
		if (bottom) {
			cursor->pos.y = (floor->pos.y+floor->h)-cursor->yvel+1;
		} else if (!bottom && floor->yvel != 0) {
			if (!top && floor->yvel > 0) {
				cursor->pos.y = (floor->pos.y+floor->h)-cursor->yvel+1;
			}
		}
		cursor->yvel = 0;
		floor->yvel = 0;
	}
	#ifdef DEBUG
	printf("%u, %u\n%u\n%i, %i\n%07.06f, %07.06f\n", cursor->clx, cursor->cly, cursor->gnded, cursor->pos.x, cursor->pos.y, cursor->xvel, cursor->yvel);
	printf("%u, %u\n%u\n%i, %i\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, floor->pos.x, floor->pos.y, floor->xvel, floor->yvel);
	#endif
}

/* Floating point Axis Aligned Bounding Box Collision. */
void faabb(struct colis *cursor, struct colis *floor) {
	uint8_t coldir = get_coldir(1, AABB, -1, cursor, floor);
	uint8_t left   = (cursor->xvel > 0) && (!floor->xvel || floor->xvel != 0);
	uint8_t right  = (cursor->xvel < 0) && (!floor->xvel || floor->xvel != 0);
	uint8_t top    = (cursor->yvel > 0) && (!floor->yvel || floor->yvel != 0);
	uint8_t bottom = (cursor->yvel < 0) && (!floor->yvel || floor->yvel != 0);

	cursor->clx = (coldir & 0x03) == 0x03;
	cursor->cly = (coldir & 0x0C) == 0x0C;
	/* Is the Cursor is on the ground? */
	cursor->gnded = (cursor->d2 == floor->d1-cursor->yvel-1);
	if (cursor->clx) {
		if (left) {
			cursor->pos.fx = (floor->pos.fx-cursor->xvel)-cursor->w-1;
		} else if (!left && floor->xvel != 0) {
			if (!right && floor->xvel < 0) {
				cursor->pos.fx = (floor->pos.fx-cursor->xvel)-cursor->w-1;
			}
		}
		if (right) {
			cursor->pos.fx = (floor->pos.fx+floor->w)-cursor->xvel+1;
		} else if (!right && floor->xvel != 0) {
			if (!left && floor->xvel > 0) {
				cursor->pos.fx = (floor->pos.fx+floor->w)-cursor->xvel+1;
			}
		}
		cursor->xvel = 0;
		floor->xvel = 0;
	}
	if (cursor->cly) {
		if (top) {
			cursor->pos.fy = (floor->pos.fy-cursor->yvel)-cursor->h-1;
		} else if (!top && floor->yvel != 0) {
			if (!bottom && floor->yvel < 0) {
				cursor->pos.fy = (floor->pos.fy-cursor->yvel)-cursor->h-1;
			}
		}
		if (bottom) {
			cursor->pos.fy = (floor->pos.fy+floor->h)-cursor->yvel+1;
		} else if (!bottom && floor->yvel != 0) {
			if (!top && floor->yvel > 0) {
				cursor->pos.fy = (floor->pos.fy+floor->h)-cursor->yvel+1;
			}
		}
		cursor->yvel = 0;
		floor->yvel = 0;
	}
	#ifdef DEBUG
	printf("%u, %u\n%u\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor->clx, cursor->cly, cursor->gnded, cursor->pos.fx, cursor->pos.fy, cursor->xvel, cursor->yvel);
	printf("%u, %u\n%u\n%07.06f, %07.06f\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, floor->pos.fx, floor->pos.fy, floor->xvel, floor->yvel);
	#endif
}