Skip to content

Commit eeddb68

Browse files
committed
More xMath3 work
1 parent 3a7b91a commit eeddb68

File tree

5 files changed

+158
-62
lines changed

5 files changed

+158
-62
lines changed

src/SB/Core/x/xMath3.cpp

Lines changed: 138 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
#include <types.h>
55
#include <intrin.h>
66

7-
#include "iMath.h" // icos and isin
7+
#include "iMath.h"
8+
#include "xMath.h" // icos and isin
89
#include "xClimate.h" // xMat3x3Identity
910
#include "xMathInlines.h" // xasin, xatan2
1011
//#include "xVec3Inlines.h" // xVec3Init, imported, realized xClimate has a declaration as well though.
1112

12-
xVec3 g_O3 = { 0, 0, 0 };
13+
const xVec3 g_O3 = { 0, 0, 0 };
14+
const xQuat g_IQ = { 0.0f, 0.0f, 0.0f, 1.0f };
1315

1416
S32 xPointInBox(const xBox* b, const xVec3* p)
1517
{
@@ -248,32 +250,33 @@ void xMat4x3MoveLocalUp(xMat4x3* m, F32 mag)
248250
/* xMat3x3GetEuler (xMat3x3 const *, xVec3 *) */
249251
void xMat3x3GetEuler(const xMat3x3* m, xVec3* a)
250252
{
251-
F32 temp_f31;
252-
F32 var_f1;
253-
F32 var_f30;
253+
F32 yaw = -xasin(m->at.y);
254+
255+
F32 roll;
256+
F32 pitch;
254257

255-
temp_f31 = -xasin(m->at.y);
256-
if (temp_f31 < 1.5707964f)
258+
if (yaw < (PI / 2))
257259
{
258-
if (temp_f31 > -1.5707964f)
260+
if (yaw > -(PI / 2))
259261
{
260-
var_f30 = xatan2(m->at.x, m->at.z);
261-
var_f1 = xatan2(m->right.y, m->up.y);
262+
pitch = xatan2(m->at.x, m->at.z);
263+
roll = xatan2(m->right.y, m->up.y);
262264
}
263265
else
264266
{
265-
var_f30 = -xatan2(-m->up.x, m->up.x);
266-
var_f1 = 0.0f;
267+
pitch = -xatan2(-m->up.x, m->right.x);
268+
roll = 0.0f;
267269
}
268270
}
269271
else
270272
{
271-
var_f1 = 0.0f;
272-
var_f30 = xatan2(-m->up.x, m->right.x);
273+
pitch = xatan2(-m->up.x, m->right.x);
274+
roll = 0.0f;
273275
}
274-
a->x = var_f30;
275-
a->y = temp_f31;
276-
a->z = var_f1;
276+
277+
a->x = pitch;
278+
a->y = yaw;
279+
a->z = roll;
277280
}
278281

279282
/* xMat3x3Euler (xMat3x3 *, xVec3 const *) */
@@ -285,48 +288,31 @@ void xMat3x3Euler(xMat3x3* m, const xVec3* ypr)
285288
/* xQuatToMat (xQuat const *, xMat3x3 *) */
286289
void xQuatToMat(const xQuat* q, xMat3x3* m)
287290
{
288-
F32 temp_f10;
289-
F32 temp_f11;
290-
F32 temp_f12;
291-
F32 temp_f13;
292-
F32 temp_f1;
293-
F32 temp_f2;
294-
F32 temp_f3;
295-
F32 temp_f3_2;
296-
F32 temp_f4;
297-
F32 temp_f4_2;
298-
F32 temp_f5;
299-
F32 temp_f5_2;
300-
F32 temp_f6;
301-
F32 temp_f7;
302-
F32 temp_f8;
303-
F32 temp_f9;
291+
F32 tx = (2.0f * q->v.x);
292+
F32 ty = (2.0f * q->v.y);
293+
F32 tz = (2.0f * q->v.z);
294+
F32 tsx = tx * q->s;
295+
F32 tsy = ty * q->s;
296+
F32 tsz = tz * q->s;
297+
F32 txx = tx * q->v.x;
298+
F32 txy = ty * q->v.x;
299+
F32 txz = tz * q->v.x;
300+
F32 tyy = ty * q->v.y;
301+
F32 tyz = tz * q->v.y;
302+
F32 tzz = tz * q->v.z;
303+
304+
m->right.x = (1.0f - tyy) - tzz;
305+
m->right.y = txy - tsz;
306+
m->right.z = txz + tsy;
307+
308+
m->up.x = txy + tsz;
309+
m->up.y = (1.0f - tzz) - txx;
310+
m->up.z = tyz - tsx;
311+
312+
m->at.x = txz - tsy;
313+
m->at.y = tyz + tsx;
314+
m->at.z = (1.0f - txx) - tyy;
304315

305-
temp_f5 = q->v.y;
306-
temp_f1 = q->v.z;
307-
temp_f2 = 2.0f * temp_f5;
308-
temp_f4 = q->v.x;
309-
temp_f7 = 2.0f * temp_f1;
310-
temp_f3 = q->s;
311-
temp_f6 = 2.0f * temp_f4;
312-
temp_f12 = temp_f2 * temp_f5;
313-
temp_f13 = temp_f7 * temp_f1;
314-
temp_f9 = temp_f7 * temp_f3;
315-
temp_f10 = temp_f2 * temp_f4;
316-
temp_f8 = temp_f2 * temp_f3;
317-
temp_f11 = temp_f7 * temp_f4;
318-
m->right.x = (1.0f - temp_f12) - temp_f13;
319-
temp_f4_2 = temp_f6 * temp_f4;
320-
m->right.y = temp_f10 - temp_f9;
321-
temp_f3_2 = temp_f6 * temp_f3;
322-
temp_f5_2 = temp_f7 * temp_f5;
323-
m->right.z = temp_f11 + temp_f8;
324-
m->up.x = temp_f10 + temp_f9;
325-
m->up.y = (1.0f - temp_f13) - temp_f4_2;
326-
m->up.z = temp_f5_2 - temp_f3_2;
327-
m->at.x = temp_f11 - temp_f8;
328-
m->at.y = temp_f5_2 + temp_f3_2;
329-
m->at.z = (1.0f - temp_f4_2) - temp_f12;
330316
m->flags = 0;
331317
}
332318

@@ -404,6 +390,28 @@ void xQuatFromMat(xQuat* q, const xMat3x3* m)
404390
// }
405391
}
406392

