NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
c_lib.cpp
1#include <game/cLib/c_lib.hpp>
2#include <lib/egg/math/eggMath.h>
3#include <lib/revolution/MTX/vec.h>
4#include <string.h>
5
6inline float calcDistance(const mVec3_c &a, const mVec3_c &b) {
7 float dx = a.x - b.x;
8 float dy = a.y - b.y;
9 float dz = a.z - b.z;
10 return EGG::Mathf::sqrt(dx * dx + dy * dy + dz * dz);
11}
12
13namespace cLib {
14
15void memSet(void *dst, int val, ulong size) {
16 memset(dst, val, size);
17}
18
19float addCalcPos(mVec3_c *currPos, const mVec3_c &targetPos, float ratio, float maxStep, float minStep) {
20 if (*currPos != targetPos) {
21 mVec3_c stepVector;
22 stepVector = *currPos - targetPos;
23 float distance = PSVECMag(stepVector);
24 if (distance < minStep) {
25 *currPos = targetPos;
26 } else {
27 stepVector *= ratio;
28 float step = distance * ratio;
29 if (!isZero(step)) {
30 if (step > maxStep) {
31 stepVector *= maxStep / step;
32 } else if (step < minStep) {
33 stepVector *= minStep / step;
34 }
35 *currPos -= stepVector;
36 } else {
37 *currPos = targetPos;
38 }
39 }
40 }
41
42 return calcDistance(*currPos, targetPos);
43}
44
45bool chasePos(mVec3_c *currentPos, const mVec3_c &targetPos, float step) {
46 if (step) {
47 mVec3_c offset = *currentPos - targetPos;
48 float distance = PSVECMag(offset);
49 if (isZero(distance) || distance <= step) {
50 *currentPos = targetPos;
51 return true;
52 }
53 *currentPos -= (step / distance) * offset;
54 } else if (*currentPos == targetPos) {
55 return true;
56 }
57 return false;
58}
59
60s16 targetAngleY(const mVec3_c &vec1, const mVec3_c &vec2) {
61 return cM::atan2s(vec2.x - vec1.x, vec2.z - vec1.z);
62}
63
64s16 targetAngleX(const mVec3_c &vec1, const mVec3_c &vec2) {
65 mVec3_c diff = vec2 - vec1;
66 return cM::atan2s(diff.y, diff.xzLen());
67}
68
69} // namespace cLib
A three-dimensional floating point vector.
Definition m_vec.hpp:122
Provides common utilities.
Definition c_lib.cpp:13
float addCalcPos(mVec3_c *currPos, const mVec3_c &targetPos, float ratio, float maxStep, float minStep)
Smoothly moves currentPos towards targetPos using proportional scaling.
Definition c_lib.cpp:19
bool isZero(float val)
Checks if a floating-point value is effectively zero within epsilon tolerance.
Definition c_lib.hpp:13
void memSet(void *dst, int val, ulong size)
See memset.
Definition c_lib.cpp:15
bool chasePos(mVec3_c *currentPos, const mVec3_c &targetPos, float step)
Moves currentPos towards targetPos by a fixed step amount.
Definition c_lib.cpp:45
s16 targetAngleY(const mVec3_c &vec1, const mVec3_c &vec2)
Calculates the yaw (Y-axis) orientation angle from an origin position to a target position.
Definition c_lib.cpp:60
s16 targetAngleX(const mVec3_c &vec1, const mVec3_c &vec2)
Calculates the pitch (X-axis) orientation angle from an origin position to a target position.
Definition c_lib.cpp:64
s16 atan2s(float sin, float cos)
Converts a sine and a cosine to an angle in units.
Definition c_math.cpp:164