-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgravity_cylclsn.cpp
More file actions
187 lines (145 loc) · 4.9 KB
/
gravity_cylclsn.cpp
File metadata and controls
187 lines (145 loc) · 4.9 KB
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 "gravity_actor_extension.h"
template<class T> requires(sizeof(T) == 4) [[gnu::always_inline]]
inline T GetScalarAt(const void* base, std::size_t index)
{
std::remove_const_t<T> res;
asm("ldr %[res], [%[base], %[index], lsl #2]" :
[res] "=r" (res) :
[base] "r" (base),
[index] "r" (index)
);
return res;
}
template<class T> requires(sizeof(T) == 4) [[gnu::always_inline]]
inline void SetScalarAt(void* base, std::size_t index, T val)
{
asm volatile("str %[val], [%[base], %[index], lsl #2]" ::
[val] "r" (val),
[base] "r" (base),
[index] "r" (index)
);
}
template<FixUR T> struct Fix30 : Fix<T, 30, Fix30> { using Fix<T, 30, Fix30>::Fix; };
using Fix30i = Fix30<int>;
// Assumes that u.Dot(w) > -0.5_f
static void MakeRotationBetween(Matrix3x3& res, const Vector3& u, const Vector3& w)
{
const Vector3 k = u.Cross(w);
const Matrix3x3 generator
{
0._f, -k.z, k.y,
k.z, 0._f, -k.x,
-k.y, k.x, 0._f
};
res = generator(generator);
const Fix30i d = {(Fix12i(Fix30i(1), as_raw) / (1._f + u.Dot(w))).val, as_raw};
for (int i = 0; i < 9; i++)
{
Fix12i a = {d * GetScalarAt<Fix30i>(&res, i), as_raw};
if (i & 3) // if not on the diagonal
a += GetScalarAt<Fix12i>(&generator, i);
else
a += 1._f;
SetScalarAt(&res, i, a);
}
}
// This will be allocated on the stack in the function at 02014aa8
struct CylClsnData
{
const Actor* fixedCylClsnOwner; // owner of the cylinder collider updated in the outer loop
const Matrix3x3* movedCylClsnGravity;
const Matrix3x3* fixedCylClsnGravity;
Vector3 movedCylClsnPos; // position of the cylinder collider updated in the inner loop
Matrix3x3 alignRotation; // used to align the cylinder colliders with each other
private:
static constexpr std::size_t ogAllocSize = 0x14;
static void Hooks();
};
static_assert(std::is_standard_layout_v<CylClsnData>);
[[gnu::naked, deprecated("This function is not meant to be called!")]]
void CylClsnData::Hooks() { asm volatile (R"(
@ Change the size of the stack allocation
nsub_02014aac:
sub r13, r13, %[customAllocSize]
b 0x02014ab0
nsub_02014abc:
nsub_02014f34:
addeq r13, r13, %[customAllocSize]
popeq {r4-r11, r15}
b 0x02014ac8
@ Get a pointer to the owner of the fixed cylinder collider before entering the inner loop
nsub_02014b08:
beq 0x02014f14
bl _ZN13ActorTreeNode4FindEj
str r0, [r13, %[actorPtrOffset]]
b 0x02014b0c
repl_02014b58: @ replaces a virtual call to CylinderClsn::GetPos
add r1, r13, %[ogAllocSize]
add r14, r14, #8
b AlignCylColliders
repl_02014f04:
str r1, [r8, #0x14]
repl_02014d70:
mov r0, r8
add r1, r13, %[ogAllocSize]
bl TransformPushback
b 0x02014f08
)"
::
[ogAllocSize] "I" (ogAllocSize),
[customAllocSize] "I" (ogAllocSize + sizeof(CylClsnData)),
[actorPtrOffset] "I" (ogAllocSize + offsetof(CylClsnData, fixedCylClsnOwner))
);}
// The only calls to GetPos in the updater function are at 0x02014ae0 and 0x02014b60
// The updater doesn't change any of the positions
extern "C" Vector3& AlignCylColliders(CylinderClsn& movedCylClsn, CylClsnData& data)
{
#ifdef GRAVITY_DEBUG_COUNTERS
extern unsigned cylClsnUpdateCounter;
++cylClsnUpdateCounter;
#endif
data.movedCylClsnPos = movedCylClsn.GetPos();
const Actor* movedCylClsnOwner = ActorTreeNode::Find(movedCylClsn.GetOwnerID());
const Actor* fixedCylClsnOwner = data.fixedCylClsnOwner;
if (fixedCylClsnOwner && movedCylClsnOwner)
{
const Vector3* p1 = &movedCylClsnOwner->pos;
data.movedCylClsnGravity = &ActorExtension::Get(*movedCylClsnOwner).GetGravityMatrix();
const Vector3* p2 = &fixedCylClsnOwner->pos;
data.fixedCylClsnGravity = &ActorExtension::Get(*fixedCylClsnOwner).GetGravityMatrix();
const Matrix3x3& g1 = *data.movedCylClsnGravity;
const Matrix3x3& g2 = *data.fixedCylClsnGravity;
MakeRotationBetween(data.alignRotation, g1.c1, (g1.c1 + g2.c1).Normalized());
const Matrix3x3& r = data.alignRotation;
data.movedCylClsnPos = g2.Transpose()(r(r(g1(data.movedCylClsnPos - *p1)) + *p1 - *p2)) + *p2;
}
else
data.movedCylClsnGravity = nullptr;
return data.movedCylClsnPos;
}
// Transform the pushback vector of the cylinder collider updated in the inner loop
extern "C" void TransformPushback(CylinderClsn& movedCylClsn, CylClsnData& data)
{
if (data.movedCylClsnGravity)
{
const Matrix3x3& g1 = *data.movedCylClsnGravity;
const Matrix3x3& g2 = *data.fixedCylClsnGravity;
const auto invR = data.alignRotation.Transpose();
movedCylClsn.pushback = g1.Transpose()(invR(invR(g2(movedCylClsn.pushback))));
}
}
// replaces Actor::UpdatePosWithOnlySpeed
Actor& nsub_02010d40(Actor& actor, const CylinderClsn* cylClsn)
{
auto& extension = ActorExtension::Get(actor);
Vector3 delta = actor.pos - extension.savedPos + actor.speed;
if (cylClsn)
{
delta.x += cylClsn->pushback.x;
delta.z += cylClsn->pushback.z;
}
delta *= extension.GetGravityMatrix();
extension.savedPos += delta;
actor.pos = extension.savedPos;
return actor;
}