Make the camera smooth to move around.
This commit is contained in:
@@ -37,17 +37,19 @@ public:
|
||||
float farPlane = 100.0f;
|
||||
float fov = 72.5f;
|
||||
SDL_Event event;
|
||||
|
||||
void keyState () {
|
||||
const Uint8* ks = SDL_GetKeyboardState(nullptr);
|
||||
}
|
||||
|
||||
[[nodiscard]] float framerate() const {
|
||||
return 1.f / this->frameDelta;
|
||||
}
|
||||
|
||||
//Prevent taking a ton of screenshots really fast.
|
||||
void takeScreenshot() const {
|
||||
std::string fName = std::to_string(time(nullptr)) + ".bmp";
|
||||
std::ifstream f (fName, std::ifstream::in);
|
||||
if(f.good()) {
|
||||
std::cout << "Screenshot not taken." << std::endl;
|
||||
f.close();
|
||||
return;
|
||||
}
|
||||
|
@@ -3,105 +3,75 @@
|
||||
#include "../engine/engine.h"
|
||||
#include "entityList.h"
|
||||
|
||||
enum class CAMERAMODE: uint8_t {
|
||||
enum class CameraMode: uint8_t {
|
||||
FREECAM = 0,
|
||||
FOLLOW_PLAYER = 1
|
||||
};
|
||||
class Camera : public Moby {
|
||||
public:
|
||||
uint8_t mode = 0;
|
||||
CameraMode mode = CameraMode::FREECAM;
|
||||
bool takingScreenshot = false;
|
||||
|
||||
void moveForward(float speed) {
|
||||
vector3 forwardAngle(
|
||||
cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)),
|
||||
sin(glm::radians(this->angles.x)),
|
||||
sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x))
|
||||
);
|
||||
//forwardDirection.normalize();
|
||||
//My axis are flipped????
|
||||
this->position.z += speed * -forwardAngle.x;
|
||||
this->position.x += speed * -forwardAngle.z;
|
||||
this->position.y += speed * forwardAngle.y;
|
||||
}
|
||||
|
||||
void moveBack(float speed) {
|
||||
vector3 forwardDirection(
|
||||
cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)),
|
||||
sin(glm::radians(this->angles.x)),
|
||||
sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x))
|
||||
);
|
||||
//forwardDirection.normalize();
|
||||
//My axis are flipped????
|
||||
this->position.z += speed * forwardDirection.x;
|
||||
this->position.x += speed * forwardDirection.z;
|
||||
this->position.y += speed * -forwardDirection.y;
|
||||
}
|
||||
|
||||
void move(float x, float y, float z) {
|
||||
position.x += x;
|
||||
position.y += y;
|
||||
position.z += z;
|
||||
}
|
||||
|
||||
void update() {
|
||||
if (mode == 0) {
|
||||
const Uint8* keyState = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
//TODO: move this to render next.
|
||||
//I suspect this will be more complicated.
|
||||
const Uint8* keyState = SDL_GetKeyboardState(nullptr);
|
||||
if (keyState[SDL_SCANCODE_SPACE] == 1) {
|
||||
this->position.y -= 0.025;
|
||||
}
|
||||
|
||||
if (keyState[SDL_SCANCODE_LSHIFT] == 1) {
|
||||
this->position.y += 0.025;
|
||||
}
|
||||
|
||||
if (keyState[SDL_SCANCODE_LEFT] == 1) {
|
||||
this->angles.y +=2.0f;
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_RIGHT] == 1) {
|
||||
this->angles.y -=2.0f;
|
||||
//std::cout << this->angles.y << std::endl;
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_UP] == 1) {
|
||||
this->angles.x +=2.0f;
|
||||
//std::cout << this->angles.x << std::endl;
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_DOWN] == 1) {
|
||||
this->angles.x -=2.0f;
|
||||
//std::cout << this->angles.x << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void render() {
|
||||
if (mode == CameraMode::FREECAM) {
|
||||
//TODO: check this once every frame instead of here.
|
||||
const Uint8* keyState = SDL_GetKeyboardState(nullptr);
|
||||
if (keyState[SDL_SCANCODE_0] == 1) {
|
||||
this->takingScreenshot = true;
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_S] == 1) {
|
||||
moveBack(0.125);
|
||||
move(bAngle(),2);
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_W] == 1) {
|
||||
//this->position.z = this->position.z-0.1f;
|
||||
moveForward(0.125);
|
||||
move(fAngle(),2);
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_A] == 1) {
|
||||
this->position.x = this->position.x-0.025f;;
|
||||
move(lAngle(), 2);
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_D] == 1) {
|
||||
this->position.x = this->position.x+0.025f;
|
||||
}
|
||||
|
||||
if (keyState[SDL_SCANCODE_SPACE] == 1) {
|
||||
this->position.y -= 0.025;
|
||||
}
|
||||
|
||||
if (keyState[SDL_SCANCODE_LSHIFT] == 1) {
|
||||
this->position.y += 0.025;
|
||||
}
|
||||
|
||||
if (keyState[SDL_SCANCODE_LEFT] == 1) {
|
||||
this->angles.y +=2.0f;
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_RIGHT] == 1) {
|
||||
this->angles.y -=2.0f;
|
||||
//std::cout << this->angles.y << std::endl;
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_UP] == 1) {
|
||||
this->angles.x +=2.0f;
|
||||
//std::cout << this->angles.x << std::endl;
|
||||
}
|
||||
if (keyState[SDL_SCANCODE_DOWN] == 1) {
|
||||
this->angles.x -=2.0f;
|
||||
//std::cout << this->angles.x << std::endl;
|
||||
move(rAngle(),2);
|
||||
}
|
||||
}
|
||||
this->angles.clamp();
|
||||
}
|
||||
|
||||
void render() {
|
||||
//glTranslatef(-position.x, -position.y, -position.z);
|
||||
// up vector
|
||||
glRotatef(-angles.x,1.0f, 0.0f, 0.0f);
|
||||
glRotatef(-angles.y,0.0f, 1.0f, 0.0f);
|
||||
|
||||
glTranslatef(0.0f, 0.0f, 0.0f);
|
||||
gluLookAt(position.x, position.y, position.z, // camera position
|
||||
position.x, position.y, -engine->farPlane+position.z, // target position, We're always *looking at* the far plane straight ahead so the camera never turns around.
|
||||
0.0f, 1.0f, 0.0f);
|
||||
upVector.x,upVector.y,upVector.z);
|
||||
|
||||
if (engine->debug && engine->tickCount %64 == 0) {
|
||||
std::cout << "Camera:" << std::endl;
|
||||
|
@@ -7,4 +7,48 @@ public:
|
||||
float velocity = 0;
|
||||
angle angles = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
|
||||
angle velAngles = {0,0,0}; //The angle of an entities velocity.
|
||||
vector3 upVector = {0.0f,1.0f,0.0f};
|
||||
|
||||
void move(angle a, float speed) {
|
||||
this->position.z += (speed*engine->frameDelta) * a.x;
|
||||
this->position.y += (speed*engine->frameDelta) * a.y;
|
||||
this->position.x += (speed*engine->frameDelta) * a.z;
|
||||
}
|
||||
|
||||
//forward angle
|
||||
angle fAngle() {
|
||||
angle a;
|
||||
a.x = -(cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)));
|
||||
a.y = sin(glm::radians(this->angles.x));
|
||||
a.z = -(sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)));
|
||||
return a;
|
||||
}
|
||||
|
||||
//back angle
|
||||
angle bAngle() {
|
||||
angle a;
|
||||
a.x = cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x));
|
||||
a.y = -(sin(glm::radians(this->angles.x)));
|
||||
a.z = sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x));
|
||||
return a;
|
||||
}
|
||||
|
||||
//left angle
|
||||
angle lAngle() {
|
||||
angle f = fAngle();
|
||||
angle a;
|
||||
a.x = f.y * upVector.z - f.z * upVector.y;
|
||||
a.y = f.z * upVector.x - f.x * upVector.z;
|
||||
a.z = f.x * upVector.y - f.y * upVector.x;
|
||||
return a;
|
||||
}
|
||||
|
||||
angle rAngle() {
|
||||
angle f = fAngle();
|
||||
angle a;
|
||||
a.x = -(f.y * upVector.z - f.z * upVector.y);
|
||||
a.y = (f.z * upVector.x - f.x * upVector.z);
|
||||
a.z = -(f.x * upVector.y - f.y * upVector.x);
|
||||
return a;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user