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
|
#include "entity.h"
#include "entities/antifaShip.h"
// This fucker is used for creating entities.
const EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT] = {
(EntityTypeInfo){initAntifaShip, closeAntifaShip, updateAntifaShip, drawAntifaShip}
};
Entity createEntity(EntityType type) {
EntityTypeInfo info = entityTypeInfo[type];
// Set defaults.
Entity entity = (Entity){
.type = type,
.position = Vector3Zero(),
.angularVelocity = 0,
.rotationAxis = Vector3Zero(),
.velocity = Vector3Zero(),
.rotation = QuaternionIdentity(),
.updateCb = info.updateCb,
.drawCb = info.drawCb,
.data = NULL
};
// Init.
info.initCb(&entity);
return entity;
}
void closeEntity(Entity * entity) {
entityTypeInfo[entity->type].closeCb(entity);
}
// Basic wireframe drawing.
void entityDraw(Entity * entity) {
entity->model.transform = QuaternionToMatrix(entity->rotation);
DrawModelWires(
entity->model,
entity->position,
1,
GREEN
);
}
void entityUpdatePosition(Entity * entity) {
float t = GetFrameTime();
Vector3 velocity = (Vector3){
entity->velocity.x * t,
entity->velocity.y * t,
entity->velocity.z * t
};
entity->position = Vector3Add(entity->position, velocity);
}
void entityUpdateRotation(Entity * entity) {
float t = GetFrameTime();
Quaternion angularRotation = QuaternionFromAxisAngle(
entity->rotationAxis,
entity->angularVelocity * t
);
entity->rotation = QuaternionMultiply(entity->rotation, angularRotation);
}
void entityJoystickControl(Entity * entity, Vector3 stick, float speed) {
// Set angular velocity.
Vector3 angularVelocity = Vector3Scale(stick, PI);
entity->angularVelocity = Vector3Length(angularVelocity);
entity->rotationAxis = stick;
entityUpdateRotation(entity);
// Set position.
Matrix m = QuaternionToMatrix(QuaternionInvert(entity->rotation));
entity->velocity = (Vector3){
m.m2 * speed,
m.m6 * speed,
m.m10 * speed,
};
entityUpdatePosition(entity);
}
|