393+
void xQuatFromAxisAngle(xQuat* q, const xVec3* a, F32 t)
394+
{
395+
F32 t_2;
396+
397+
if (t == 0.0f)
398+
{
399+
xQuatCopy(q, &g_IQ);
400+
}
401+
else
402+
{
403+
t_2 = isin(t * 0.5f);
404+
q->s = icos((t * 0.5f));
405+
xVec3SMul(&q->v, a, t_2);
406+
}
407+
}
408+
409+
void xQuatToAxisAngle(const xQuat* q, xVec3* a, F32* t)
410+
{
411+
*t = 2.0f * xacos(q->s);
412+
xVec3Normalize(a, &q->v);
413+
}
414+
407415
/* xQuatSlerp (xQuat *, xQuat const *, xQuat const *, float) */
408416
void xQuatSlerp(xQuat* q, const xQuat* a, const xQuat* b, F32 t)
409417
{
@@ -589,6 +597,74 @@ void xMat3x3ScaleC(xMat3x3* m, F32 x, F32 y, F32 z)
589597

590598
void xMat3x3RMulRotY(xMat3x3* o, const xMat3x3* m, F32 t)
591599
{
600+
F32 cos = icos(t);
601+
F32 sin = isin(t);
602+
if (o == m)
603+
{
604+
F32 temp = o->right.z;
605+
o->right.z = ((cos * o->right.z) - (sin * o->right.x));
606+
o->right.x = ((cos * o->right.x) + (sin * temp));
607+
608+
temp = o->up.z;
609+
o->up.z = ((cos * temp) - (sin * o->up.x));
610+
o->up.x = ((cos * o->up.x) + (sin * temp));
611+
612+
temp = o->at.z;
613+
o->at.z = ((cos * temp) - (sin * o->at.x));
614+
o->at.x = ((cos * o->at.x) + (sin * temp));
615+
}
616+
else
617+
{
618+
o->right.x = (cos * m->right.x + (sin * m->right.z));
619+
o->right.y = m->right.y;
620+
o->right.z = (cos * m->right.z - (sin * m->right.x));
621+
622+
o->up.x = (cos * m->up.x + (sin * m->up.z));
623+
o->up.y = m->up.y;
624+
o->up.z = (cos * m->up.z - (sin * m->up.x));
625+
626+
o->at.x = (cos * m->at.x + (sin * m->at.z));
627+
o->at.y = m->at.y;
628+
o->at.z = (cos * m->at.z - (sin * m->at.x));
629+
630+
o->flags = 0;
631+
}
632+
}
633+
634+
void xMat3x3Transpose(xMat3x3* o, const xMat3x3* m)
635+
{
636+
F32 temp;
637+
638+
if (o == m)
639+
{
640+
temp = o->right.y;
641+
o->right.y = o->up.x;
642+
o->up.x = temp;
643+
644+
temp = o->right.z;
645+
o->right.z = o->at.x;
646+
o->at.x = temp;
647+
648+
temp = o->up.z;
649+
o->up.z = o->at.y;
650+
o->at.y = temp;
651+
652+
return;
653+
}
654+
655+
o->right.x = m->right.x;
656+
o->right.y = m->up.x;
657+
o->right.z = m->at.x;
658+
659+
o->up.x = m->right.y;
660+
o->up.y = m->up.y;
661+
o->up.z = m->at.y;
662+
663+
o->at.x = m->right.z;
664+
o->at.y = m->up.z;
665+
o->at.z = m->at.z;
666+
667+
o->flags = 0;
592668
}
593669

594670
/* xMat3x3Mul (xMat3x3 *, xMat3x3 const *, xMat3x3 const *) */
@@ -674,6 +750,10 @@ void xBoxFromRay(xBox& box, const xRay3& ray)
674750
{
675751
}
676752

753+
// Equivalent: regalloc(?)
677754
void xMat3x3LMulVec(xVec3* o, const xMat3x3* m, const xVec3* v)
678755
{
756+
o->x = (v->x * m->right.x) + (v->y * m->right.y) + (v->z * m->right.z);
757+
o->y = (m->up.x * v->x) + (m->up.y * v->y) + (m->up.z * v->z);
758+
o->z = (m->at.x * v->x) + (m->at.y * v->y) + (m->at.z * v->z);
679759
}

src/SB/Core/x/xMath3.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef XMATH3_H
22
#define XMATH3_H
33

4+
#include "xMath.h"
5+
46
#include "xVec3.h"
57
#include "xVec3Inlines.h"
68

@@ -76,7 +78,8 @@ struct xLine3
7678

7779
struct xRay3;
7880

79-
extern xVec3 g_O3;
81+
extern const xQuat g_IQ;
82+
extern const xVec3 g_O3;
8083
extern xVec3 g_X3;
8184
extern xVec3 g_Y3;
8285
extern xVec3 g_Z3;
@@ -107,9 +110,22 @@ F32 xQuatGetAngle(const xQuat* q);
107110
void xQuatFromMat(xQuat* q, const xMat3x3* m);
108111
void xQuatSlerp(xQuat* q, const xQuat* a, const xQuat* b, F32 t);
109112
void xQuatConj(xQuat* o, const xQuat* q);
113+
void xQuatCopy(xQuat*, const xQuat*);
110114
void xMat3x3LookAt(xMat3x3* m, const xVec3* pos, const xVec3* at);
111115
F32 xMat3x3LookVec(xMat3x3* m, const xVec3* at);
112116
void xBoxInitBoundOBB(xBox* o, const xBox* b, const xMat4x3* m);
117+
118+
void xBoxUnion(xBox& a, const xBox& b, const xBox& c)
119+
{
120+
a.upper.x = MAX(b.upper.x, c.upper.x);
121+
a.upper.y = MAX(b.upper.y, c.upper.y);
122+
a.upper.z = MAX(b.upper.z, c.upper.z);
123+
124+
a.lower.x = MIN(b.lower.x, c.lower.x);
125+
a.lower.y = MIN(b.lower.y, c.lower.y);
126+
a.lower.z = MIN(b.lower.z, c.lower.z);
127+
}
128+
113129
void xMat3x3Scale(xMat3x3* m, const xVec3* s);
114130
void xMat3x3ScaleC(xMat3x3* m, F32 x, F32 y, F32 z);
115131
void xMat3x3RMulRotY(xMat3x3* o, const xMat3x3* m, F32 t);

src/SB/Game/zCamera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static F32 rewardTiltTime = 1.5f;
9494
static F32 rewardTiltAmount = -0.22f;
9595

9696
extern zGlobals globals;
97-
extern xVec3 g_O3;
97+
extern const xVec3 g_O3;
9898
extern F32 gSkipTimeFlythrough;
9999

100100
extern F32 zCamera_f_75_0; // 75.0

src/SB/Game/zNPCHazard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern S32 g_cnt_activehaz;
1717
extern NPCHazard* g_haz_uvAnimQue[27];
1818
extern RpAtomic* g_hazard_rawModel[30];
1919
extern xAnimTable* g_haz_animTable[30];
20-
extern xVec3 g_O3;
20+
extern const xVec3 g_O3;
2121
extern F32 _958_Hazard; // 0.0f
2222
extern F32 _959_Hazard; // 1.0f
2323
extern F32 _1041_Hazard; // -1.0f

src/SB/Game/zNPCTypeVillager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern char* g_strz_platanim[2];
2121
extern U32 g_hash_platanim[2];
2222
extern zParEmitter* g_pemit_aqualeak;
2323
extern xParEmitterCustomSettings g_parf_aqualeak;
24-
extern xVec3 g_O3;
24+
extern const xVec3 g_O3;
2525

2626
// Taken from zNPCTypeVillager.s
2727
// Defining these here makes the stringBase0 offsets match in the later functions.

0 commit comments

Comments
 (0)