Browse Source

Review camera system

raysan5 10 years ago
parent
commit
322ca97c24
2 changed files with 51 additions and 19 deletions
  1. 45 14
      src/camera.c
  2. 6 5
      src/camera.h

+ 45 - 14
src/camera.c

@@ -123,6 +123,7 @@ static int IsKeyDown(int key) { return 0; }
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------
 
 
 // Select camera mode (multiple camera modes available)
 // Select camera mode (multiple camera modes available)
+// TODO: Review hardcoded values when changing modes...
 void SetCameraMode(int mode)
 void SetCameraMode(int mode)
 {
 {
     if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_FREE))
     if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_FREE))
@@ -144,7 +145,7 @@ void SetCameraMode(int mode)
         cameraTargetDistance = 10;
         cameraTargetDistance = 10;
         cameraAngle.x = 45 * DEG2RAD;
         cameraAngle.x = 45 * DEG2RAD;
         cameraAngle.y = -40 * DEG2RAD;
         cameraAngle.y = -40 * DEG2RAD;
-        internalCamera.target = (Vector3){ 0, 0, 0};
+        internalCamera.target = (Vector3){ 0, 0, 0 };
         ProcessCamera(&internalCamera, &internalCamera.position);
         ProcessCamera(&internalCamera, &internalCamera.position);
         
         
         ShowCursor();
         ShowCursor();
@@ -154,7 +155,7 @@ void SetCameraMode(int mode)
         cameraTargetDistance = 10;
         cameraTargetDistance = 10;
         cameraAngle.x = 225 * DEG2RAD;
         cameraAngle.x = 225 * DEG2RAD;
         cameraAngle.y = -40 * DEG2RAD;
         cameraAngle.y = -40 * DEG2RAD;
-        internalCamera.target = (Vector3){ 3, 0, 3};
+        internalCamera.target = (Vector3){ 0, 0, 0};
         ProcessCamera(&internalCamera, &internalCamera.position);
         ProcessCamera(&internalCamera, &internalCamera.position);
     }
     }
 
 
@@ -162,6 +163,7 @@ void SetCameraMode(int mode)
 }
 }
 
 
 // Update camera with position
 // Update camera with position
+// TODO: I don't like how this function works right now... not clear enough...
 Camera UpdateCamera(Vector3 *position)
 Camera UpdateCamera(Vector3 *position)
 {
 {
     // Calculate camera
     // Calculate camera
@@ -170,41 +172,70 @@ Camera UpdateCamera(Vector3 *position)
     return internalCamera;
     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)
 void SetCameraPanControl(int panKey)
 {
 {
     panControlKey = panKey;
     panControlKey = panKey;
 }
 }
 
 
+// Set camera alt key to combine with mouse movement (free camera)
 void SetCameraAltControl(int altKey)
 void SetCameraAltControl(int altKey)
 {
 {
     altControlKey = altKey;
     altControlKey = altKey;
 }
 }
 
 
+// Set camera smooth zoom key to combine with mouse (free camera)
 void SetCameraSmoothZoomControl(int szKey)
 void SetCameraSmoothZoomControl(int szKey)
 {
 {
     smoothZoomControlKey = 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
 // 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))
             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;
                 if (cameraTargetDistance < FREE_CAMERA_DISTANCE_MIN_CLAMP) cameraTargetDistance = FREE_CAMERA_DISTANCE_MIN_CLAMP;
             }
             }
 
 

+ 6 - 5
src/camera.h

@@ -76,18 +76,19 @@ extern "C" {            // Prevents name mangling of functions
 // Module Functions Declaration
 // Module Functions Declaration
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------
 void SetCameraMode(int mode);                               // Set camera mode (multiple camera modes available)
 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 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 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 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 SetCameraMouseSensitivity(float sensitivity);          // Set camera mouse sensitivity (1st person and 3rd person cameras)
-void SetCameraTarget(Vector3 target);                       // Set internal camera target
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }