ViewAngle

This commit is contained in:
2023-11-20 17:21:37 -05:00
parent fb0cfb1cef
commit f1830a10d6
3 changed files with 55 additions and 34 deletions

View File

@@ -45,21 +45,21 @@ public:
}
if (engine->keyState[SDL_SCANCODE_LEFT] == 1) {
this->angles.y +=75.0f*engine->frameDelta;
this->angle.y -=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_RIGHT] == 1) {
this->angles.y -=75.0f*engine->frameDelta;
this->angle.y +=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_UP] == 1) {
this->angles.x +=75.0f*engine->frameDelta;
this->angle.x -=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_DOWN] == 1) {
this->angles.x -=75.0f*engine->frameDelta;
this->angle.x +=75.0f*engine->frameDelta;
}
}
this->angles.clamp();
glRotatef(-angles.x,1.0f, 0.0f, 0.0f);
glRotatef(-angles.y,0.0f, 1.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
@@ -69,7 +69,7 @@ public:
if (engine->debug && engine->tickCount %64 == 0) {
std::cout << "Camera:" << std::endl;
std::cout << "X: " << position.x << " Y: " << position.y << " Z: " << position.z << std::endl;
std::cout << "Pitch: " << angles.x << " Yaw: " << angles.y << " Roll: " << angles.z << std::endl;
std::cout << "Pitch: " <<angle.x << " Yaw: " <<angle.y << " Roll: " <<angle.z << std::endl;
}
}

View File

@@ -5,47 +5,47 @@
class Moby : public Entity {
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};
ViewAngle angle = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
//Angle velangle = {0,0,0}; //The angle of an entities velocity.
Vector3 upVector = {0.0f,1.0f,0.0f};
void move(angle a, float speed) {
void move(Angle a, float speed) {
this->position.z += (speed*engine->frameDelta) * a.x;
this->position.y += (speed*engine->frameDelta) * a.y;
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)));
ViewAngle fAngle() {
ViewAngle a;
a.x = -(cos(glm::radians(this->angle.y)) * cos(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;
}
//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));
ViewAngle bAngle() {
ViewAngle a;
a.x = cos(glm::radians(this->angle.y)) * cos(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;
}
//left angle
angle lAngle() {
angle f = fAngle();
angle a;
ViewAngle lAngle() {
ViewAngle f = fAngle();
ViewAngle 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;
ViewAngle rAngle() {
ViewAngle f = fAngle();
ViewAngle 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);

View File

@@ -3,14 +3,35 @@
#include <cmath>
#include <cstdlib>
class vector3{
class Vector3{
public:
float x = 0; //pitch
float y = 0; //yaw
float z = 0; //roll
};
class angle : public vector3 {
class Angle : public Vector3 {
private:
void clamp() {
if (this->x <= -360.0f)
this->x = 0.0f;
if (this->x >= 360.0f)
this->x = 0.0f;
//TODO: Make this entirely seamless by getting the amount they rotated passed -180 and +180 by.
if (this->y <= -360.0f)
this->y = 0.0f;
if (this->y >= 360.0f)
this->y = 0.0f;
if (this->z >= 360.0f)
this->z = 0.0f;
if(this->z <= -360.0f)
this->z = 0.0f;
}
};
class ViewAngle : public Angle {
public:
void clamp() {
if (this->x > 89.0f)
@@ -31,7 +52,7 @@ public:
}
};
class Position : public vector3 {
class Position : public Vector3 {
public:
void set(Position p) {
@@ -46,13 +67,13 @@ public:
this->z = p->z;
}
void set(vector3 v) {
void set(Vector3 v) {
this->x = v.x;
this->y = v.y;
this->z = v.z;
}
void set(vector3* v) {
void set(Vector3* v) {
this->x = v->x;
this->y = v->y;
this->z = v->z;