Fixing Camera bugs

This commit is contained in:
2019-12-09 12:24:34 +02:00
parent b51c63ea0c
commit b936db0ce9
4 changed files with 27 additions and 44 deletions
+15 -18
View File
@@ -15,6 +15,7 @@ Camera::Camera()
vertical_camera_speed = 0.5; vertical_camera_speed = 0.5;
vertical_camera_clamp_up = 90; vertical_camera_clamp_up = 90;
vertical_camera_clamp_down = -90; vertical_camera_clamp_down = -90;
movement_speed = 1.f;
} }
Vec3f Camera::GetForward() { Vec3f Camera::GetForward() {
@@ -56,35 +57,31 @@ void Camera::SetFarPlane(float far_val) {
far_plane = far_val; far_plane = far_val;
} }
void Camera::rotate_hor(float d_angle) {
void Camera::rotate_camera_right() { rotation.y += d_angle * horizontal_camera_speed;
rotation.y += horizontal_camera_speed;
} }
void Camera::rotate_camera_left() { void Camera::rotate_ver(float d_angle) {
rotation.y -= horizontal_camera_speed; rotation.x += d_angle * vertical_camera_speed;
} rotation.x = std::fmin(rotation.x, vertical_camera_clamp_up);
void Camera::rotate_camera_up() { rotation.x = std::fmax(rotation.x, vertical_camera_clamp_down);
rotation.x += vertical_camera_speed;
if (rotation.x > vertical_camera_clamp_up) rotation.x = 90;
}
void Camera::rotate_camera_down() {
rotation.x -= vertical_camera_speed;
if (rotation.x < vertical_camera_clamp_down) rotation.x = -90;
} }
#define MOVEMENT_SPEED 0.05f;
void Camera::move_camera_left() { void Camera::move_camera_left() {
position = position - right * MOVEMENT_SPEED; position = position - right * movement_speed;
} }
void Camera::move_camera_right() { void Camera::move_camera_right() {
position = position + right * MOVEMENT_SPEED; position = position + right * movement_speed;
} }
void Camera::move_camera_forward() { void Camera::move_camera_forward() {
position = position + forward * MOVEMENT_SPEED; position = position + forward * movement_speed;
} }
void Camera::move_camera_backward() { void Camera::move_camera_backward() {
position = position - forward * MOVEMENT_SPEED; position = position - forward * movement_speed;
}
void Camera::SetMovementSpeed(float speed) {
movement_speed = speed;
} }
void Camera::ApplyChanges() { void Camera::ApplyChanges() {
+4 -4
View File
@@ -16,6 +16,7 @@ private:
float vertical_camera_speed; float vertical_camera_speed;
float vertical_camera_clamp_up; float vertical_camera_clamp_up;
float vertical_camera_clamp_down; float vertical_camera_clamp_down;
float movement_speed;
public: public:
Camera(); Camera();
@@ -30,12 +31,11 @@ public:
void SetHorizontalRotSpeed(float speed); void SetHorizontalRotSpeed(float speed);
void SetClampRotUp(float angle); void SetClampRotUp(float angle);
void SetClampRotDown(float angle); void SetClampRotDown(float angle);
void SetMovementSpeed(float speed);
Vec3f GetForward(); Vec3f GetForward();
void rotate_camera_right(); void rotate_hor(float d_angle);
void rotate_camera_left(); void rotate_ver(float d_angle);
void rotate_camera_up();
void rotate_camera_down();
void move_camera_left(); void move_camera_left();
void move_camera_right(); void move_camera_right();
void move_camera_forward(); void move_camera_forward();
+4 -20
View File
@@ -33,30 +33,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return Msg.wParam; return Msg.wParam;
} }
bool HandleMouseMovement() { void HandleMouseMovement() {
int xPos, yPos;
POINT point; POINT point;
GetCursorPos(&point); GetCursorPos(&point);
bool movement_detected_x = true; camera.rotate_hor(point.x - prev_mouse_x);
bool movement_detected_y = true; camera.rotate_ver(point.y - prev_mouse_y);
if (point.x > prev_mouse_x)
camera.rotate_camera_right();
else if (point.x < prev_mouse_x)
camera.rotate_camera_left();
else
movement_detected_x = false;
if (point.y > prev_mouse_y)
camera.rotate_camera_up();
else if (point.y < prev_mouse_y)
camera.rotate_camera_down();
else
movement_detected_y = false;
SetCursorPos(prev_mouse_x, prev_mouse_y); SetCursorPos(prev_mouse_x, prev_mouse_y);
return movement_detected_x || movement_detected_y;
} }
bool HandleButtonPressed() { bool HandleButtonPressed() {
@@ -73,7 +57,6 @@ bool HandleButtonPressed() {
void CALLBACK FixedUpdate(HWND hwnd, UINT message, UINT uInt, DWORD dWord) void CALLBACK FixedUpdate(HWND hwnd, UINT message, UINT uInt, DWORD dWord)
{ {
HandleMouseMovement();
HandleButtonPressed(); HandleButtonPressed();
camera.ApplyChanges(); camera.ApplyChanges();
render(); render();
@@ -85,6 +68,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
switch (message) switch (message)
{ {
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
HandleMouseMovement();
break; break;
case WM_RBUTTONDOWN: case WM_RBUTTONDOWN:
break; break;
+4 -2
View File
@@ -9,8 +9,8 @@
#include <ctime> #include <ctime>
#include "camera.h" #include "camera.h"
#define HORIZONTAL_CAMERA_SPEED 1 #define HORIZONTAL_CAMERA_SPEED 0.1
#define VERTICAL_CAMERA_SPEED 1 #define VERTICAL_CAMERA_SPEED 0.1
#define VERTICAL_CAMERA_CLAMP_UP 90 #define VERTICAL_CAMERA_CLAMP_UP 90
#define VERTICAL_CAMERA_CLAMP_DOWN -90 #define VERTICAL_CAMERA_CLAMP_DOWN -90
#define NEAR_CLIP_PLANE 0 #define NEAR_CLIP_PLANE 0
@@ -18,6 +18,7 @@
#define FOV 30 #define FOV 30
#define DEFAULT_CAMERA_POS Vec3f(0, 0, 5) #define DEFAULT_CAMERA_POS Vec3f(0, 0, 5)
#define DEFAULT_CAMERA_ROT Vec3f(0, 0, 0) #define DEFAULT_CAMERA_ROT Vec3f(0, 0, 0)
#define CAMERA_MOVEMENT_SPEED 1.f
const TGAColor white = TGAColor(255, 255, 255, 255); const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255); const TGAColor red = TGAColor(255, 0, 0, 255);
@@ -204,6 +205,7 @@ void init_camera() {
camera.SetClampRotUp(VERTICAL_CAMERA_CLAMP_UP); camera.SetClampRotUp(VERTICAL_CAMERA_CLAMP_UP);
camera.SetHorizontalRotSpeed(HORIZONTAL_CAMERA_SPEED); camera.SetHorizontalRotSpeed(HORIZONTAL_CAMERA_SPEED);
camera.SetVerticalRotSpeed(VERTICAL_CAMERA_SPEED); camera.SetVerticalRotSpeed(VERTICAL_CAMERA_SPEED);
camera.SetMovementSpeed(CAMERA_MOVEMENT_SPEED);
camera.ApplyChanges(); camera.ApplyChanges();
} }