/* Copyright (c) 2021 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "gamemath/Scalar.h" #include "gamemath/WalkCamera.h" #include #define stemobject_implementation WalkCamera stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(getPosition); stemobject_vtable_entry(getOrientation); stemobject_vtable_entry(getTransform); stemobject_vtable_entry(update); stemobject_vtable_end(); WalkCamera * WalkCamera_create(Vector3f position, Vector2f lookAngle) { stemobject_create_implementation(init, position, lookAngle) } bool WalkCamera_init(WalkCamera * self, Vector3f position, Vector2f lookAngle) { call_super(init, self); self->position = position; self->velocity = VECTOR3f_ZERO; self->lookAngle = lookAngle; self->moveInput = MoveInputState_allNeutral; self->lookUpLimit = M_PI * 0.5f; self->lookDownLimit = -M_PI * 0.5f; self->lookSensitivity = 1.0f / 256.0f; self->acceleration = 0.75f / 256.0f; self->deceleration = 2.0f / 256.0f; self->speedMax = 4.0f / 256.0f; return true; } void WalkCamera_dispose(WalkCamera * self) { call_super_virtual(dispose, self); } Vector3f WalkCamera_getPosition(WalkCamera * self) { return self->position; } Quaternionf WalkCamera_getOrientation(WalkCamera * self) { return Quaternionf_rotated(Quaternionf_fromAxisAngle(VECTOR3f_UP, -self->lookAngle.y), VECTOR3f_RIGHT, -self->lookAngle.x); } Matrix4x4f WalkCamera_getTransform(WalkCamera * self) { Matrix4x4f matrix = MATRIX4x4f_IDENTITY; Matrix4x4f_multiply(&matrix, Quaternionf_toMatrix(Quaternionf_rotated(Quaternionf_fromAxisAngle(VECTOR3f_RIGHT, self->lookAngle.x), VECTOR3f_UP, self->lookAngle.y))); Matrix4x4f_translate(&matrix, -self->position.x, -self->position.y, -self->position.z); return matrix; } static Vector3f decelerateVector(Vector3f value, Vector3f axis, float speed) { float dot = Vector3f_dot(value, axis); if (dot < 0.0f) { axis.x = -axis.x; axis.y = -axis.y; axis.z = -axis.z; dot = -dot; } if (speed < dot) { dot = speed; } return Vector3f_add(value, Vector3f_multiplyScalar(axis, -dot)); } static Vector3f accelerateVector(Vector3f value, Vector3f axis, float speed, float max) { float dot = Vector3f_dot(value, axis); if ((max < 0.0f && dot > max) || (max > 0.0f && dot < max)) { if ((max < 0.0f && speed + dot < max) || (max > 0.0f && speed + dot > max)) { speed = max - dot; } return Vector3f_add(value, Vector3f_multiplyScalar(axis, speed)); } return value; } void WalkCamera_update(WalkCamera * self) { Vector3f forward = VECTOR3f(-sinf(self->lookAngle.y), 0.0f, cosf(self->lookAngle.y)); Vector3f right = Vector3f_cross(VECTOR3f_UP, forward); MoveInput_direction moveDirectionX = MoveInput_mostRecentDirection(self->moveInput, MOVE_AXIS_X); MoveInput_direction moveDirectionY = MoveInput_mostRecentDirection(self->moveInput, MOVE_AXIS_Y); MoveInput_direction moveDirectionZ = MoveInput_mostRecentDirection(self->moveInput, MOVE_AXIS_Z); if (moveDirectionX == MOVE_DIRECTION_NEUTRAL) { self->velocity = decelerateVector(self->velocity, right, self->deceleration); } else { self->velocity = accelerateVector(self->velocity, right, self->acceleration * moveDirectionX, self->speedMax * moveDirectionX); } if (moveDirectionY == MOVE_DIRECTION_NEUTRAL) { self->velocity = decelerateVector(self->velocity, VECTOR3f_UP, self->deceleration); } else { self->velocity = accelerateVector(self->velocity, VECTOR3f_UP, self->acceleration * moveDirectionY, self->speedMax * moveDirectionY); } if (moveDirectionZ == MOVE_DIRECTION_NEUTRAL) { self->velocity = decelerateVector(self->velocity, forward, self->deceleration); } else { self->velocity = accelerateVector(self->velocity, forward, self->acceleration * moveDirectionZ, self->speedMax * moveDirectionZ); } self->position = Vector3f_add(self->position, self->velocity); } void WalkCamera_adjustLook(WalkCamera * self, float deltaX, float deltaY) { self->lookAngle.x = clampf(self->lookAngle.x + deltaY * self->lookSensitivity, self->lookDownLimit, self->lookUpLimit); self->lookAngle.y += deltaX * self->lookSensitivity; if (self->lookAngle.y <= -M_PI * 2.0) { self->lookAngle.y += M_PI * 2.0; } else if (self->lookAngle.y >= M_PI * 2.0) { self->lookAngle.y -= M_PI * 2.0; } } bool WalkCamera_startMoveInput(WalkCamera * self, MoveInput_axis axis, MoveInput_direction direction) { return MoveInput_start(&self->moveInput, axis, direction); } bool WalkCamera_stopMoveInput(WalkCamera * self, MoveInput_axis axis, MoveInput_direction direction) { return MoveInput_stop(&self->moveInput, axis, direction); }