aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
blob: 25b8d510fa1061d25254c3434554eb15c601bf68 (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
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
#include "util.h"

AxisAngle AxisAngleIdentity() {
	return (AxisAngle){Vector3Zero(), 0.0};
}

float signum(float n) {
	if (n > 0.0)
		return 1.0;
	else if (n < 0.0)
		return -1.0;
	else
		return 0.0;
}

void printVector3(Vector3 v) {
	printf("%f %f %f\n", v.x, v.y, v.z);
}

void printVector2(Vector2 v) {
	printf("%f %f\n", v.x, v.y);
}

// Warning. Mostly chatgpt written.

static void projectTriangleOntoAxis(const Triangle3D triangle, const Vector3 axis, float* min, float* max) {
    float dot1 = Vector3DotProduct(triangle[0], axis);
    float dot2 = Vector3DotProduct(triangle[1], axis);
    float dot3 = Vector3DotProduct(triangle[2], axis);

    *min = fminf(fminf(dot1, dot2), dot3);
    *max = fmaxf(fmaxf(dot1, dot2), dot3);
}

static bool trianglesIntersectOnAxis(const Triangle3D triangleA, const Triangle3D triangleB, Vector3 axis) {
    float minA, maxA;
    float minB, maxB;

    projectTriangleOntoAxis(triangleA, axis, &minA, &maxA);
    projectTriangleOntoAxis(triangleB, axis, &minB, &maxB);

    return (minA <= maxB && maxA >= minB);
}

bool checkTriangleCollision3D(const Triangle3D triangleA, const Triangle3D triangleB, Vector3 normalA, Vector3 normalB) {
    // Test triangle normals
    if (!trianglesIntersectOnAxis(triangleA, triangleB, normalA))
        return false;

    if (!trianglesIntersectOnAxis(triangleA, triangleB, normalB))
        return false;

    // Test cross products of triangle edges
    Vector3 edgesA[3] = {
        Vector3Subtract(triangleA[1], triangleA[0]),
        Vector3Subtract(triangleA[2], triangleA[1]),
        Vector3Subtract(triangleA[0], triangleA[2])
    };

    Vector3 edgesB[3] = {
        Vector3Subtract(triangleB[1], triangleB[0]),
        Vector3Subtract(triangleB[2], triangleB[1]),
        Vector3Subtract(triangleB[0], triangleB[2])
    };

    for (int i = 0; i < 3; i++) {
        Vector3 axis = Vector3CrossProduct(normalA, edgesA[i]);
        if (!trianglesIntersectOnAxis(triangleA, triangleB, axis))
            return false;

        axis = Vector3CrossProduct(normalB, edgesB[i]);
        if (!trianglesIntersectOnAxis(triangleA, triangleB, axis))
            return false;
    }

    return true;
}

void copyTriangle3D(Triangle3D a, const Triangle3D b) {
	a[0] = b[0];
	a[1] = b[1];
	a[2] = b[2];
}

Vector3 getVertexAt(Mesh mesh, int vertexNum) {
    return (Vector3){
        mesh.vertices[vertexNum * 3],
        mesh.vertices[vertexNum * 3 + 1],
        mesh.vertices[vertexNum * 3 + 2]
    };
}

void drawModelWireframe(Model model, Vector3 position, Quaternion rotation, Color color) {
    // DrawModelWires doesn't work on the version of opengl that web assembly uses.
    // Instead I use this stupid peice of shit.

#ifdef PLATFORM_WEB
    for (int i = 0; i < model.meshCount; ++i) {
        Mesh mesh = model.meshes[i];

        // Loop through the triangles.
        for (int triangleNum = 0; triangleNum < mesh.triangleCount; ++triangleNum) {
            Triangle3D triangle = {
                getVertexAt(mesh, triangleNum * 3),
                getVertexAt(mesh, triangleNum * 3 + 1),
                getVertexAt(mesh, triangleNum * 3 + 2)
            };

            // Rotation and transform.
            for (int vertexNum = 0; vertexNum < 3; ++vertexNum) {
                triangle[vertexNum] = Vector3RotateByQuaternion(triangle[vertexNum], rotation);
                triangle[vertexNum] = Vector3Add(triangle[vertexNum], position);
            }

            // Draw.
            DrawLine3D(triangle[0], triangle[1], color);
            DrawLine3D(triangle[1], triangle[2], color);
            DrawLine3D(triangle[2], triangle[0], color);
        }
    }
#else
    model.transform = QuaternionToMatrix(rotation);
    DrawModelWires(model, position, 1.0, color);
#endif
}