Fixes to ghosts
This commit is contained in:
79
ghost.cpp
79
ghost.cpp
@@ -4,7 +4,9 @@
|
|||||||
|
|
||||||
#include "ghost.h"
|
#include "ghost.h"
|
||||||
|
|
||||||
|
float pi = glm::pi<float>();
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
float direction_persist_factor = 0.02;
|
||||||
|
|
||||||
Ghost::Ghost(float xmin, float xmax, float zmin, float zmax) {
|
Ghost::Ghost(float xmin, float xmax, float zmin, float zmax) {
|
||||||
|
|
||||||
@@ -20,65 +22,82 @@ Ghost::Ghost(float xmin, float xmax, float zmin, float zmax) {
|
|||||||
pos.y = 2.5;
|
pos.y = 2.5;
|
||||||
pos.z = rand_float(zmin, zmax);
|
pos.z = rand_float(zmin, zmax);
|
||||||
|
|
||||||
y_offset = rand_float(0.0, 3.14);
|
y_offset = rand_float(0.0, pi);
|
||||||
yawr = rand_float(0.0, 6.28);
|
yawr = rand_float(0.0, pi*2.0f);
|
||||||
|
|
||||||
first_frame = false;
|
first_frame = false;
|
||||||
|
|
||||||
|
direction_persist = 0.0f;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ghost::apply_movement() {
|
void Ghost::apply_movement(float curr_time, float timed) {
|
||||||
|
|
||||||
float curr_time = static_cast<float>(glfwGetTime());
|
// vertical movement
|
||||||
|
pos.y = 2.5 + sin(curr_time + y_offset);
|
||||||
|
|
||||||
if (!first_frame) {
|
moved.x = -sin(yawr) + cos(yawr);
|
||||||
first_frame = true;
|
moved.z = cos(yawr) + sin(yawr);
|
||||||
prev_move_time = curr_time;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float diff_time = curr_time - prev_move_time;
|
pos += moved * timed;
|
||||||
prev_move_time = curr_time;
|
|
||||||
|
|
||||||
glm::vec3 moved;
|
|
||||||
moved.x = pos.x + (-sin(yawr) + cos(yawr)) * diff_time;
|
|
||||||
moved.y = 2.5 + sin(curr_time + y_offset);
|
|
||||||
moved.z = pos.z + (cos(yawr) + sin(yawr)) * diff_time;
|
|
||||||
|
|
||||||
bool reset_yaw = false;
|
bool reset_yaw = false;
|
||||||
|
|
||||||
if (moved.x < xmin) {
|
if (pos.x < xmin) {
|
||||||
reset_yaw = true;
|
reset_yaw = true;
|
||||||
moved.x = xmin;
|
pos.x = xmin;
|
||||||
} else if (moved.x > xmax) {
|
} else if (pos.x > xmax) {
|
||||||
reset_yaw = true;
|
reset_yaw = true;
|
||||||
moved.x = xmax;
|
pos.x = xmax;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (moved.z < zmin) {
|
if (pos.z < zmin) {
|
||||||
reset_yaw = true;
|
reset_yaw = true;
|
||||||
moved.z = zmin;
|
pos.z = zmin;
|
||||||
} else if (moved.z > zmax) {
|
} else if (pos.z > zmax) {
|
||||||
reset_yaw = true;
|
reset_yaw = true;
|
||||||
moved.z = zmax;
|
pos.z = zmax;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO make this face origin
|
||||||
if (reset_yaw) {
|
if (reset_yaw) {
|
||||||
yawr = rand_float(0.0, 6.28);
|
yawr = rand_float(0.0, pi * 2.0);
|
||||||
} else {
|
} else {
|
||||||
yawr += rand_float(-0.3, 0.3);
|
yawr += rand_float(-0.05, 0.05);
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = moved;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 Ghost::get_model(glm::vec3 &camera_pos) {
|
glm::mat4 Ghost::get_model(glm::vec3 &camera_pos) {
|
||||||
|
|
||||||
|
// initialize to identity matrix
|
||||||
glm::mat4 ghost_model = glm::mat4(1.0f);
|
glm::mat4 ghost_model = glm::mat4(1.0f);
|
||||||
|
// apply location
|
||||||
ghost_model = glm::translate(ghost_model, pos);
|
ghost_model = glm::translate(ghost_model, pos);
|
||||||
glm::vec3 ghost_direction = pos - camera_pos;
|
// apply direction
|
||||||
ghost_model = glm::rotate(ghost_model, atan2f(ghost_direction.x, ghost_direction.z), glm::vec3(0.0f, 1.0f, 0.0f));
|
// the cross product helps us determine which direction it's going relative to player
|
||||||
|
glm::vec3 ghost_to_player = pos - camera_pos;
|
||||||
|
glm::vec3 crossed = glm::cross(ghost_to_player, moved);
|
||||||
|
//float dist2 = ghost_to_player.x * ghost_to_player.x + ghost_to_player.z * ghost_to_player.z;
|
||||||
|
float theta = atan2f(ghost_to_player.x, ghost_to_player.z);
|
||||||
|
if (crossed.y < 0.0) {
|
||||||
|
direction_persist = (1.0 - direction_persist_factor) * direction_persist - direction_persist_factor;
|
||||||
|
} else {
|
||||||
|
direction_persist = (1.0 - direction_persist_factor) * direction_persist + direction_persist_factor;
|
||||||
|
}
|
||||||
|
if (direction_persist < 0.0) {
|
||||||
|
theta += pi;
|
||||||
|
}
|
||||||
|
ghost_model = glm::rotate(ghost_model, theta, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
/*
|
||||||
|
if (dist2 > 256) {
|
||||||
|
yawr = theta;
|
||||||
|
}
|
||||||
|
if (ghost_id == 0) {
|
||||||
|
printf("(%f, %f, %f)\n", pos.x, pos.y, pos.z);
|
||||||
|
printf("%f %f %f %f\n", xmin, xmax, zmin, zmax);
|
||||||
|
}
|
||||||
|
*/
|
||||||
return ghost_model;
|
return ghost_model;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
4
ghost.h
4
ghost.h
@@ -6,12 +6,13 @@
|
|||||||
class Ghost {
|
class Ghost {
|
||||||
public:
|
public:
|
||||||
Ghost(float xmin, float xmax, float zmin, float zmax);
|
Ghost(float xmin, float xmax, float zmin, float zmax);
|
||||||
void apply_movement();
|
void apply_movement(float curr_time, float timed);
|
||||||
glm::mat4 get_model(glm::vec3 &camera_pos);
|
glm::mat4 get_model(glm::vec3 &camera_pos);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int ghost_id;
|
int ghost_id;
|
||||||
bool first_frame;
|
bool first_frame;
|
||||||
|
glm::vec3 moved;
|
||||||
glm::vec3 pos;
|
glm::vec3 pos;
|
||||||
float yawr;
|
float yawr;
|
||||||
float xmin;
|
float xmin;
|
||||||
@@ -20,6 +21,7 @@ class Ghost {
|
|||||||
float zmax;
|
float zmax;
|
||||||
float y_offset;
|
float y_offset;
|
||||||
float prev_move_time;
|
float prev_move_time;
|
||||||
|
float direction_persist;
|
||||||
};
|
};
|
||||||
|
|
||||||
float rand_float(float rmin, float rmax);
|
float rand_float(float rmin, float rmax);
|
||||||
|
|||||||
@@ -27,20 +27,11 @@ void set_light_front(int xoffset, int yoffset);
|
|||||||
Player *player;
|
Player *player;
|
||||||
|
|
||||||
// settings
|
// settings
|
||||||
const unsigned int WINDOWWIDTH = 1600;
|
unsigned int WINDOWWIDTH = 1600;
|
||||||
const unsigned int WINDOWHEIGHT = 900;
|
unsigned int WINDOWHEIGHT = 900;
|
||||||
|
|
||||||
// camera
|
// camera
|
||||||
bool mousemove = false;
|
bool first_frame = false;
|
||||||
float xpersist = 0.0f;
|
|
||||||
float ypersist = 0.0f;
|
|
||||||
|
|
||||||
bool first_mouse = true;
|
|
||||||
float yaw = 0.0f;
|
|
||||||
float pitch = 0.0f;
|
|
||||||
float lastX = WINDOWWIDTH / 2.0;
|
|
||||||
float lastY = WINDOWHEIGHT / 2.0;
|
|
||||||
|
|
||||||
float timed = 0.0f;
|
float timed = 0.0f;
|
||||||
float last_frame = 0.0f;
|
float last_frame = 0.0f;
|
||||||
|
|
||||||
@@ -88,7 +79,11 @@ int main(int argc, char *argv[])
|
|||||||
// num surfaces * 6 (vertices per point) * 6 (floats per point)
|
// num surfaces * 6 (vertices per point) * 6 (floats per point)
|
||||||
int num_wall_vertices = num_walls * 6 * 6;
|
int num_wall_vertices = num_walls * 6 * 6;
|
||||||
wall_vertices = (float *)calloc(sizeof(float), num_wall_vertices);
|
wall_vertices = (float *)calloc(sizeof(float), num_wall_vertices);
|
||||||
int xmin_wall, xmax_wall, zmin_wall, zmax_wall;
|
float xmin_wall, xmax_wall, zmin_wall, zmax_wall;
|
||||||
|
xmin_wall = 9999.9;
|
||||||
|
xmax_wall = -9999.9;
|
||||||
|
zmin_wall = 9999.9;
|
||||||
|
zmax_wall = -9999.9;
|
||||||
// read walls
|
// read walls
|
||||||
for (int i = 0; i < num_walls; i++) {
|
for (int i = 0; i < num_walls; i++) {
|
||||||
for (int j = 0; j < 6; j++) {
|
for (int j = 0; j < 6; j++) {
|
||||||
@@ -149,7 +144,13 @@ int main(int argc, char *argv[])
|
|||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(WINDOWWIDTH, WINDOWHEIGHT, "Ghostland!", NULL, NULL);
|
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
|
||||||
|
const GLFWvidmode *monitor_mode = glfwGetVideoMode(monitor);
|
||||||
|
|
||||||
|
WINDOWWIDTH = monitor_mode->width;
|
||||||
|
WINDOWHEIGHT = monitor_mode->height;
|
||||||
|
|
||||||
|
GLFWwindow* window = glfwCreateWindow(WINDOWWIDTH, WINDOWHEIGHT, "Ghostland!", monitor, NULL);
|
||||||
if (window == NULL)
|
if (window == NULL)
|
||||||
{
|
{
|
||||||
printf("Failed to create GLFW window.\n");
|
printf("Failed to create GLFW window.\n");
|
||||||
@@ -166,6 +167,7 @@ int main(int argc, char *argv[])
|
|||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||||
{
|
{
|
||||||
printf("Failed to initialize GLAD.\n");
|
printf("Failed to initialize GLAD.\n");
|
||||||
|
glfwTerminate();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,12 +272,12 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
// ghost vertices
|
// ghost vertices
|
||||||
float ghost_vertices[] = {
|
float ghost_vertices[] = {
|
||||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
|
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
|
||||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
||||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
||||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
|
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
|
||||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||||
};
|
};
|
||||||
unsigned int ghostVBO, ghostVAO;
|
unsigned int ghostVBO, ghostVAO;
|
||||||
glGenVertexArrays(1, &ghostVAO);
|
glGenVertexArrays(1, &ghostVAO);
|
||||||
@@ -327,7 +329,7 @@ int main(int argc, char *argv[])
|
|||||||
float smallcutoff = glm::cos(glm::radians(7.5f));
|
float smallcutoff = glm::cos(glm::radians(7.5f));
|
||||||
|
|
||||||
std::vector<Ghost *> ghosts;
|
std::vector<Ghost *> ghosts;
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 800; i++) {
|
||||||
ghosts.push_back(new Ghost(xmin_wall, xmax_wall, zmin_wall, zmax_wall));
|
ghosts.push_back(new Ghost(xmin_wall, xmax_wall, zmin_wall, zmax_wall));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,8 +361,10 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
timed = current_frame - last_frame;
|
timed = current_frame - last_frame;
|
||||||
last_frame = current_frame;
|
last_frame = current_frame;
|
||||||
|
if (!first_frame) {
|
||||||
mousemove = false;
|
first_frame = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
player->process_input(window);
|
player->process_input(window);
|
||||||
|
|
||||||
@@ -451,13 +455,13 @@ int main(int argc, char *argv[])
|
|||||||
set_uniform(ghost_program, viewC, view);
|
set_uniform(ghost_program, viewC, view);
|
||||||
for (int i = 0; i < ghosts.size(); i++) {
|
for (int i = 0; i < ghosts.size(); i++) {
|
||||||
glm::mat4 ghost_model = ghosts[i]->get_model(camera_pos);
|
glm::mat4 ghost_model = ghosts[i]->get_model(camera_pos);
|
||||||
ghosts[i]->apply_movement();
|
ghosts[i]->apply_movement(current_frame, timed);
|
||||||
set_uniform(ghost_program, modelC, ghost_model);
|
set_uniform(ghost_program, modelC, ghost_model);
|
||||||
glBindVertexArray(ghostVAO);
|
glBindVertexArray(ghostVAO);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, (3 + 3 + 2) * 6);
|
glDrawArrays(GL_TRIANGLES, 0, (3 + 3 + 2) * 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
player->apply_movement();
|
player->apply_movement(timed);
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|||||||
21
player.cpp
21
player.cpp
@@ -6,9 +6,9 @@
|
|||||||
Player::Player(glm::vec3 startpos, float startyaw) {
|
Player::Player(glm::vec3 startpos, float startyaw) {
|
||||||
|
|
||||||
position = startpos;
|
position = startpos;
|
||||||
yaw = startyaw;
|
yaw = 45.0f + startyaw;
|
||||||
|
|
||||||
pitch = 0.0f;
|
pitch = 45.0f;
|
||||||
first_mouse = false;
|
first_mouse = false;
|
||||||
first_frame = false;
|
first_frame = false;
|
||||||
light_xpersist = 0.0f;
|
light_xpersist = 0.0f;
|
||||||
@@ -203,18 +203,7 @@ void Player::set_light_offset(float xoffset, float yoffset) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::apply_movement() {
|
void Player::apply_movement(float timed) {
|
||||||
|
|
||||||
float curr_time = static_cast<float>(glfwGetTime());
|
|
||||||
|
|
||||||
if (!first_frame) {
|
|
||||||
prev_move_time = curr_time;
|
|
||||||
first_frame = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float diff_time = curr_time - prev_move_time;
|
|
||||||
prev_move_time = curr_time;
|
|
||||||
|
|
||||||
// mouse movement
|
// mouse movement
|
||||||
glm::vec3 front;
|
glm::vec3 front;
|
||||||
@@ -230,7 +219,7 @@ void Player::apply_movement() {
|
|||||||
|
|
||||||
// positional movement
|
// positional movement
|
||||||
glm::vec3 movement = velocity;
|
glm::vec3 movement = velocity;
|
||||||
movement *= diff_time;
|
movement *= timed;
|
||||||
glm::vec3 intersected = check_intersection(movement);
|
glm::vec3 intersected = check_intersection(movement);
|
||||||
if (is_in_air()) {
|
if (is_in_air()) {
|
||||||
if (intersected.x == 0.0) {
|
if (intersected.x == 0.0) {
|
||||||
@@ -253,7 +242,7 @@ void Player::apply_movement() {
|
|||||||
position.y = 0.0f;
|
position.y = 0.0f;
|
||||||
velocity.y = 0.0f;
|
velocity.y = 0.0f;
|
||||||
} else {
|
} else {
|
||||||
velocity.y += diff_time * vacceleration;
|
velocity.y += timed * vacceleration;
|
||||||
}
|
}
|
||||||
|
|
||||||
position.x += intersected.x;
|
position.x += intersected.x;
|
||||||
|
|||||||
2
player.h
2
player.h
@@ -38,7 +38,7 @@ class Player {
|
|||||||
void process_input(GLFWwindow *window);
|
void process_input(GLFWwindow *window);
|
||||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||||
bool is_in_air();
|
bool is_in_air();
|
||||||
void apply_movement();
|
void apply_movement(float timed);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float yaw;
|
float yaw;
|
||||||
|
|||||||
Reference in New Issue
Block a user