/
object_collision.c
187 lines (165 loc) · 6.62 KB
/
object_collision.c
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
#include <PR/ultratypes.h>
#include "sm64.h"
#include "debug.h"
#include "interaction.h"
#include "mario.h"
#include "object_list_processor.h"
#include "spawn_object.h"
struct Object *debug_print_obj_collision(struct Object *a) {
struct Object *sp24;
UNUSED u8 filler[4];
s32 i;
for (i = 0; i < a->numCollidedObjs; i++) {
print_debug_top_down_objectinfo("ON", 0);
sp24 = a->collidedObjs[i];
if (sp24 != gMarioObject) {
return sp24;
}
}
return NULL;
}
s32 detect_object_hitbox_overlap(struct Object *a, struct Object *b) {
f32 sp3C = a->oPosY - a->hitboxDownOffset;
f32 sp38 = b->oPosY - b->hitboxDownOffset;
f32 dx = a->oPosX - b->oPosX;
UNUSED f32 sp30 = sp3C - sp38;
f32 dz = a->oPosZ - b->oPosZ;
f32 collisionRadius = a->hitboxRadius + b->hitboxRadius;
f32 distance = sqrtf(dx * dx + dz * dz);
if (collisionRadius > distance) {
f32 sp20 = a->hitboxHeight + sp3C;
f32 sp1C = b->hitboxHeight + sp38;
if (sp3C > sp1C) {
return 0;
}
if (sp20 < sp38) {
return 0;
}
if (a->numCollidedObjs >= 4) {
return 0;
}
if (b->numCollidedObjs >= 4) {
return 0;
}
a->collidedObjs[a->numCollidedObjs] = b;
b->collidedObjs[b->numCollidedObjs] = a;
a->collidedObjInteractTypes |= b->oInteractType;
b->collidedObjInteractTypes |= a->oInteractType;
a->numCollidedObjs++;
b->numCollidedObjs++;
return 1;
}
//! no return value
#ifdef AVOID_UB
return 0;
#endif
}
s32 detect_object_hurtbox_overlap(struct Object *a, struct Object *b) {
f32 sp3C = a->oPosY - a->hitboxDownOffset;
f32 sp38 = b->oPosY - b->hitboxDownOffset;
f32 sp34 = a->oPosX - b->oPosX;
UNUSED f32 sp30 = sp3C - sp38;
f32 sp2C = a->oPosZ - b->oPosZ;
f32 sp28 = a->hurtboxRadius + b->hurtboxRadius;
f32 sp24 = sqrtf(sp34 * sp34 + sp2C * sp2C);
if (a == gMarioObject) {
b->oInteractionSubtype |= INT_SUBTYPE_DELAY_INVINCIBILITY;
}
if (sp28 > sp24) {
f32 sp20 = a->hitboxHeight + sp3C;
f32 sp1C = b->hurtboxHeight + sp38;
if (sp3C > sp1C) {
return 0;
}
if (sp20 < sp38) {
return 0;
}
if (a == gMarioObject) {
b->oInteractionSubtype &= ~INT_SUBTYPE_DELAY_INVINCIBILITY;
}
return 1;
}
//! no return value
#ifdef AVOID_UB
return 0;
#endif
}
void clear_object_collision(struct Object *a) {
struct Object *sp4 = (struct Object *) a->header.next;
while (sp4 != a) {
sp4->numCollidedObjs = 0;
sp4->collidedObjInteractTypes = 0;
if (sp4->oIntangibleTimer > 0) {
sp4->oIntangibleTimer--;
}
sp4 = (struct Object *) sp4->header.next;
}
}
void check_collision_in_list(struct Object *a, struct Object *b, struct Object *c) {
if (a->oIntangibleTimer == 0) {
while (b != c) {
if (b->oIntangibleTimer == 0) {
if (detect_object_hitbox_overlap(a, b) && b->hurtboxRadius != 0.0f) {
detect_object_hurtbox_overlap(a, b);
}
}
b = (struct Object *) b->header.next;
}
}
}
void check_player_object_collision(void) {
struct Object *sp1C = (struct Object *) &gObjectLists[OBJ_LIST_PLAYER];
struct Object *sp18 = (struct Object *) sp1C->header.next;
while (sp18 != sp1C) {
check_collision_in_list(sp18, (struct Object *) sp18->header.next, sp1C);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_POLELIKE].next,
(struct Object *) &gObjectLists[OBJ_LIST_POLELIKE]);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_LEVEL].next,
(struct Object *) &gObjectLists[OBJ_LIST_LEVEL]);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_GENACTOR].next,
(struct Object *) &gObjectLists[OBJ_LIST_GENACTOR]);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_PUSHABLE].next,
(struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE]);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_SURFACE].next,
(struct Object *) &gObjectLists[OBJ_LIST_SURFACE]);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_DESTRUCTIVE].next,
(struct Object *) &gObjectLists[OBJ_LIST_DESTRUCTIVE]);
sp18 = (struct Object *) sp18->header.next;
}
}
void check_pushable_object_collision(void) {
struct Object *sp1C = (struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE];
struct Object *sp18 = (struct Object *) sp1C->header.next;
while (sp18 != sp1C) {
check_collision_in_list(sp18, (struct Object *) sp18->header.next, sp1C);
sp18 = (struct Object *) sp18->header.next;
}
}
void check_destructive_object_collision(void) {
struct Object *sp1C = (struct Object *) &gObjectLists[OBJ_LIST_DESTRUCTIVE];
struct Object *sp18 = (struct Object *) sp1C->header.next;
while (sp18 != sp1C) {
if (sp18->oDistanceToMario < 2000.0f && !(sp18->activeFlags & ACTIVE_FLAG_UNK9)) {
check_collision_in_list(sp18, (struct Object *) sp18->header.next, sp1C);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_GENACTOR].next,
(struct Object *) &gObjectLists[OBJ_LIST_GENACTOR]);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_PUSHABLE].next,
(struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE]);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_SURFACE].next,
(struct Object *) &gObjectLists[OBJ_LIST_SURFACE]);
}
sp18 = (struct Object *) sp18->header.next;
}
}
void detect_object_collisions(void) {
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_POLELIKE]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_PLAYER]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_GENACTOR]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_LEVEL]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_SURFACE]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_DESTRUCTIVE]);
check_player_object_collision();
check_destructive_object_collision();
check_pushable_object_collision();
}