From 322ca97c24f214d665cad512a70a696b489ecb52 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 28 Aug 2015 14:14:12 +0200 Subject: [PATCH] Review camera system --- src/camera.c | 59 +++++++++++++++++++++++++++++++++++++++------------- src/camera.h | 11 +++++----- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/camera.c b/src/camera.c index 627451fe..5c4d9ade 100644 --- a/src/camera.c +++ b/src/camera.c @@ -123,6 +123,7 @@ static int IsKeyDown(int key) { return 0; } //---------------------------------------------------------------------------------- // Select camera mode (multiple camera modes available) +// TODO: Review hardcoded values when changing modes... void SetCameraMode(int mode) { if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_FREE)) @@ -144,7 +145,7 @@ void SetCameraMode(int mode) cameraTargetDistance = 10; cameraAngle.x = 45 * DEG2RAD; cameraAngle.y = -40 * DEG2RAD; - internalCamera.target = (Vector3){ 0, 0, 0}; + internalCamera.target = (Vector3){ 0, 0, 0 }; ProcessCamera(&internalCamera, &internalCamera.position); ShowCursor(); @@ -154,7 +155,7 @@ void SetCameraMode(int mode) cameraTargetDistance = 10; cameraAngle.x = 225 * DEG2RAD; cameraAngle.y = -40 * DEG2RAD; - internalCamera.target = (Vector3){ 3, 0, 3}; + internalCamera.target = (Vector3){ 0, 0, 0}; ProcessCamera(&internalCamera, &internalCamera.position); } @@ -162,6 +163,7 @@ void SetCameraMode(int mode) } // Update camera with position +// TODO: I don't like how this function works right now... not clear enough... Camera UpdateCamera(Vector3 *position) { // Calculate camera @@ -170,41 +172,70 @@ Camera UpdateCamera(Vector3 *position) return internalCamera; } -void SetCameraMoveControls(int frontKey, int backKey, int leftKey, int rightKey, int upKey, int downKey) +// Set internal camera position +void SetCameraPosition(Vector3 position) { - cameraMoveControl[MOVE_FRONT] = frontKey; - cameraMoveControl[MOVE_LEFT] = leftKey; - cameraMoveControl[MOVE_BACK] = backKey; - cameraMoveControl[MOVE_RIGHT] = rightKey; - cameraMoveControl[MOVE_UP] = upKey; - cameraMoveControl[MOVE_DOWN] = downKey; + internalCamera.position = position; + + Vector3 v1 = internalCamera.position; + Vector3 v2 = internalCamera.target; + + float dx = v2.x - v1.x; + float dy = v2.y - v1.y; + float dz = v2.z - v1.z; + + cameraTargetDistance = sqrt(dx*dx + dy*dy + dz*dz); } -void SetCameraMouseSensitivity(float sensitivity) +// Set internal camera target +void SetCameraTarget(Vector3 target) { - mouseSensitivity = (sensitivity / 10000.0); + internalCamera.target = target; + + Vector3 v1 = internalCamera.position; + Vector3 v2 = internalCamera.target; + + float dx = v2.x - v1.x; + float dy = v2.y - v1.y; + float dz = v2.z - v1.z; + + cameraTargetDistance = sqrt(dx*dx + dy*dy + dz*dz); } +// Set camera pan key to combine with mouse movement (free camera) void SetCameraPanControl(int panKey) { panControlKey = panKey; } +// Set camera alt key to combine with mouse movement (free camera) void SetCameraAltControl(int altKey) { altControlKey = altKey; } +// Set camera smooth zoom key to combine with mouse (free camera) void SetCameraSmoothZoomControl(int szKey) { smoothZoomControlKey = szKey; } -void SetCameraTarget(Vector3 target) +// Set camera move controls (1st person and 3rd person cameras) +void SetCameraMoveControls(int frontKey, int backKey, int leftKey, int rightKey, int upKey, int downKey) { - internalCamera.target = target; + cameraMoveControl[MOVE_FRONT] = frontKey; + cameraMoveControl[MOVE_LEFT] = leftKey; + cameraMoveControl[MOVE_BACK] = backKey; + cameraMoveControl[MOVE_RIGHT] = rightKey; + cameraMoveControl[MOVE_UP] = upKey; + cameraMoveControl[MOVE_DOWN] = downKey; } +// Set camera mouse sensitivity (1st person and 3rd person cameras) +void SetCameraMouseSensitivity(float sensitivity) +{ + mouseSensitivity = (sensitivity / 10000.0); +} //---------------------------------------------------------------------------------- // Module specific Functions Definition @@ -298,7 +329,7 @@ static void ProcessCamera(Camera *camera, Vector3 *playerPosition) } else if ((camera->position.y < camera->target.y) && (camera->target.y > 0) && (mouseWheelMove > 0)) { - cameraTargetDistance -= (mouseWheelMove * CAMERA_SCROLL_SENSITIVITY); + cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY); if (cameraTargetDistance < FREE_CAMERA_DISTANCE_MIN_CLAMP) cameraTargetDistance = FREE_CAMERA_DISTANCE_MIN_CLAMP; } diff --git a/src/camera.h b/src/camera.h index 63d8f786..93929c2a 100644 --- a/src/camera.h +++ b/src/camera.h @@ -76,18 +76,19 @@ extern "C" { // Prevents name mangling of functions // Module Functions Declaration //---------------------------------------------------------------------------------- void SetCameraMode(int mode); // Set camera mode (multiple camera modes available) -Camera UpdateCamera(Vector3 *playerPosition); // Update camera and player position (1st person and 3rd person cameras) +Camera UpdateCamera(Vector3 *position); // Update camera and player position (1st person and 3rd person cameras) -void SetCameraMoveControls(int frontKey, int backKey, - int leftKey, int rightKey, - int upKey, int downKey); // Set camera move controls (1st person and 3rd person cameras) +void SetCameraPosition(Vector3 position); // Set internal camera position +void SetCameraTarget(Vector3 target); // Set internal camera target void SetCameraPanControl(int panKey); // Set camera pan key to combine with mouse movement (free camera) void SetCameraAltControl(int altKey); // Set camera alt key to combine with mouse movement (free camera) void SetCameraSmoothZoomControl(int szKey); // Set camera smooth zoom key to combine with mouse (free camera) +void SetCameraMoveControls(int frontKey, int backKey, + int leftKey, int rightKey, + int upKey, int downKey); // Set camera move controls (1st person and 3rd person cameras) void SetCameraMouseSensitivity(float sensitivity); // Set camera mouse sensitivity (1st person and 3rd person cameras) -void SetCameraTarget(Vector3 target); // Set internal camera target #ifdef __cplusplus }