|
|
@ -170,7 +170,7 @@ void SetCameraMoveControls(int frontKey, int backKey, |
|
|
|
#define CAMERA_FIRST_PERSON_MIN_CLAMP 89.0f |
|
|
|
#define CAMERA_FIRST_PERSON_MAX_CLAMP -89.0f |
|
|
|
|
|
|
|
#define CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER 5.0f |
|
|
|
#define CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER 8.0f |
|
|
|
#define CAMERA_FIRST_PERSON_STEP_DIVIDER 30.0f |
|
|
|
#define CAMERA_FIRST_PERSON_WAVING_DIVIDER 200.0f |
|
|
|
|
|
|
@ -197,17 +197,18 @@ typedef enum { |
|
|
|
MOVE_DOWN |
|
|
|
} CameraMove; |
|
|
|
|
|
|
|
// Camera global state context data |
|
|
|
// Camera global state context data [56 bytes] |
|
|
|
typedef struct { |
|
|
|
int mode; // Current camera mode |
|
|
|
unsigned int mode; // Current camera mode |
|
|
|
float targetDistance; // Camera distance from position to target |
|
|
|
float playerEyesPosition; // Default player eyes position from ground (in meters) |
|
|
|
float playerEyesPosition; // Player eyes position from ground (in meters) |
|
|
|
Vector2 angle; // Camera angle in plane XZ |
|
|
|
|
|
|
|
int moveControl[6]; |
|
|
|
int smoothZoomControl; // raylib: KEY_LEFT_CONTROL |
|
|
|
int altControl; // raylib: KEY_LEFT_ALT |
|
|
|
int panControl; // raylib: MOUSE_MIDDLE_BUTTON |
|
|
|
// Camera movement control keys |
|
|
|
int moveControl[6]; // Move controls (CAMERA_FIRST_PERSON) |
|
|
|
int smoothZoomControl; // Smooth zoom control key |
|
|
|
int altControl; // Alternative control key |
|
|
|
int panControl; // Pan view control key |
|
|
|
} CameraData; |
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------- |
|
|
@ -219,9 +220,9 @@ static CameraData CAMERA = { // Global CAMERA state context |
|
|
|
.playerEyesPosition = 1.85f, |
|
|
|
.angle = { 0 }, |
|
|
|
.moveControl = { 'W', 'S', 'D', 'A', 'E', 'Q' }, |
|
|
|
.smoothZoomControl = 341, |
|
|
|
.altControl = 342, |
|
|
|
.panControl = 2 |
|
|
|
.smoothZoomControl = 341, // raylib: KEY_LEFT_CONTROL |
|
|
|
.altControl = 342, // raylib: KEY_LEFT_ALT |
|
|
|
.panControl = 2 // raylib: MOUSE_MIDDLE_BUTTON |
|
|
|
}; |
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------- |
|
|
@ -253,13 +254,13 @@ void SetCameraMode(Camera camera, int mode) |
|
|
|
float dy = v2.y - v1.y; |
|
|
|
float dz = v2.z - v1.z; |
|
|
|
|
|
|
|
CAMERA.targetDistance = sqrtf(dx*dx + dy*dy + dz*dz); |
|
|
|
CAMERA.targetDistance = sqrtf(dx*dx + dy*dy + dz*dz); // Distance to target |
|
|
|
|
|
|
|
// Camera angle calculation |
|
|
|
CAMERA.angle.x = atan2f(dx, dz); // Camera angle in plane XZ (0 aligned with Z, move positive CCW) |
|
|
|
CAMERA.angle.y = atan2f(dy, sqrtf(dx*dx + dz*dz)); // Camera angle in plane XY (0 aligned with X, move positive CW) |
|
|
|
CAMERA.angle.x = atan2f(dx, dz); // Camera angle in plane XZ (0 aligned with Z, move positive CCW) |
|
|
|
CAMERA.angle.y = atan2f(dy, sqrtf(dx*dx + dz*dz)); // Camera angle in plane XY (0 aligned with X, move positive CW) |
|
|
|
|
|
|
|
CAMERA.playerEyesPosition = camera.position.y; |
|
|
|
CAMERA.playerEyesPosition = camera.position.y; // Init player eyes position to camera Y position |
|
|
|
|
|
|
|
// Lock cursor for first person and third person cameras |
|
|
|
if ((mode == CAMERA_FIRST_PERSON) || (mode == CAMERA_THIRD_PERSON)) DisableCursor(); |
|
|
@ -287,6 +288,7 @@ void UpdateCamera(Camera *camera) |
|
|
|
int mouseWheelMove = GetMouseWheelMove(); |
|
|
|
|
|
|
|
// Keys input detection |
|
|
|
// TODO: Input detection is raylib-dependant, it could be moved outside the module |
|
|
|
bool panKey = IsMouseButtonDown(CAMERA.panControl); |
|
|
|
bool altKey = IsKeyDown(CAMERA.altControl); |
|
|
|
bool szoomKey = IsKeyDown(CAMERA.smoothZoomControl); |
|
|
@ -297,8 +299,6 @@ void UpdateCamera(Camera *camera) |
|
|
|
IsKeyDown(CAMERA.moveControl[MOVE_UP]), |
|
|
|
IsKeyDown(CAMERA.moveControl[MOVE_DOWN]) }; |
|
|
|
|
|
|
|
// TODO: Touch input detection (probably gestures system required) |
|
|
|
|
|
|
|
if (CAMERA.mode != CAMERA_CUSTOM) |
|
|
|
{ |
|
|
|
mousePositionDelta.x = mousePosition.x - previousMousePosition.x; |
|
|
@ -321,7 +321,6 @@ void UpdateCamera(Camera *camera) |
|
|
|
} |
|
|
|
|
|
|
|
// Camera looking down |
|
|
|
// TODO: Review, weird comparison of CAMERA.targetDistance == 120.0f? |
|
|
|
else if ((camera->position.y > camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0)) |
|
|
|
{ |
|
|
|
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
|
|
@ -342,7 +341,6 @@ void UpdateCamera(Camera *camera) |
|
|
|
if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP; |
|
|
|
} |
|
|
|
// Camera looking up |
|
|
|
// TODO: Review, weird comparisson of CAMERA.targetDistance == 120.0f? |
|
|
|
else if ((camera->position.y < camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0)) |
|
|
|
{ |
|
|
|
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
|
|
@ -489,8 +487,10 @@ void UpdateCamera(Camera *camera) |
|
|
|
|
|
|
|
// TODO: It seems camera->position is not correctly updated or some rounding issue makes the camera move straight to camera->target... |
|
|
|
camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x; |
|
|
|
|
|
|
|
if (CAMERA.angle.y <= 0.0f) camera->position.y = sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y; |
|
|
|
else camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y; |
|
|
|
|
|
|
|
camera->position.z = cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z; |
|
|
|
|
|
|
|
} break; |
|
|
|