core functionality CAMERA_FREE
This commit is contained in:
parent
234576da71
commit
864da80858
|
|
@ -49,6 +49,9 @@ int main(void)
|
|||
|
||||
SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
|
||||
|
||||
// Catch cursor
|
||||
DisableCursor();
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -307,6 +307,7 @@ typedef struct Camera3D {
|
|||
Vector3 up; // Camera up vector (rotation over its axis)
|
||||
float fovy; // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic
|
||||
int projection; // Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
|
||||
int mode; // Camera mode: CAMERA_FREE, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON, CAMERA_ORBITAL or CUSTOM
|
||||
} Camera3D;
|
||||
|
||||
typedef Camera3D Camera; // Camera type fallback, defaults to Camera3D
|
||||
|
|
@ -1151,7 +1152,7 @@ RLAPI float GetGesturePinchAngle(void); // Get gesture pinch ang
|
|||
//------------------------------------------------------------------------------------
|
||||
// Camera System Functions (Module: rcamera)
|
||||
//------------------------------------------------------------------------------------
|
||||
RLAPI void SetCameraMode(Camera camera, int mode); // Set camera mode (multiple camera modes available)
|
||||
RLAPI void SetCameraMode(Camera* camera, int mode); // Set camera mode (multiple camera modes available)
|
||||
RLAPI void UpdateCamera(Camera *camera); // Update camera position for selected mode
|
||||
|
||||
RLAPI void SetCameraPanControl(int keyPan); // Set camera pan key to combine with mouse movement (free camera)
|
||||
|
|
|
|||
595
src/rcamera.h
595
src/rcamera.h
|
|
@ -2,7 +2,7 @@
|
|||
*
|
||||
* rcamera - Basic camera system for multiple camera modes
|
||||
*
|
||||
* NOTE: Memory footprint of this library is aproximately 52 bytes (global variables)
|
||||
* NOTE: Memory footprint of this library is aproximately ??? TODO bytes (global variables)
|
||||
*
|
||||
* CONFIGURATION:
|
||||
*
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
*
|
||||
* CONTRIBUTORS:
|
||||
* Ramon Santamaria: Supervision, review, update and maintenance
|
||||
* Christoph Wagner: Redesign (2022)
|
||||
* Marc Palau: Initial implementation (2014)
|
||||
*
|
||||
*
|
||||
|
|
@ -44,15 +45,26 @@
|
|||
#ifndef RCAMERA_H
|
||||
#define RCAMERA_H
|
||||
|
||||
// The only dependency // TODO review standalone mode
|
||||
#include "raymath.h"
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
//...
|
||||
#if defined(CAMERA_STANDALONE)
|
||||
#define CAMERA_CULL_DISTANCE_NEAR 0.01
|
||||
#define CAMERA_CULL_DISTANCE_FAR 1000.0
|
||||
#else
|
||||
#define CAMERA_CULL_DISTANCE_NEAR RL_CULL_DISTANCE_NEAR
|
||||
#define CAMERA_CULL_DISTANCE_FAR RL_CULL_DISTANCE_FAR
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
// NOTE: Below types are required for CAMERA_STANDALONE usage
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO review
|
||||
#if defined(CAMERA_STANDALONE)
|
||||
// Vector2 type
|
||||
typedef struct Vector2 {
|
||||
|
|
@ -134,42 +146,16 @@ void SetCameraMoveControls(int keyFront, int keyBack,
|
|||
|
||||
#if defined(CAMERA_IMPLEMENTATION)
|
||||
|
||||
#include <math.h> // Required for: sinf(), cosf(), sqrtf()
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#ifndef PI
|
||||
#define PI 3.14159265358979323846
|
||||
#endif
|
||||
#ifndef DEG2RAD
|
||||
#define DEG2RAD (PI/180.0f)
|
||||
#endif
|
||||
#ifndef RAD2DEG
|
||||
#define RAD2DEG (180.0f/PI)
|
||||
#endif
|
||||
|
||||
// Camera mouse movement sensitivity
|
||||
#define CAMERA_MOUSE_MOVE_SENSITIVITY 0.5f // TODO: it should be independant of framerate
|
||||
#define CAMERA_MOUSE_SCROLL_SENSITIVITY 1.5f
|
||||
|
||||
// FREE_CAMERA
|
||||
#define CAMERA_FREE_MOUSE_SENSITIVITY 0.01f
|
||||
#define CAMERA_FREE_DISTANCE_MIN_CLAMP 0.3f
|
||||
#define CAMERA_FREE_DISTANCE_MAX_CLAMP 120.0f
|
||||
#define CAMERA_FREE_MIN_CLAMP 85.0f
|
||||
#define CAMERA_FREE_MAX_CLAMP -85.0f
|
||||
#define CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY 0.05f
|
||||
#define CAMERA_FREE_PANNING_DIVIDER 5.1f
|
||||
#define CAMERA_ORBITAL_SPEED 0.01f // Radians per frame
|
||||
|
||||
// ORBITAL_CAMERA
|
||||
#define CAMERA_ORBITAL_SPEED 0.5f // Radians per second
|
||||
|
||||
// FIRST_PERSON
|
||||
//#define CAMERA_FIRST_PERSON_MOUSE_SENSITIVITY 0.003f
|
||||
#define CAMERA_FIRST_PERSON_FOCUS_DISTANCE 25.0f
|
||||
#define CAMERA_FIRST_PERSON_MIN_CLAMP 89.0f
|
||||
#define CAMERA_FIRST_PERSON_MAX_CLAMP -89.0f
|
||||
|
||||
// When walking, y-position of the player moves up-down at step frequency (swinging) but
|
||||
// also the body slightly tilts left-right on every step, when all the body weight is left over one foot (tilting)
|
||||
|
|
@ -177,13 +163,6 @@ void SetCameraMoveControls(int keyFront, int keyBack,
|
|||
#define CAMERA_FIRST_PERSON_SWINGING_DELTA 0.03f // Maximum up-down swinging distance when walking
|
||||
#define CAMERA_FIRST_PERSON_TILTING_DELTA 0.005f // Maximum left-right tilting distance when walking
|
||||
|
||||
// THIRD_PERSON
|
||||
//#define CAMERA_THIRD_PERSON_MOUSE_SENSITIVITY 0.003f
|
||||
#define CAMERA_THIRD_PERSON_DISTANCE_CLAMP 1.2f
|
||||
#define CAMERA_THIRD_PERSON_MIN_CLAMP 5.0f
|
||||
#define CAMERA_THIRD_PERSON_MAX_CLAMP -85.0f
|
||||
#define CAMERA_THIRD_PERSON_OFFSET (Vector3){ 0.4f, 0.0f, 0.0f }
|
||||
|
||||
// PLAYER (used by camera)
|
||||
#define PLAYER_MOVEMENT_SENSITIVITY 2.0f
|
||||
|
||||
|
|
@ -192,7 +171,7 @@ void SetCameraMoveControls(int keyFront, int keyBack,
|
|||
//----------------------------------------------------------------------------------
|
||||
// Camera move modes (first person and third person cameras)
|
||||
typedef enum {
|
||||
MOVE_FRONT = 0,
|
||||
MOVE_FORWARD = 0,
|
||||
MOVE_BACK,
|
||||
MOVE_RIGHT,
|
||||
MOVE_LEFT,
|
||||
|
|
@ -201,339 +180,281 @@ typedef enum {
|
|||
} CameraMove;
|
||||
|
||||
// Camera global state context data [56 bytes]
|
||||
typedef struct {
|
||||
unsigned int mode; // Current camera mode
|
||||
float targetDistance; // Camera distance from position to target
|
||||
float playerEyesPosition; // Player eyes position from ground (in meters)
|
||||
Vector2 angle; // Camera angle in plane XZ
|
||||
// typedef struct {
|
||||
// unsigned int mode; // Current camera mode
|
||||
// float targetDistance; // Camera distance from position to target
|
||||
// float playerEyesPosition; // Player eyes position from ground (in meters)
|
||||
// Vector2 angle; // Camera angle in plane XZ
|
||||
// Vector2 previousMousePosition; // Previous mouse position
|
||||
|
||||
// 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;
|
||||
// // 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;
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
static CameraData CAMERA = { // Global CAMERA state context
|
||||
.mode = 0,
|
||||
.targetDistance = 0,
|
||||
.playerEyesPosition = 1.85f,
|
||||
.angle = { 0 },
|
||||
.moveControl = { 'W', 'S', 'D', 'A', 'E', 'Q' },
|
||||
.smoothZoomControl = 341, // raylib: KEY_LEFT_CONTROL
|
||||
.altControl = 342, // raylib: KEY_LEFT_ALT
|
||||
.panControl = 2 // raylib: MOUSE_BUTTON_MIDDLE
|
||||
};
|
||||
|
||||
// static CameraData CAMERA = { // Global CAMERA state context
|
||||
// .mode = 0,
|
||||
// .targetDistance = 0,
|
||||
// .playerEyesPosition = 1.85f,
|
||||
// .angle = { 0 },
|
||||
// .previousMousePosition = { 0 },
|
||||
// .moveControl = { 'W', 'S', 'D', 'A', 'E', 'Q' },
|
||||
// .smoothZoomControl = 341, // raylib: KEY_LEFT_CONTROL
|
||||
// .altControl = 342, // raylib: KEY_LEFT_ALT
|
||||
// .panControl = 2 // raylib: MOUSE_BUTTON_MIDDLE
|
||||
// };
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
#if defined(CAMERA_STANDALONE)
|
||||
// NOTE: Camera controls depend on some raylib input functions
|
||||
static void EnableCursor() {} // Unlock cursor
|
||||
static void DisableCursor() {} // Lock cursor
|
||||
|
||||
static int IsKeyDown(int key) { return 0; }
|
||||
|
||||
static int IsMouseButtonDown(int button) { return 0;}
|
||||
static float GetMouseWheelMove() { return 0.0f; }
|
||||
static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; }
|
||||
#endif
|
||||
// Rotates the given vector around the given axis
|
||||
// Note: angle must be provided in radians
|
||||
Vector3 Vector3RotateAxis(Vector3 v, Vector3 axis, float angle) { // TODO there is a better way without the intermediate matrix
|
||||
Matrix rot = MatrixRotate(axis, angle);
|
||||
return Vector3Transform(v, rot);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// TODO declare all functions in raylib.h and standalone "header"
|
||||
void CameraPitch(Camera3D* camera, float angle);
|
||||
void CameraYaw(Camera3D* camera, float angle);
|
||||
void CameraRoll(Camera3D* camera, float angle);
|
||||
|
||||
// Select camera mode (multiple camera modes available)
|
||||
void SetCameraMode(Camera camera, int mode)
|
||||
void SetCameraMode(Camera3D* camera, int mode)
|
||||
{
|
||||
Vector3 v1 = camera.position;
|
||||
Vector3 v2 = camera.target;
|
||||
camera->mode = mode;
|
||||
|
||||
float dx = v2.x - v1.x;
|
||||
float dy = v2.y - v1.y;
|
||||
float dz = v2.z - v1.z;
|
||||
// TODO
|
||||
|
||||
CAMERA.targetDistance = sqrtf(dx*dx + dy*dy + dz*dz); // Distance to target
|
||||
// TODO Reset roll and fix overrotation (i.e. somersaults)
|
||||
|
||||
// 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.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();
|
||||
else EnableCursor();
|
||||
|
||||
CAMERA.mode = mode;
|
||||
}
|
||||
|
||||
// Update camera depending on selected mode
|
||||
// NOTE: Camera controls depend on some raylib functions:
|
||||
// System: EnableCursor(), DisableCursor()
|
||||
// Mouse: IsMouseButtonDown(), GetMousePosition(), GetMouseWheelMove()
|
||||
// Keys: IsKeyDown()
|
||||
void UpdateCamera(Camera *camera)
|
||||
// Returns the cameras current forward vector (normalized)
|
||||
Vector3 GetCameraForward(Camera3D* camera) {
|
||||
return Vector3Normalize(Vector3Subtract(camera->target, camera->position));
|
||||
}
|
||||
|
||||
// Returns the cameras current up vector (normalized)
|
||||
Vector3 GetCameraUp(Camera3D* camera) {
|
||||
// Note: The up vector might not be perpendicular to the forward vector
|
||||
Vector3 forward = Vector3Subtract(camera->target, camera->position);
|
||||
Vector3OrthoNormalize(&forward, &camera->up);
|
||||
return camera->up;
|
||||
}
|
||||
|
||||
// Returns the cameras current right vector (normalized)
|
||||
Vector3 GetCameraRight(Camera3D* camera) {
|
||||
Vector3 forward = GetCameraForward(camera);
|
||||
Vector3 up = GetCameraUp(camera);
|
||||
return Vector3CrossProduct(forward, up);
|
||||
}
|
||||
|
||||
// Moves the camera in its current forward vector direction
|
||||
void CameraMoveForward(Camera3D *camera, float distance) {
|
||||
Vector3 forward = GetCameraForward(camera);
|
||||
|
||||
if (camera->mode == CAMERA_FIRST_PERSON ||
|
||||
camera->mode == CAMERA_THIRD_PERSON)
|
||||
{
|
||||
static float swingCounter = 0.0f; // Used for 1st person swinging movement
|
||||
// Project vector onto world plane
|
||||
forward.y = 0;
|
||||
forward = Vector3Normalize(forward);
|
||||
}
|
||||
|
||||
// TODO: Compute CAMERA.targetDistance and CAMERA.angle here (?)
|
||||
forward = Vector3Scale(forward, distance);
|
||||
|
||||
// Mouse movement detection
|
||||
camera->position = Vector3Add(camera->position, forward);
|
||||
camera->target = Vector3Add(camera->target, forward);
|
||||
}
|
||||
|
||||
// Moves the camera in its current up vector direction
|
||||
void CameraMoveUp(Camera3D *camera, float distance) {
|
||||
Vector3 up = GetCameraUp(camera);
|
||||
|
||||
up = Vector3Scale(up, distance);
|
||||
|
||||
camera->position = Vector3Add(camera->position, up);
|
||||
camera->target = Vector3Add(camera->target, up);
|
||||
}
|
||||
|
||||
// Moves the camera target in its current right vector direction
|
||||
void CameraMoveRight(Camera3D *camera, float distance) {
|
||||
Vector3 right = GetCameraRight(camera);
|
||||
|
||||
if (camera->mode == CAMERA_FIRST_PERSON ||
|
||||
camera->mode == CAMERA_THIRD_PERSON)
|
||||
{
|
||||
// Project vector onto world plane
|
||||
right.y = 0;
|
||||
right = Vector3Normalize(right);
|
||||
}
|
||||
|
||||
right = Vector3Scale(right, distance);
|
||||
|
||||
camera->position = Vector3Add(camera->position, right);
|
||||
camera->target = Vector3Add(camera->target, right);
|
||||
}
|
||||
|
||||
// Rotates the camera around its current up vector
|
||||
// Yaw is "looking left and right"
|
||||
// Note: angle must be provided in radians
|
||||
void CameraYaw(Camera3D *camera, float angle) {
|
||||
// Rotation axis
|
||||
Vector3 up = GetCameraUp(camera);
|
||||
|
||||
Vector3 target_position = Vector3Subtract(camera->target, camera->position);
|
||||
|
||||
// Rotate target
|
||||
target_position = Vector3RotateAxis(target_position, up, angle);
|
||||
camera->target = Vector3Add(camera->position, target_position);
|
||||
}
|
||||
|
||||
// Rotates the camera around its current right vector
|
||||
// Pitch is "looking up and down"
|
||||
// Note: angle must be provided in radians
|
||||
void CameraPitch(Camera3D *camera, float angle) {
|
||||
|
||||
if (camera->mode == CAMERA_FIRST_PERSON ||
|
||||
camera->mode == CAMERA_THIRD_PERSON ||
|
||||
camera->mode == CAMERA_ORBITAL)
|
||||
{
|
||||
// In these camera modes we clamp the Pitch angle
|
||||
// to allow only viewing straight up or down.
|
||||
// i.e. prevent somersaults
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
// Rotation axis
|
||||
Vector3 right = GetCameraRight(camera);
|
||||
|
||||
Vector3 target_position = Vector3Subtract(camera->target, camera->position);
|
||||
|
||||
// Rotate target
|
||||
target_position = Vector3RotateAxis(target_position, right, angle);
|
||||
camera->target = Vector3Add(camera->position, target_position);
|
||||
|
||||
// Rotate up direction
|
||||
camera->up = Vector3RotateAxis(camera->up, right, angle);
|
||||
}
|
||||
|
||||
// Rotates the camera around its current forward vector
|
||||
// Roll is "turning your head sideways to the left or right"
|
||||
// Note: angle must be provided in radians
|
||||
void CameraRoll(Camera3D *camera, float angle) {
|
||||
// Rotation axis
|
||||
Vector3 forward = GetCameraForward(camera);
|
||||
|
||||
// Rotate up direction
|
||||
camera->up = Vector3RotateAxis(camera->up, forward, angle);
|
||||
}
|
||||
|
||||
// Returns the current camera view matrix
|
||||
Matrix GetCameraViewMatrix(Camera3D* camera) {
|
||||
// Note: MatrixLookAt normalizes up internally
|
||||
// TODO review up needs to be perpendicular?
|
||||
return MatrixLookAt(camera->position, camera->target, camera->up);
|
||||
}
|
||||
|
||||
// Returns the current camera projection matrix
|
||||
Matrix GetCameraProjectionMatrix(Camera3D* camera, float aspect) {
|
||||
if (camera->projection == CAMERA_PERSPECTIVE)
|
||||
{
|
||||
return MatrixPerspective(camera->fovy * DEG2RAD, aspect, CAMERA_CULL_DISTANCE_NEAR, CAMERA_CULL_DISTANCE_FAR);
|
||||
}
|
||||
else if (camera->projection == CAMERA_ORTHOGRAPHIC)
|
||||
{
|
||||
double top = camera->fovy / 2.0;
|
||||
double right = top * aspect;
|
||||
return MatrixOrtho(-right, right, -top, top, CAMERA_CULL_DISTANCE_NEAR, CAMERA_CULL_DISTANCE_FAR);
|
||||
}
|
||||
|
||||
return MatrixIdentity();
|
||||
}
|
||||
|
||||
static bool on_init = true;
|
||||
void UpdateCamera(Camera3D* camera) {
|
||||
|
||||
// TODO review
|
||||
const float CAMERA_MOVE_SPEED = 0.03f;
|
||||
|
||||
// TODO Input detection is raylib-dependant
|
||||
|
||||
// Avoid inital mouse "jump"
|
||||
if (on_init) {
|
||||
SetMousePosition(0, 0);
|
||||
on_init = false;
|
||||
}
|
||||
Vector2 mousePositionDelta = GetMouseDelta();
|
||||
float mouseWheelMove = GetMouseWheelMove();
|
||||
|
||||
// Keys input detection
|
||||
// TODO: Input detection is raylib-dependant, it could be moved outside the module
|
||||
bool keyPan = IsMouseButtonDown(CAMERA.panControl);
|
||||
bool keyAlt = IsKeyDown(CAMERA.altControl);
|
||||
bool szoomKey = IsKeyDown(CAMERA.smoothZoomControl);
|
||||
bool direction[6] = { IsKeyDown(CAMERA.moveControl[MOVE_FRONT]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_BACK]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_RIGHT]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_LEFT]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_UP]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_DOWN]) };
|
||||
// Camera movement
|
||||
if (IsKeyDown(KEY_W)) CameraMoveForward(camera, CAMERA_MOVE_SPEED * 3);
|
||||
if (IsKeyDown(KEY_S)) CameraMoveForward(camera, -CAMERA_MOVE_SPEED * 3);
|
||||
if (IsKeyDown(KEY_D)) CameraMoveRight(camera, CAMERA_MOVE_SPEED * 3);
|
||||
if (IsKeyDown(KEY_A)) CameraMoveRight(camera, -CAMERA_MOVE_SPEED * 3);
|
||||
if (IsKeyDown(KEY_SPACE)) CameraMoveUp(camera, CAMERA_MOVE_SPEED * 3);
|
||||
if (IsKeyDown(KEY_LEFT_CONTROL)) CameraMoveUp(camera, -CAMERA_MOVE_SPEED * 3);
|
||||
|
||||
// Support for multiple automatic camera modes
|
||||
// NOTE: In case of CAMERA_CUSTOM nothing happens here, user must update it manually
|
||||
switch (CAMERA.mode)
|
||||
{
|
||||
case CAMERA_FREE: // Camera free controls, using standard 3d-content-creation scheme
|
||||
{
|
||||
// Camera zoom
|
||||
if ((CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
|
||||
{
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
if (CAMERA.targetDistance > CAMERA_FREE_DISTANCE_MAX_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MAX_CLAMP;
|
||||
}
|
||||
|
||||
// Camera looking down
|
||||
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;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
}
|
||||
else if ((camera->position.y > camera->target.y) && (camera->target.y >= 0))
|
||||
{
|
||||
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
|
||||
// if (camera->target.y < 0) camera->target.y = -0.001;
|
||||
}
|
||||
else if ((camera->position.y > camera->target.y) && (camera->target.y < 0) && (mouseWheelMove > 0))
|
||||
{
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
|
||||
}
|
||||
// Camera looking up
|
||||
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;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
}
|
||||
else if ((camera->position.y < camera->target.y) && (camera->target.y <= 0))
|
||||
{
|
||||
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
|
||||
// if (camera->target.y > 0) camera->target.y = 0.001;
|
||||
}
|
||||
else if ((camera->position.y < camera->target.y) && (camera->target.y > 0) && (mouseWheelMove > 0))
|
||||
{
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
|
||||
}
|
||||
|
||||
// Input keys checks
|
||||
if (keyPan)
|
||||
{
|
||||
if (keyAlt) // Alternative key behaviour
|
||||
{
|
||||
if (szoomKey)
|
||||
{
|
||||
// Camera smooth zoom
|
||||
CAMERA.targetDistance += (mousePositionDelta.y*CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Camera rotation
|
||||
CAMERA.angle.x += mousePositionDelta.x*-CAMERA_FREE_MOUSE_SENSITIVITY;
|
||||
CAMERA.angle.y += mousePositionDelta.y*-CAMERA_FREE_MOUSE_SENSITIVITY;
|
||||
if (IsKeyDown(KEY_DOWN)) CameraPitch(camera, -CAMERA_MOVE_SPEED);
|
||||
if (IsKeyDown(KEY_UP)) CameraPitch(camera, CAMERA_MOVE_SPEED);
|
||||
if (IsKeyDown(KEY_RIGHT)) CameraYaw(camera, -CAMERA_MOVE_SPEED);
|
||||
if (IsKeyDown(KEY_LEFT)) CameraYaw(camera, CAMERA_MOVE_SPEED);
|
||||
if (IsKeyDown(KEY_Q)) CameraRoll(camera, -CAMERA_MOVE_SPEED);
|
||||
if (IsKeyDown(KEY_E)) CameraRoll(camera, CAMERA_MOVE_SPEED);
|
||||
|
||||
// Angle clamp
|
||||
if (CAMERA.angle.y > CAMERA_FREE_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MIN_CLAMP*DEG2RAD;
|
||||
else if (CAMERA.angle.y < CAMERA_FREE_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MAX_CLAMP*DEG2RAD;
|
||||
|
||||
CameraYaw(camera, mousePositionDelta.x * -CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
CameraPitch(camera, mousePositionDelta.y * -CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
|
||||
#if 0
|
||||
|
||||
// Adjust camera target_distance
|
||||
camera->target_distance -= GetMouseWheelMove();
|
||||
if (IsKeyPressed(KEY_KP_SUBTRACT)) camera->target_distance -= 1.0f;
|
||||
if (IsKeyPressed(KEY_KP_ADD)) camera->target_distance += 1.0f;
|
||||
|
||||
|
||||
// Switch camera mode
|
||||
if (IsKeyPressed(KEY_ONE)) SetCameraMode(camera, CAMERA_FREE);
|
||||
if (IsKeyPressed(KEY_TWO)) SetCameraMode(camera, CAMERA_FIRST_PERSON);
|
||||
if (IsKeyPressed(KEY_THREE)) SetCameraMode(camera, CAMERA_THIRD_PERSON);
|
||||
if (IsKeyPressed(KEY_FOUR)) SetCameraMode(camera, CAMERA_ORBITAL);
|
||||
|
||||
// Switch camera projection
|
||||
if (IsKeyPressed(KEY_P)) {
|
||||
if (camera->projection == CAMERA_PERSPECTIVE) {
|
||||
// Create isometric view
|
||||
InitializeCamera(camera, camera->aspect);
|
||||
camera->mode = CAMERA_THIRD_PERSON;
|
||||
camera->projection = CAMERA_ORTHOGRAPHIC;
|
||||
camera->fovy = 20.0f; // near plane width in orthografic
|
||||
camera->target_distance = 100.0f; // influences only clip distance
|
||||
CameraYaw(camera, -135 * DEG2RAD);
|
||||
CameraPitch(camera, 45 * DEG2RAD);
|
||||
}
|
||||
else if (camera->projection == CAMERA_ORTHOGRAPHIC) {
|
||||
// Reset default view
|
||||
InitializeCamera(camera, camera->aspect);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Camera panning
|
||||
camera->target.x += ((mousePositionDelta.x*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.x) + (mousePositionDelta.y*-CAMERA_FREE_MOUSE_SENSITIVITY)*sinf(CAMERA.angle.x)*sinf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER);
|
||||
camera->target.y += ((mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER);
|
||||
camera->target.z += ((mousePositionDelta.x*-CAMERA_FREE_MOUSE_SENSITIVITY)*sinf(CAMERA.angle.x) + (mousePositionDelta.y*-CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.x)*sinf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// Update camera position with changes
|
||||
camera->position.x = -sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
|
||||
camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance + camera->target.y;
|
||||
camera->position.z = -cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z;
|
||||
|
||||
} break;
|
||||
case CAMERA_ORBITAL: // Camera just orbits around target, only zoom allowed
|
||||
{
|
||||
CAMERA.angle.x += CAMERA_ORBITAL_SPEED*GetFrameTime(); // Camera orbit angle
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY); // Camera zoom
|
||||
|
||||
// Camera distance clamp
|
||||
if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;
|
||||
|
||||
// Update camera position with changes
|
||||
camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
|
||||
camera->position.y = ((CAMERA.angle.y <= 0.0f)? 1 : -1)*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;
|
||||
case CAMERA_FIRST_PERSON: // Camera moves as in a first-person game, controls are configurable
|
||||
{
|
||||
camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_FRONT] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_LEFT] +
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])*PLAYER_MOVEMENT_SENSITIVITY*GetFrameTime();
|
||||
|
||||
camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] -
|
||||
sinf(CAMERA.angle.y)*direction[MOVE_BACK] +
|
||||
1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])*PLAYER_MOVEMENT_SENSITIVITY*GetFrameTime();
|
||||
|
||||
camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_FRONT] +
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_LEFT] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])*PLAYER_MOVEMENT_SENSITIVITY*GetFrameTime();
|
||||
|
||||
// Camera orientation calculation
|
||||
CAMERA.angle.x -= mousePositionDelta.x*CAMERA_MOUSE_MOVE_SENSITIVITY*GetFrameTime();
|
||||
CAMERA.angle.y -= mousePositionDelta.y*CAMERA_MOUSE_MOVE_SENSITIVITY*GetFrameTime();
|
||||
|
||||
// Angle clamp
|
||||
if (CAMERA.angle.y > CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD;
|
||||
else if (CAMERA.angle.y < CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD;
|
||||
|
||||
// Calculate translation matrix
|
||||
Matrix matTranslation = { 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, (CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER),
|
||||
0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
// Calculate rotation matrix
|
||||
Matrix matRotation = { 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
float cosz = cosf(0.0f);
|
||||
float sinz = sinf(0.0f);
|
||||
float cosy = cosf(-(PI*2 - CAMERA.angle.x));
|
||||
float siny = sinf(-(PI*2 - CAMERA.angle.x));
|
||||
float cosx = cosf(-(PI*2 - CAMERA.angle.y));
|
||||
float sinx = sinf(-(PI*2 - CAMERA.angle.y));
|
||||
|
||||
matRotation.m0 = cosz*cosy;
|
||||
matRotation.m4 = (cosz*siny*sinx) - (sinz*cosx);
|
||||
matRotation.m8 = (cosz*siny*cosx) + (sinz*sinx);
|
||||
matRotation.m1 = sinz*cosy;
|
||||
matRotation.m5 = (sinz*siny*sinx) + (cosz*cosx);
|
||||
matRotation.m9 = (sinz*siny*cosx) - (cosz*sinx);
|
||||
matRotation.m2 = -siny;
|
||||
matRotation.m6 = cosy*sinx;
|
||||
matRotation.m10= cosy*cosx;
|
||||
|
||||
// Multiply translation and rotation matrices
|
||||
Matrix matTransform = { 0 };
|
||||
matTransform.m0 = matTranslation.m0*matRotation.m0 + matTranslation.m1*matRotation.m4 + matTranslation.m2*matRotation.m8 + matTranslation.m3*matRotation.m12;
|
||||
matTransform.m1 = matTranslation.m0*matRotation.m1 + matTranslation.m1*matRotation.m5 + matTranslation.m2*matRotation.m9 + matTranslation.m3*matRotation.m13;
|
||||
matTransform.m2 = matTranslation.m0*matRotation.m2 + matTranslation.m1*matRotation.m6 + matTranslation.m2*matRotation.m10 + matTranslation.m3*matRotation.m14;
|
||||
matTransform.m3 = matTranslation.m0*matRotation.m3 + matTranslation.m1*matRotation.m7 + matTranslation.m2*matRotation.m11 + matTranslation.m3*matRotation.m15;
|
||||
matTransform.m4 = matTranslation.m4*matRotation.m0 + matTranslation.m5*matRotation.m4 + matTranslation.m6*matRotation.m8 + matTranslation.m7*matRotation.m12;
|
||||
matTransform.m5 = matTranslation.m4*matRotation.m1 + matTranslation.m5*matRotation.m5 + matTranslation.m6*matRotation.m9 + matTranslation.m7*matRotation.m13;
|
||||
matTransform.m6 = matTranslation.m4*matRotation.m2 + matTranslation.m5*matRotation.m6 + matTranslation.m6*matRotation.m10 + matTranslation.m7*matRotation.m14;
|
||||
matTransform.m7 = matTranslation.m4*matRotation.m3 + matTranslation.m5*matRotation.m7 + matTranslation.m6*matRotation.m11 + matTranslation.m7*matRotation.m15;
|
||||
matTransform.m8 = matTranslation.m8*matRotation.m0 + matTranslation.m9*matRotation.m4 + matTranslation.m10*matRotation.m8 + matTranslation.m11*matRotation.m12;
|
||||
matTransform.m9 = matTranslation.m8*matRotation.m1 + matTranslation.m9*matRotation.m5 + matTranslation.m10*matRotation.m9 + matTranslation.m11*matRotation.m13;
|
||||
matTransform.m10 = matTranslation.m8*matRotation.m2 + matTranslation.m9*matRotation.m6 + matTranslation.m10*matRotation.m10 + matTranslation.m11*matRotation.m14;
|
||||
matTransform.m11 = matTranslation.m8*matRotation.m3 + matTranslation.m9*matRotation.m7 + matTranslation.m10*matRotation.m11 + matTranslation.m11*matRotation.m15;
|
||||
matTransform.m12 = matTranslation.m12*matRotation.m0 + matTranslation.m13*matRotation.m4 + matTranslation.m14*matRotation.m8 + matTranslation.m15*matRotation.m12;
|
||||
matTransform.m13 = matTranslation.m12*matRotation.m1 + matTranslation.m13*matRotation.m5 + matTranslation.m14*matRotation.m9 + matTranslation.m15*matRotation.m13;
|
||||
matTransform.m14 = matTranslation.m12*matRotation.m2 + matTranslation.m13*matRotation.m6 + matTranslation.m14*matRotation.m10 + matTranslation.m15*matRotation.m14;
|
||||
matTransform.m15 = matTranslation.m12*matRotation.m3 + matTranslation.m13*matRotation.m7 + matTranslation.m14*matRotation.m11 + matTranslation.m15*matRotation.m15;
|
||||
|
||||
camera->target.x = camera->position.x - matTransform.m12;
|
||||
camera->target.y = camera->position.y - matTransform.m13;
|
||||
camera->target.z = camera->position.z - matTransform.m14;
|
||||
|
||||
// Camera position update
|
||||
// NOTE: On CAMERA_FIRST_PERSON player Y-movement is limited to player 'eyes position'
|
||||
camera->position.y = CAMERA.playerEyesPosition;
|
||||
|
||||
// Camera swinging (y-movement), only when walking (some key pressed)
|
||||
for (int i = 0; i < 6; i++) if (direction[i]) { swingCounter += GetFrameTime(); break; }
|
||||
camera->position.y -= sinf(2*PI*CAMERA_FIRST_PERSON_STEP_FREQUENCY*swingCounter)*CAMERA_FIRST_PERSON_SWINGING_DELTA;
|
||||
|
||||
// Camera waiving (xz-movement), only when walking (some key pressed)
|
||||
camera->up.x = sinf(2*PI*CAMERA_FIRST_PERSON_STEP_FREQUENCY*swingCounter)*CAMERA_FIRST_PERSON_TILTING_DELTA;
|
||||
camera->up.z = -sinf(2*PI*CAMERA_FIRST_PERSON_STEP_FREQUENCY*swingCounter)*CAMERA_FIRST_PERSON_TILTING_DELTA;
|
||||
|
||||
} break;
|
||||
case CAMERA_THIRD_PERSON: // Camera moves as in a third-person game, following target at a distance, controls are configurable
|
||||
{
|
||||
camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_FRONT] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_LEFT] +
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])*PLAYER_MOVEMENT_SENSITIVITY*GetFrameTime();
|
||||
|
||||
camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] -
|
||||
sinf(CAMERA.angle.y)*direction[MOVE_BACK] +
|
||||
1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])*PLAYER_MOVEMENT_SENSITIVITY*GetFrameTime();
|
||||
|
||||
camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_FRONT] +
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_LEFT] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])*PLAYER_MOVEMENT_SENSITIVITY*GetFrameTime();
|
||||
|
||||
// Camera orientation calculation
|
||||
CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
|
||||
// Angle clamp
|
||||
if (CAMERA.angle.y > CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD;
|
||||
else if (CAMERA.angle.y < CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD;
|
||||
|
||||
// Camera zoom
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
|
||||
// Camera distance clamp
|
||||
if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;
|
||||
|
||||
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;
|
||||
case CAMERA_CUSTOM: break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
// TODO can we provide compatibility with the old rcamera usage?
|
||||
#if 0
|
||||
|
||||
// Set camera pan key to combine with mouse movement (free camera)
|
||||
void SetCameraPanControl(int keyPan) { CAMERA.panControl = keyPan; }
|
||||
|
|
@ -554,5 +475,7 @@ void SetCameraMoveControls(int keyFront, int keyBack, int keyRight, int keyLeft,
|
|||
CAMERA.moveControl[MOVE_UP] = keyUp;
|
||||
CAMERA.moveControl[MOVE_DOWN] = keyDown;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif // CAMERA_IMPLEMENTATION
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user