2015-05-05 00:46:31 +03:00
|
|
|
/**********************************************************************************************
|
|
|
|
*
|
2015-07-29 22:42:11 +03:00
|
|
|
* raylib Camera System - Camera Modes Setup and Control Functions
|
2015-05-05 00:46:31 +03:00
|
|
|
*
|
2016-01-28 12:03:37 +03:00
|
|
|
* Copyright (c) 2015 Marc Palau and Ramon Santamaria (@raysan5)
|
2015-05-05 00:46:31 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
//#define CAMERA_STANDALONE // NOTE: To use the camera module as standalone lib, just uncomment this line
|
|
|
|
// NOTE: ProcessCamera() should be reviewed to adapt inputs to other systems
|
|
|
|
|
|
|
|
#if defined(CAMERA_STANDALONE)
|
|
|
|
#include "camera.h"
|
|
|
|
#else
|
|
|
|
#include "raylib.h"
|
|
|
|
#endif
|
2015-07-13 19:17:05 +03:00
|
|
|
|
2015-05-05 00:46:31 +03:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// CAMERA_GENERIC
|
2016-02-03 19:45:28 +03:00
|
|
|
#define CAMERA_SCROLL_SENSITIVITY 1.5f
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// FREE_CAMERA
|
2016-02-03 19:45:28 +03:00
|
|
|
#define FREE_CAMERA_MOUSE_SENSITIVITY 0.01f
|
|
|
|
#define FREE_CAMERA_DISTANCE_MIN_CLAMP 0.3f
|
|
|
|
#define FREE_CAMERA_DISTANCE_MAX_CLAMP 120.0f
|
|
|
|
#define FREE_CAMERA_MIN_CLAMP 85.0f
|
|
|
|
#define FREE_CAMERA_MAX_CLAMP -85.0f
|
|
|
|
#define FREE_CAMERA_SMOOTH_ZOOM_SENSITIVITY 0.05f
|
|
|
|
#define FREE_CAMERA_PANNING_DIVIDER 5.1f
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// ORBITAL_CAMERA
|
2016-02-03 19:45:28 +03:00
|
|
|
#define ORBITAL_CAMERA_SPEED 0.01f
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// FIRST_PERSON
|
2016-02-03 19:45:28 +03:00
|
|
|
//#define FIRST_PERSON_MOUSE_SENSITIVITY 0.003f
|
|
|
|
#define FIRST_PERSON_FOCUS_DISTANCE 25.0f
|
|
|
|
#define FIRST_PERSON_MIN_CLAMP 85.0f
|
|
|
|
#define FIRST_PERSON_MAX_CLAMP -85.0f
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
#define FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER 5.0f
|
|
|
|
#define FIRST_PERSON_STEP_DIVIDER 30.0f
|
|
|
|
#define FIRST_PERSON_WAVING_DIVIDER 200.0f
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
#define FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION 0.85f
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// THIRD_PERSON
|
2016-02-03 19:45:28 +03:00
|
|
|
//#define THIRD_PERSON_MOUSE_SENSITIVITY 0.003f
|
|
|
|
#define THIRD_PERSON_DISTANCE_CLAMP 1.2f
|
|
|
|
#define THIRD_PERSON_MIN_CLAMP 5.0f
|
|
|
|
#define THIRD_PERSON_MAX_CLAMP -85.0f
|
|
|
|
#define THIRD_PERSON_OFFSET (Vector3){ 0.4f, 0.0f, 0.0f }
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// PLAYER (used by camera)
|
2016-02-03 19:45:28 +03:00
|
|
|
#define PLAYER_WIDTH 0.4f
|
|
|
|
#define PLAYER_HEIGHT 0.9f
|
|
|
|
#define PLAYER_DEPTH 0.4f
|
|
|
|
#define PLAYER_MOVEMENT_DIVIDER 20.0f
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Camera move modes (first person and third person cameras)
|
|
|
|
typedef enum { MOVE_FRONT = 0, MOVE_LEFT, MOVE_BACK, MOVE_RIGHT, MOVE_UP, MOVE_DOWN } CameraMove;
|
|
|
|
|
2015-05-05 00:46:31 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-02-03 19:45:28 +03:00
|
|
|
static Camera internalCamera = {{ 2.0f, 0.0f, 2.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
|
|
|
|
static Vector2 cameraAngle = { 0.0f, 0.0f };
|
2015-08-29 21:20:09 +03:00
|
|
|
static float cameraTargetDistance = 5.0f;
|
2016-02-03 19:45:28 +03:00
|
|
|
static Vector2 cameraMousePosition = { 0.0f, 0.0f };
|
|
|
|
static Vector2 cameraMouseVariation = { 0.0f, 0.0f };
|
2015-08-29 21:20:09 +03:00
|
|
|
static float mouseSensitivity = 0.003f;
|
2015-07-29 22:42:11 +03:00
|
|
|
static int cameraMoveControl[6] = { 'W', 'A', 'S', 'D', 'E', 'Q' };
|
|
|
|
static int cameraMoveCounter = 0;
|
|
|
|
static int cameraUseGravity = 1;
|
|
|
|
static int panControlKey = 2; // raylib: MOUSE_MIDDLE_BUTTON
|
|
|
|
static int altControlKey = 342; // raylib: KEY_LEFT_ALT
|
|
|
|
static int smoothZoomControlKey = 341; // raylib: KEY_LEFT_CONTROL
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
static int cameraMode = CAMERA_CUSTOM;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
static void ProcessCamera(Camera *camera, Vector3 *playerPosition);
|
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
#if defined(CAMERA_STANDALONE)
|
|
|
|
// NOTE: Camera controls depend on some raylib input functions
|
|
|
|
// TODO: Set your own input functions (used in ProcessCamera())
|
2016-02-03 19:45:28 +03:00
|
|
|
static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; }
|
2015-07-29 22:42:11 +03:00
|
|
|
static void SetMousePosition(Vector2 pos) {}
|
|
|
|
static int IsMouseButtonDown(int button) { return 0;}
|
|
|
|
static int GetMouseWheelMove() { return 0; }
|
|
|
|
static int GetScreenWidth() { return 1280; }
|
|
|
|
static int GetScreenHeight() { return 720; }
|
|
|
|
static void ShowCursor() {}
|
|
|
|
static void HideCursor() {}
|
|
|
|
static int IsKeyDown(int key) { return 0; }
|
|
|
|
#endif
|
|
|
|
|
2015-05-05 00:46:31 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Select camera mode (multiple camera modes available)
|
2015-08-28 15:14:12 +03:00
|
|
|
// TODO: Review hardcoded values when changing modes...
|
2015-05-05 00:46:31 +03:00
|
|
|
void SetCameraMode(int mode)
|
|
|
|
{
|
|
|
|
if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_FREE))
|
|
|
|
{
|
|
|
|
cameraMode = CAMERA_THIRD_PERSON;
|
2016-02-03 19:45:28 +03:00
|
|
|
cameraTargetDistance = 5.0f;
|
|
|
|
cameraAngle.y = -40*DEG2RAD;
|
2015-05-05 00:46:31 +03:00
|
|
|
ProcessCamera(&internalCamera, &internalCamera.position);
|
|
|
|
}
|
|
|
|
else if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_ORBITAL))
|
|
|
|
{
|
|
|
|
cameraMode = CAMERA_THIRD_PERSON;
|
2016-02-03 19:45:28 +03:00
|
|
|
cameraTargetDistance = 5.0f;
|
|
|
|
cameraAngle.y = -40*DEG2RAD;
|
2015-05-05 00:46:31 +03:00
|
|
|
ProcessCamera(&internalCamera, &internalCamera.position);
|
|
|
|
}
|
|
|
|
else if ((cameraMode == CAMERA_CUSTOM) && (mode == CAMERA_FREE))
|
|
|
|
{
|
2016-02-03 19:45:28 +03:00
|
|
|
cameraTargetDistance = 10.0f;
|
|
|
|
cameraAngle.x = 45*DEG2RAD;
|
|
|
|
cameraAngle.y = -40*DEG2RAD;
|
|
|
|
internalCamera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
2015-05-05 00:46:31 +03:00
|
|
|
ProcessCamera(&internalCamera, &internalCamera.position);
|
2015-08-27 17:09:44 +03:00
|
|
|
|
|
|
|
ShowCursor();
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
else if ((cameraMode == CAMERA_CUSTOM) && (mode == CAMERA_ORBITAL))
|
|
|
|
{
|
2016-02-03 19:45:28 +03:00
|
|
|
cameraTargetDistance = 10.0f;
|
|
|
|
cameraAngle.x = 225*DEG2RAD;
|
|
|
|
cameraAngle.y = -40*DEG2RAD;
|
|
|
|
internalCamera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
2015-05-05 00:46:31 +03:00
|
|
|
ProcessCamera(&internalCamera, &internalCamera.position);
|
|
|
|
}
|
|
|
|
|
|
|
|
cameraMode = mode;
|
|
|
|
}
|
|
|
|
|
2015-08-30 18:46:19 +03:00
|
|
|
// Update camera (player position is ignored)
|
|
|
|
void UpdateCamera(Camera *camera)
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2016-02-03 19:45:28 +03:00
|
|
|
Vector3 position = { 0.0f, 0.0f, 0.0f };
|
2015-08-30 18:46:19 +03:00
|
|
|
|
|
|
|
// Process internal camera and player position (if required)
|
|
|
|
if (cameraMode != CAMERA_CUSTOM) ProcessCamera(&internalCamera, &position);
|
|
|
|
|
|
|
|
*camera = internalCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update camera and player position (1st person and 3rd person cameras)
|
|
|
|
void UpdateCameraPlayer(Camera *camera, Vector3 *position)
|
|
|
|
{
|
|
|
|
// Process internal camera and player position (if required)
|
2015-05-05 00:46:31 +03:00
|
|
|
if (cameraMode != CAMERA_CUSTOM) ProcessCamera(&internalCamera, position);
|
|
|
|
|
2015-08-30 18:46:19 +03:00
|
|
|
*camera = internalCamera;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:14:12 +03:00
|
|
|
// Set internal camera position
|
|
|
|
void SetCameraPosition(Vector3 position)
|
2015-07-05 19:19:18 +03:00
|
|
|
{
|
2015-08-28 15:14:12 +03:00
|
|
|
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);
|
2015-07-05 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:14:12 +03:00
|
|
|
// Set internal camera target
|
|
|
|
void SetCameraTarget(Vector3 target)
|
2015-07-05 19:19:18 +03:00
|
|
|
{
|
2015-08-28 15:14:12 +03:00
|
|
|
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);
|
2015-07-05 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:14:12 +03:00
|
|
|
// Set camera pan key to combine with mouse movement (free camera)
|
2015-07-29 22:42:11 +03:00
|
|
|
void SetCameraPanControl(int panKey)
|
2015-07-05 19:19:18 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
panControlKey = panKey;
|
2015-07-05 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:14:12 +03:00
|
|
|
// Set camera alt key to combine with mouse movement (free camera)
|
2015-07-29 22:42:11 +03:00
|
|
|
void SetCameraAltControl(int altKey)
|
2015-07-05 19:19:18 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
altControlKey = altKey;
|
2015-07-05 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:14:12 +03:00
|
|
|
// Set camera smooth zoom key to combine with mouse (free camera)
|
2015-07-29 22:42:11 +03:00
|
|
|
void SetCameraSmoothZoomControl(int szKey)
|
2015-07-05 19:19:18 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
smoothZoomControlKey = szKey;
|
2015-07-05 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:14:12 +03:00
|
|
|
// Set camera move controls (1st person and 3rd person cameras)
|
|
|
|
void SetCameraMoveControls(int frontKey, int backKey, int leftKey, int rightKey, int upKey, int downKey)
|
2015-07-05 19:19:18 +03:00
|
|
|
{
|
2015-08-28 15:14:12 +03:00
|
|
|
cameraMoveControl[MOVE_FRONT] = frontKey;
|
|
|
|
cameraMoveControl[MOVE_LEFT] = leftKey;
|
|
|
|
cameraMoveControl[MOVE_BACK] = backKey;
|
|
|
|
cameraMoveControl[MOVE_RIGHT] = rightKey;
|
|
|
|
cameraMoveControl[MOVE_UP] = upKey;
|
|
|
|
cameraMoveControl[MOVE_DOWN] = downKey;
|
2015-07-05 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:14:12 +03:00
|
|
|
// Set camera mouse sensitivity (1st person and 3rd person cameras)
|
|
|
|
void SetCameraMouseSensitivity(float sensitivity)
|
|
|
|
{
|
2016-02-03 19:45:28 +03:00
|
|
|
mouseSensitivity = (sensitivity/10000.0f);
|
2015-08-28 15:14:12 +03:00
|
|
|
}
|
2015-07-05 19:19:18 +03:00
|
|
|
|
2015-05-05 00:46:31 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Process desired camera mode and controls
|
2015-07-29 22:42:11 +03:00
|
|
|
// NOTE: Camera controls depend on some raylib functions:
|
|
|
|
// Mouse: GetMousePosition(), SetMousePosition(), IsMouseButtonDown(), GetMouseWheelMove()
|
|
|
|
// System: GetScreenWidth(), GetScreenHeight(), ShowCursor(), HideCursor()
|
|
|
|
// Keys: IsKeyDown()
|
2015-05-05 00:46:31 +03:00
|
|
|
static void ProcessCamera(Camera *camera, Vector3 *playerPosition)
|
|
|
|
{
|
|
|
|
// Mouse movement detection
|
2015-07-29 22:42:11 +03:00
|
|
|
Vector2 mousePosition = GetMousePosition();
|
|
|
|
int mouseWheelMove = GetMouseWheelMove();
|
|
|
|
int panKey = IsMouseButtonDown(panControlKey); // bool value
|
|
|
|
|
|
|
|
int screenWidth = GetScreenWidth();
|
|
|
|
int screenHeight = GetScreenHeight();
|
|
|
|
|
2015-06-17 20:56:52 +03:00
|
|
|
if ((cameraMode != CAMERA_FREE) && (cameraMode != CAMERA_ORBITAL))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
|
|
|
HideCursor();
|
2015-07-29 22:42:11 +03:00
|
|
|
|
|
|
|
if (mousePosition.x < screenHeight/3) SetMousePosition((Vector2){ screenWidth - screenHeight/3, mousePosition.y});
|
|
|
|
else if (mousePosition.y < screenHeight/3) SetMousePosition((Vector2){ mousePosition.x, screenHeight - screenHeight/3});
|
|
|
|
else if (mousePosition.x > screenWidth - screenHeight/3) SetMousePosition((Vector2) { screenHeight/3, mousePosition.y});
|
|
|
|
else if (mousePosition.y > screenHeight - screenHeight/3) SetMousePosition((Vector2){ mousePosition.x, screenHeight/3});
|
2015-05-05 00:46:31 +03:00
|
|
|
else
|
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
cameraMouseVariation.x = mousePosition.x - cameraMousePosition.x;
|
|
|
|
cameraMouseVariation.y = mousePosition.y - cameraMousePosition.y;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ShowCursor();
|
2015-07-29 22:42:11 +03:00
|
|
|
|
|
|
|
cameraMouseVariation.x = mousePosition.x - cameraMousePosition.x;
|
|
|
|
cameraMouseVariation.y = mousePosition.y - cameraMousePosition.y;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 21:20:09 +03:00
|
|
|
// NOTE: We GetMousePosition() again because it can be modified by a previous SetMousePosition() call
|
|
|
|
// If using directly mousePosition variable we have problems on CAMERA_FIRST_PERSON and CAMERA_THIRD_PERSON
|
|
|
|
cameraMousePosition = GetMousePosition();
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Support for multiple automatic camera modes
|
|
|
|
switch (cameraMode)
|
|
|
|
{
|
|
|
|
case CAMERA_FREE:
|
|
|
|
{
|
|
|
|
// Camera zoom
|
2015-07-29 22:42:11 +03:00
|
|
|
if ((cameraTargetDistance < FREE_CAMERA_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
if (cameraTargetDistance > FREE_CAMERA_DISTANCE_MAX_CLAMP) cameraTargetDistance = FREE_CAMERA_DISTANCE_MAX_CLAMP;
|
|
|
|
}
|
|
|
|
// Camera looking down
|
2015-07-29 22:42:11 +03:00
|
|
|
else if ((camera->position.y > camera->target.y) && (cameraTargetDistance == FREE_CAMERA_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
else if ((camera->position.y > camera->target.y) && (camera->target.y >= 0))
|
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-01-08 04:53:26 +03:00
|
|
|
// if (camera->target.y < 0) camera->target.y = -0.001;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
2015-07-29 22:42:11 +03:00
|
|
|
else if ((camera->position.y > camera->target.y) && (camera->target.y < 0) && (mouseWheelMove > 0))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
|
2015-05-05 00:46:31 +03:00
|
|
|
if (cameraTargetDistance < FREE_CAMERA_DISTANCE_MIN_CLAMP) cameraTargetDistance = FREE_CAMERA_DISTANCE_MIN_CLAMP;
|
|
|
|
}
|
|
|
|
// Camera looking up
|
2015-07-29 22:42:11 +03:00
|
|
|
else if ((camera->position.y < camera->target.y) && (cameraTargetDistance == FREE_CAMERA_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
else if ((camera->position.y < camera->target.y) && (camera->target.y <= 0))
|
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
|
|
|
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-01-08 04:53:26 +03:00
|
|
|
// if (camera->target.y > 0) camera->target.y = 0.001;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
2015-07-29 22:42:11 +03:00
|
|
|
else if ((camera->position.y < camera->target.y) && (camera->target.y > 0) && (mouseWheelMove > 0))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-08-28 15:14:12 +03:00
|
|
|
cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
|
2015-05-05 00:46:31 +03:00
|
|
|
if (cameraTargetDistance < FREE_CAMERA_DISTANCE_MIN_CLAMP) cameraTargetDistance = FREE_CAMERA_DISTANCE_MIN_CLAMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inputs
|
2015-07-29 22:42:11 +03:00
|
|
|
if (IsKeyDown(altControlKey))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
if (IsKeyDown(smoothZoomControlKey))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
|
|
|
// Camera smooth zoom
|
2015-07-29 22:42:11 +03:00
|
|
|
if (panKey) cameraTargetDistance += (cameraMouseVariation.y*FREE_CAMERA_SMOOTH_ZOOM_SENSITIVITY);
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
// Camera orientation calculation
|
2015-07-29 22:42:11 +03:00
|
|
|
else if (panKey)
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
|
|
|
// Camera orientation calculation
|
|
|
|
// Get the mouse sensitivity
|
2015-07-29 22:42:11 +03:00
|
|
|
cameraAngle.x += cameraMouseVariation.x*-FREE_CAMERA_MOUSE_SENSITIVITY;
|
|
|
|
cameraAngle.y += cameraMouseVariation.y*-FREE_CAMERA_MOUSE_SENSITIVITY;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Angle clamp
|
2015-07-29 22:42:11 +03:00
|
|
|
if (cameraAngle.y > FREE_CAMERA_MIN_CLAMP*DEG2RAD) cameraAngle.y = FREE_CAMERA_MIN_CLAMP*DEG2RAD;
|
|
|
|
else if (cameraAngle.y < FREE_CAMERA_MAX_CLAMP*DEG2RAD) cameraAngle.y = FREE_CAMERA_MAX_CLAMP*DEG2RAD;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Paning
|
2015-07-29 22:42:11 +03:00
|
|
|
else if (panKey)
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->target.x += ((cameraMouseVariation.x*-FREE_CAMERA_MOUSE_SENSITIVITY)*cos(cameraAngle.x) + (cameraMouseVariation.y*FREE_CAMERA_MOUSE_SENSITIVITY)*sin(cameraAngle.x)*sin(cameraAngle.y))*(cameraTargetDistance/FREE_CAMERA_PANNING_DIVIDER);
|
|
|
|
camera->target.y += ((cameraMouseVariation.y*FREE_CAMERA_MOUSE_SENSITIVITY)*cos(cameraAngle.y))*(cameraTargetDistance/FREE_CAMERA_PANNING_DIVIDER);
|
|
|
|
camera->target.z += ((cameraMouseVariation.x*FREE_CAMERA_MOUSE_SENSITIVITY)*sin(cameraAngle.x) + (cameraMouseVariation.y*FREE_CAMERA_MOUSE_SENSITIVITY)*cos(cameraAngle.x)*sin(cameraAngle.y))*(cameraTargetDistance/FREE_CAMERA_PANNING_DIVIDER);
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focus to center
|
2016-02-12 14:22:56 +03:00
|
|
|
// TODO: Move this function out of this module?
|
2016-02-03 19:45:28 +03:00
|
|
|
if (IsKeyDown('Z')) camera->target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Camera position update
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->position.x = sin(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.x;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
if (cameraAngle.y <= 0.0f) camera->position.y = sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
|
2015-07-29 22:42:11 +03:00
|
|
|
else camera->position.y = -sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->position.z = cos(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.z;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case CAMERA_ORBITAL:
|
|
|
|
{
|
|
|
|
cameraAngle.x += ORBITAL_CAMERA_SPEED;
|
|
|
|
|
|
|
|
// Camera zoom
|
2015-07-29 22:42:11 +03:00
|
|
|
cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
|
|
|
|
|
2015-05-05 00:46:31 +03:00
|
|
|
// Camera distance clamp
|
|
|
|
if (cameraTargetDistance < THIRD_PERSON_DISTANCE_CLAMP) cameraTargetDistance = THIRD_PERSON_DISTANCE_CLAMP;
|
|
|
|
|
|
|
|
// Focus to center
|
2016-02-03 19:45:28 +03:00
|
|
|
if (IsKeyDown('Z')) camera->target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Camera position update
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->position.x = sin(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.x;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
if (cameraAngle.y <= 0.0f) camera->position.y = sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
|
2015-07-29 22:42:11 +03:00
|
|
|
else camera->position.y = -sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->position.z = cos(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.z;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case CAMERA_FIRST_PERSON:
|
|
|
|
case CAMERA_THIRD_PERSON:
|
|
|
|
{
|
2016-02-03 19:45:28 +03:00
|
|
|
bool isMoving = false;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Keyboard inputs
|
2015-07-29 22:42:11 +03:00
|
|
|
if (IsKeyDown(cameraMoveControl[MOVE_FRONT]))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
playerPosition->x -= sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
|
|
|
playerPosition->z -= cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
|
|
|
|
|
|
|
if (!cameraUseGravity) camera->position.y += sin(cameraAngle.y)/PLAYER_MOVEMENT_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
isMoving = true;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
2015-07-29 22:42:11 +03:00
|
|
|
else if (IsKeyDown(cameraMoveControl[MOVE_BACK]))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
playerPosition->x += sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
|
|
|
playerPosition->z += cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
|
|
|
|
|
|
|
if (!cameraUseGravity) camera->position.y -= sin(cameraAngle.y)/PLAYER_MOVEMENT_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
isMoving = true;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
if (IsKeyDown(cameraMoveControl[MOVE_LEFT]))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
playerPosition->x -= cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
|
|
|
playerPosition->z += sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
isMoving = true;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
2015-07-29 22:42:11 +03:00
|
|
|
else if (IsKeyDown(cameraMoveControl[MOVE_RIGHT]))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
playerPosition->x += cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
|
|
|
playerPosition->z -= sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
isMoving = true;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
if (IsKeyDown(cameraMoveControl[MOVE_UP]))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2016-02-03 19:45:28 +03:00
|
|
|
if (!cameraUseGravity) playerPosition->y += 1.0f/PLAYER_MOVEMENT_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
2015-07-29 22:42:11 +03:00
|
|
|
else if (IsKeyDown(cameraMoveControl[MOVE_DOWN]))
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2016-02-03 19:45:28 +03:00
|
|
|
if (!cameraUseGravity) playerPosition->y -= 1.0f/PLAYER_MOVEMENT_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cameraMode == CAMERA_THIRD_PERSON)
|
|
|
|
{
|
|
|
|
// Camera orientation calculation
|
2015-07-29 22:42:11 +03:00
|
|
|
cameraAngle.x += cameraMouseVariation.x*-mouseSensitivity;
|
|
|
|
cameraAngle.y += cameraMouseVariation.y*-mouseSensitivity;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Angle clamp
|
2015-07-29 22:42:11 +03:00
|
|
|
if (cameraAngle.y > THIRD_PERSON_MIN_CLAMP*DEG2RAD) cameraAngle.y = THIRD_PERSON_MIN_CLAMP*DEG2RAD;
|
|
|
|
else if (cameraAngle.y < THIRD_PERSON_MAX_CLAMP*DEG2RAD) cameraAngle.y = THIRD_PERSON_MAX_CLAMP*DEG2RAD;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Camera zoom
|
2015-07-29 22:42:11 +03:00
|
|
|
cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Camera distance clamp
|
|
|
|
if (cameraTargetDistance < THIRD_PERSON_DISTANCE_CLAMP) cameraTargetDistance = THIRD_PERSON_DISTANCE_CLAMP;
|
|
|
|
|
|
|
|
// Camera is always looking at player
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->target.x = playerPosition->x + THIRD_PERSON_OFFSET.x*cos(cameraAngle.x) + THIRD_PERSON_OFFSET.z*sin(cameraAngle.x);
|
|
|
|
camera->target.y = playerPosition->y + PLAYER_HEIGHT*FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION + THIRD_PERSON_OFFSET.y;
|
|
|
|
camera->target.z = playerPosition->z + THIRD_PERSON_OFFSET.z*sin(cameraAngle.x) - THIRD_PERSON_OFFSET.x*sin(cameraAngle.x);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Camera position update
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->position.x = sin(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.x;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2016-02-03 19:45:28 +03:00
|
|
|
if (cameraAngle.y <= 0.0f) camera->position.y = sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
|
2015-07-29 22:42:11 +03:00
|
|
|
else camera->position.y = -sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->position.z = cos(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.z;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
2015-07-29 22:42:11 +03:00
|
|
|
else // CAMERA_FIRST_PERSON
|
2015-05-05 00:46:31 +03:00
|
|
|
{
|
2015-07-29 22:42:11 +03:00
|
|
|
if (isMoving) cameraMoveCounter++;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Camera orientation calculation
|
2015-08-29 21:20:09 +03:00
|
|
|
cameraAngle.x += (cameraMouseVariation.x * -mouseSensitivity);
|
|
|
|
cameraAngle.y += (cameraMouseVariation.y * -mouseSensitivity);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Angle clamp
|
2015-07-29 22:42:11 +03:00
|
|
|
if (cameraAngle.y > FIRST_PERSON_MIN_CLAMP*DEG2RAD) cameraAngle.y = FIRST_PERSON_MIN_CLAMP*DEG2RAD;
|
|
|
|
else if (cameraAngle.y < FIRST_PERSON_MAX_CLAMP*DEG2RAD) cameraAngle.y = FIRST_PERSON_MAX_CLAMP*DEG2RAD;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
// Camera is always looking at player
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->target.x = camera->position.x - sin(cameraAngle.x)*FIRST_PERSON_FOCUS_DISTANCE;
|
|
|
|
camera->target.y = camera->position.y + sin(cameraAngle.y)*FIRST_PERSON_FOCUS_DISTANCE;
|
|
|
|
camera->target.z = camera->position.z - cos(cameraAngle.x)*FIRST_PERSON_FOCUS_DISTANCE;
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
camera->position.x = playerPosition->x;
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->position.y = (playerPosition->y + PLAYER_HEIGHT*FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION) - sin(cameraMoveCounter/FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER)/FIRST_PERSON_STEP_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
camera->position.z = playerPosition->z;
|
|
|
|
|
2015-07-29 22:42:11 +03:00
|
|
|
camera->up.x = sin(cameraMoveCounter/(FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/FIRST_PERSON_WAVING_DIVIDER;
|
|
|
|
camera->up.z = -sin(cameraMoveCounter/(FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/FIRST_PERSON_WAVING_DIVIDER;
|
2015-05-05 00:46:31 +03:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: break;
|
|
|
|
}
|
2015-09-02 03:41:55 +03:00
|
|
|
}
|