diff options
author | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-07-07 02:24:09 -0600 |
---|---|---|
committer | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-07-07 02:24:09 -0600 |
commit | a90e1987de75cfecc2693952625af8cce507ae95 (patch) | |
tree | 5a0c3b195db071563e028b839b5dbd34157ec546 /src/entity.c | |
parent | 028cf5d33d99274deea9567159a4eb07c13ef85c (diff) |
Added acceleration
Diffstat (limited to 'src/entity.c')
-rw-r--r-- | src/entity.c | 82 |
1 files changed, 67 insertions, 15 deletions
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); |