aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornathansmithsmith <thenathansmithsmith@gmail.com>2023-07-07 02:24:09 -0600
committernathansmithsmith <thenathansmithsmith@gmail.com>2023-07-07 02:24:09 -0600
commita90e1987de75cfecc2693952625af8cce507ae95 (patch)
tree5a0c3b195db071563e028b839b5dbd34157ec546 /src
parent028cf5d33d99274deea9567159a4eb07c13ef85c (diff)
Added acceleration
Diffstat (limited to 'src')
-rw-r--r--src/entities/antifaShip.c12
-rw-r--r--src/entity.c82
-rw-r--r--src/entity.h32
-rw-r--r--src/gameScreen.c2
-rw-r--r--src/playerCamera.c2
-rw-r--r--src/util.c5
-rw-r--r--src/util.h13
7 files changed, 124 insertions, 24 deletions
diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c
index 2552d24..1944053 100644
--- a/src/entities/antifaShip.c
+++ b/src/entities/antifaShip.c
@@ -3,6 +3,14 @@
void initAntifaShip(Entity * entity) {
entity->model = LoadModel("/home/nathan/Documents/KillaFacsista/assets/antifaShip.obj");
+
+ entity->useAcceleration = true;
+ entity->acceleration = (EntityAcceleration){
+ .speedUp = 30.0,
+ .speedDown = 15,
+ .rotationUp = (Vector3){0.6, 0.6, 0.6},
+ .rotationDown = (Vector3){0.6, 0.6, 0.6}
+ };
}
void closeAntifaShip(Entity * entity) {
@@ -18,10 +26,10 @@ void drawAntifaShip(Game * game, Entity * entity, EntityId id) {
Vector3 stick = (Vector3){
GetGamepadAxisMovement(0, 1),
-GetGamepadAxisMovement(0, 0),
- GetGamepadAxisMovement(0, 2)
+ GetGamepadAxisMovement(0, 2) * 0.25
};
stick = Vector3Scale(stick, 0.5);
- entityJoystickControl(entity, stick, fabs(GetGamepadAxisMovement(0, 3) * 50.0 + 5.0));
+ entityJoystickControl(entity, stick, fabs(GetGamepadAxisMovement(0, 3) * 300.0));
}
diff --git a/src/entity.c b/src/entity.c
index 0b6be9d..c88c767 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -6,6 +6,32 @@ const EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT] = {
(EntityTypeInfo){initAntifaShip, closeAntifaShip, updateAntifaShip, drawAntifaShip}
};
+EntityVelocity entityVelocityIdentity() {
+ return (EntityVelocity){
+ .velocity = Vector3Zero(),
+ .angularVelocity = AxisAngleIdentity(),
+ .stick = Vector3Zero(),
+ .speed = 0
+ };
+}
+
+float accelerateValue(float value, float lastValue, float up, float down) {
+ if (value - lastValue >= up)
+ return lastValue + up;
+ if (lastValue - value >= down)
+ return lastValue - down;
+
+ return value;
+}
+
+Vector3 accelerateVector3(Vector3 value, Vector3 lastValue, Vector3 up, Vector3 down) {
+ return (Vector3){
+ accelerateValue(value.x, lastValue.x, up.x, down.x),
+ accelerateValue(value.y, lastValue.y, up.y, down.y),
+ accelerateValue(value.z, lastValue.z, up.z, down.z)
+ };
+}
+
Entity createEntity(EntityType type) {
EntityTypeInfo info = entityTypeInfo[type];
@@ -13,10 +39,10 @@ Entity createEntity(EntityType type) {
Entity entity = (Entity){
.type = type,
.position = Vector3Zero(),
- .angularVelocity = 0,
- .rotationAxis = Vector3Zero(),
- .velocity = Vector3Zero(),
.rotation = QuaternionIdentity(),
+ .velocity = entityVelocityIdentity(),
+ .lastVelocity = entityVelocityIdentity(),
+ .useAcceleration = false,
.updateCb = info.updateCb,
.drawCb = info.drawCb,
.data = NULL
@@ -48,9 +74,9 @@ void entityUpdatePosition(Entity * entity) {
float t = GetFrameTime();
Vector3 velocity = (Vector3){
- entity->velocity.x * t,
- entity->velocity.y * t,
- entity->velocity.z * t
+ entity->velocity.velocity.x * t,
+ entity->velocity.velocity.y * t,
+ entity->velocity.velocity.z * t
};
entity->position = Vector3Add(entity->position, velocity);
@@ -60,27 +86,53 @@ void entityUpdateRotation(Entity * entity) {
float t = GetFrameTime();
Quaternion angularRotation = QuaternionFromAxisAngle(
- entity->rotationAxis,
- entity->angularVelocity * t
+ entity->velocity.angularVelocity.axis,
+ entity->velocity.angularVelocity.angle * t
);
entity->rotation = QuaternionMultiply(entity->rotation, angularRotation);
}
void entityJoystickControl(Entity * entity, Vector3 stick, float speed) {
+ float s = speed;
+ Vector3 st = stick;
+ float t = GetFrameTime();
+
+ // Handle acceleration.
+ if (entity->useAcceleration) {
+ s = accelerateValue(
+ speed,
+ entity->lastVelocity.speed,
+ entity->acceleration.speedUp * t,
+ entity->acceleration.speedDown * t
+ );
+
+ st = accelerateVector3(
+ stick,
+ entity->lastVelocity.stick,
+ Vector3Scale(entity->acceleration.rotationUp, t),
+ Vector3Scale(entity->acceleration.rotationDown, t)
+ );
+ }
+
+ entity->velocity.stick = st;
+ entity->velocity.speed = s;
+ entity->lastVelocity = entity->velocity;
+
// Set angular velocity.
- Vector3 angularVelocity = Vector3Scale(stick, PI);
- entity->angularVelocity = Vector3Length(angularVelocity);
- entity->rotationAxis = stick;
+ Vector3 angularVelocity = Vector3Scale(st, PI);
+ entity->velocity.angularVelocity.angle = Vector3Length(angularVelocity);
+ entity->velocity.angularVelocity.axis = st;
+
entityUpdateRotation(entity);
// Set position.
Matrix m = QuaternionToMatrix(QuaternionInvert(entity->rotation));
- entity->velocity = (Vector3){
- m.m2 * speed,
- m.m6 * speed,
- m.m10 * speed,
+ entity->velocity.velocity = (Vector3){
+ m.m2 * s,
+ m.m6 * s,
+ m.m10 * s,
};
entityUpdatePosition(entity);
diff --git a/src/entity.h b/src/entity.h
index 0f9ec3d..7d27258 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -1,4 +1,5 @@
#include "gameCommon.h"
+#include "util.h"
#ifndef ENTITY_H
#define ENTITY_H
@@ -23,19 +24,40 @@ typedef int16_t EntityId; // Id in world.
typedef void (*EntityUpdateCb)(Game * game, Entity * entity, EntityId id);
typedef void (*EntityDrawCb)(Game * game, Entity * entity, EntityId id);
+// Acceleration indeed hehehe.
+typedef struct EntityAcceleration {
+ Vector3 rotationUp;
+ Vector3 rotationDown;
+ float speedUp;
+ float speedDown;
+} EntityAcceleration;
+
+float accelerateValue(float value, float lastValue, float up, float down);
+Vector3 accelerateVector3(Vector3 value, Vector3 lastValue, Vector3 up, Vector3 down);
+
+typedef struct EntityVelocity {
+ Vector3 velocity;
+ AxisAngle angularVelocity;
+ Vector3 stick; // Pilot control stick.
+ float speed; // Somewhat general use (:
+} EntityVelocity;
+
+EntityVelocity entityVelocityIdentity();
+
// This fucker is a entity.
typedef struct Entity {
EntityType type;
Model model;
Vector3 position;
- Vector3 velocity;
-
- float angularVelocity;
- Vector3 rotationAxis;
-
Quaternion rotation;
+ EntityVelocity velocity;
+ EntityVelocity lastVelocity;
+
+ bool useAcceleration;
+ EntityAcceleration acceleration;
+
EntityUpdateCb updateCb;
EntityDrawCb drawCb;
diff --git a/src/gameScreen.c b/src/gameScreen.c
index c62584a..893da0c 100644
--- a/src/gameScreen.c
+++ b/src/gameScreen.c
@@ -9,7 +9,7 @@ void updateGameScreen(Game * game) {
BeginMode3D(game->playerCamera);
- DrawGrid(100, 5.0);
+ DrawGrid(500, 25.0);
game->ship.drawCb(game, &game->ship, 0);
diff --git a/src/playerCamera.c b/src/playerCamera.c
index 4b9530f..d84d6d3 100644
--- a/src/playerCamera.c
+++ b/src/playerCamera.c
@@ -7,7 +7,7 @@ void initPlayerCamera(Camera3D * camera) {
.position = (Vector3){0.0, 15.0, -15.0},
.target = (Vector3){0.0, 0.0, 0.0},
.up = (Vector3){0.0, 1.0, 0.0},
- .fovy = 45.0,
+ .fovy = 90.0,
.projection = CAMERA_PERSPECTIVE
};
}
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..63cc9ab
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,5 @@
+#include "util.h"
+
+AxisAngle AxisAngleIdentity() {
+ return (AxisAngle){Vector3Zero(), 0.0};
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..94a9dd6
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,13 @@
+#include "gameCommon.h"
+
+#ifndef UTIL_H
+#define UTIL_H
+
+typedef struct AxisAngle {
+ Vector3 axis;
+ float angle;
+} AxisAngle;
+
+AxisAngle AxisAngleIdentity();
+
+#endif