Fixes to ghosts

This commit is contained in:
2023-10-13 22:25:01 -04:00
parent e4ff110d85
commit 1581065661
5 changed files with 87 additions and 73 deletions

View File

@@ -4,7 +4,9 @@
#include "ghost.h"
float pi = glm::pi<float>();
int counter = 0;
float direction_persist_factor = 0.02;
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.z = rand_float(zmin, zmax);
y_offset = rand_float(0.0, 3.14);
yawr = rand_float(0.0, 6.28);
y_offset = rand_float(0.0, pi);
yawr = rand_float(0.0, pi*2.0f);
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) {
first_frame = true;
prev_move_time = curr_time;
return;
}
moved.x = -sin(yawr) + cos(yawr);
moved.z = cos(yawr) + sin(yawr);
float diff_time = curr_time - prev_move_time;
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;
pos += moved * timed;
bool reset_yaw = false;
if (moved.x < xmin) {
if (pos.x < xmin) {
reset_yaw = true;
moved.x = xmin;
} else if (moved.x > xmax) {
pos.x = xmin;
} else if (pos.x > xmax) {
reset_yaw = true;
moved.x = xmax;
pos.x = xmax;
}
if (moved.z < zmin) {
if (pos.z < zmin) {
reset_yaw = true;
moved.z = zmin;
} else if (moved.z > zmax) {
pos.z = zmin;
} else if (pos.z > zmax) {
reset_yaw = true;
moved.z = zmax;
pos.z = zmax;
}
// TODO make this face origin
if (reset_yaw) {
yawr = rand_float(0.0, 6.28);
yawr = rand_float(0.0, pi * 2.0);
} 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) {
// initialize to identity matrix
glm::mat4 ghost_model = glm::mat4(1.0f);
// apply location
ghost_model = glm::translate(ghost_model, pos);
glm::vec3 ghost_direction = pos - camera_pos;
ghost_model = glm::rotate(ghost_model, atan2f(ghost_direction.x, ghost_direction.z), glm::vec3(0.0f, 1.0f, 0.0f));
// apply direction
// 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;
}