Camera fixes & Partially working calcAngle

This commit is contained in:
2023-12-05 10:26:10 -05:00
parent 6cc7a19fbf
commit 278b06b321
6 changed files with 35 additions and 39 deletions

View File

@@ -17,6 +17,7 @@ add_executable(SDL3D src/main.cpp
src/types/skybox.h
src/types/moby.h
src/types/animationSequence.h
src/engine/math.h
)
find_package(OpenGL)

16
src/engine/math.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include <cmath>
#include "../types/vector3.h"
namespace math {
float distance(Position sP, Position eP) {
return sqrt(pow(eP.x - sP.x, 2) + pow(eP.y - sP.y, 2) + pow(eP.z - sP.z, 2));
}
Angle calcAngle(Position sP, Position eP) {
Angle returned;
returned.x = -(asinf((eP.y - sP.y) / distance(sP, eP)) * 180.0f / M_PI);
returned.y = -(atan2f(eP.x - sP.x, eP.z - sP.z) / M_PI * 180.0f + 180.0f);
return returned;
}
}

View File

@@ -2,6 +2,7 @@
#include "moby.h"
#include "player.h"
#include "../engine/engine.h"
#include "../engine/math.h"
#include <glm/glm.hpp>
#include "entityList.h"
@@ -34,7 +35,10 @@ public:
this->cameraMode = CameraMode::THIRD_PERSON;
if (cameraMode == CameraMode::THIRD_PERSON) {
this->position = getPlayer()->cameraPoint(2);
std::cout << "Calculated Pitch: " << math::calcAngle(position,getPlayer()->position).x << " Calculated Yaw: " << math::calcAngle(position,getPlayer()->position).y << std::endl;
//this->position = getPlayer()->cameraPoint(2);
this->angle.x = math::calcAngle(position,getPlayer()->position).x;
//this->angle.y = math::calcAngle(position,getPlayer()->position).y;
}
//if (engine->frameCount == 5000)
@@ -69,20 +73,21 @@ public:
this->angle.y -=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_UP] == 1) {
this->angle.x +=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_DOWN] == 1) {
this->angle.x -=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_DOWN] == 1) {
this->angle.x +=75.0f*engine->frameDelta;
}
}
this->angle.clampToViewAngle();
glRotatef(-angle.x,1.0f, 0.0f, 0.0f);
this->angle.clamp();
glRotatef(angle.x,1.0f, 0.0f, 0.0f);
glRotatef(-angle.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.
upVector.x,upVector.y,upVector.z);
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.
upVector.x,upVector.y,upVector.z);
}
void post_render() {

View File

@@ -19,7 +19,7 @@ public:
Angle fAngle() {
Angle a;
a.x = (cos(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x)));
a.y = sin(glm::radians(this->angle.x));
a.y = -sin(glm::radians(this->angle.x));
a.z = (sin(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x)));
return a;
}
@@ -28,7 +28,7 @@ public:
Angle bAngle() {
Angle a;
a.x = -cos(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x));
a.y = -(sin(glm::radians(this->angle.x)));
a.y = (sin(glm::radians(this->angle.x)));
a.z = -sin(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x));
return a;
}

View File

@@ -36,7 +36,7 @@ public:
if (engine->frameCount == 1) {
geometry.load("../models/cube.obj");
geometry.scale(0.25f);
position.set(0,0,0);
position.set(0,-2,0);
}
this->angle.y += 75.0*engine->frameDelta;
//this->angle.y = this->angle.y + 75.0f*engine->frameDelta;

View File

@@ -12,7 +12,7 @@ public:
class Angle : public vector3 {
public:
void clampToViewAngle() {
void clamp() {
if (this->x > 89.0f)
this->x = 89.0f;
if (this->x <= -89.0f)
@@ -29,35 +29,9 @@ public:
if(this->z <= -360.0f)
this->z = 0.0;
}
void clampToAngle() {
if (this->x <= -180.0f)
this->x = 180.0f;
if (this->x >= 180.01f)
this->x = -179.9f;
//TODO: Make this entirely seamless by getting the amount they rotated passed -180 and +180 by.
if (this->y <= -180.0f)
this->y = 180.0f;
if (this->y >= 180.01f)
this->y = -179.9f;
if (this->z <= -180.0f)
this->z = 180.0f;
if (this->z >= 180.01f)
this->z = -179.9f;
}
[[nodiscard]] float length() const {
return sqrt(x * x + y * y + z * z);
}
void normalize() {
if (this->length() != 0.0f) {
x /= this->length();
y /= this->length();
z /= this->length();
}
}
};
class Position : public vector3 {