summaryrefslogtreecommitdiff
path: root/src/d/col/c/c_m3d_g_sph.cpp
blob: a5292fe80b791239d2439d53e315b99cacc3b2b1 (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
#include "d/col/c/c_m3d_g_sph.h"

#include "m/m_vec.h"

cM3dGSph::cM3dGSph() {
    mRatio = 1.0f;
}

void cM3dGSph::SetC(const mVec3_c &c) {
    mCenter = c;
}

void cM3dGSph::Set(const mVec3_c *c, f32 r) {
    SetC(*c);
    SetR(r);
}

void cM3dGSph::SetR(f32 r) {
    if (r < 0.0f) {
        r = 0.0f;
    }
    mRadius = r;
}

void cM3dGSph::SetC(f32 x, f32 y, f32 z) {
    mVec3_c c(x, y, z);
    SetC(c);
}

bool cM3dGSph::Cross(cM3dGUnk &pUnk, mVec3_c *pOut) {
    return cM3d_Cross_UnkSph(pUnk, *this, pOut);
}

bool cM3dGSph::Cross(cM3dGUnk &pUnk, f32 *pOut) {
    return cM3d_Cross_UnkSph(pUnk, *this, pOut);
}

f32 cM3dGSph::GetYDist(f32 r) const {
    f32 b = mRadius * mRadius - (r - mCenter.y) * (r - mCenter.y);
    if (b < 0.0f) {
        return 0.0f;
    }
    return nw4r::math::FSqrt(b);
}

bool cM3dGSph::Cross(const cM3dGTri *pTri, f32 *p2, mVec3_c *p3) {
    return cM3d_Cross_SphTri(*this, *pTri, nullptr, p2, p3);
}

void cM3dGSph::Clamp(const mVec3_c &in, mVec3_c &out) const {
    f32 dist = EGG::Math<f32>::sqrt(VEC3DistSq(mCenter, in));

    if (dist < mRadius) {
        out = in;
    } else {
        f32 a = mRadius / dist;
        out = (in - mCenter) * a + mCenter;
    }
}

void cM3dGSph::SetRatio(f32 ratio) {
    mRatio = ratio;
}