+ | Portal teleportation

This commit is contained in:
TheRedShip
2025-02-04 22:36:10 +01:00
parent 9b3fb7d2b1
commit b027a8e2b0
6 changed files with 67 additions and 16 deletions

View File

@ -26,6 +26,8 @@ struct GPUCamera
int bounce;
};
class Scene;
class Camera
{
public:
@ -34,12 +36,14 @@ class Camera
~Camera(void);
void update(float deltaTime);
void update(Scene *scene, float deltaTime);
void processMouse(float xoffset, float yoffset, bool constrainPitch);
void processKeyboard(bool forward, bool backward, bool left, bool right, bool up, bool down);
void updateCameraVectors();
void portalTeleport(Scene *scene);
glm::vec3 getPosition();
glm::vec2 getDirection();
glm::mat4 getViewMatrix();

View File

@ -22,10 +22,10 @@ pl 0 2 0 0 -1 0 0 // ceiling
pl 0 -2 0 0 1 0 2 // floor
qu -1 1.999 -1 2 0 0 0 0 2 6
qu -1 1.999 -1 0 0 2 2 0 0 1 6
OBJ scenes/obj/Dragon_800K.obj -0.5 0 0.55 5 0 90 0
OBJ scenes/obj/Dragon_800K.obj 0.5 0 -0.55 5 0 -90 0
po -1.99 -0.5 -0.5 0 1 0 0 0 1 1 4
po 1.99 -0.5 -0.5 0 1 0 0 0 1 0 4
po -1.99 -0.5 -0.5 0 1 0 0 0 1 0 4
po 1.99 -0.5 -0.5 0 1 0 0 0 1 1 4

View File

@ -19,7 +19,7 @@ cu 0 10 0 5 5 5 3
sp 0 10 0 1 4
OBJ obj/lambo.obj 0 1.5 0 1 0 0 0
OBJ scenes/obj/lambo.obj 0 1.5 0 1 0 0 0

View File

@ -3,18 +3,27 @@ CAM 0 2.59881 7 -4.8 -89.9996 0 1 90 5
MAT 100 200 100 0.0 0.0 0.0 // 0
MAT 200 200 200 0.0 0.0 0.0 // 1
MAT 200 200 200 0 0 0 // 2 portal
MAT 200 100 100 0 0 0 // 2 portal
MAT 100 100 200 0 0 0 // 3 portal
MAT 100 200 100 0 0 0 // 4 portal
MAT 200 100 200 0 0 0 // 5 portal
pl 0 0 0 0 1 0 0
# long tunnel
po -1.5 0 4.5 3 0 0 0 3 0 0 2
po -1.5 0 -4.5 3 0 0 0 3 0 1 2
qu -1.5 0 -5 0 3 0 0 0 10 0 1
qu 1.5 0 -5 0 3 0 0 0 10 0 1
qu -1.5 3 -5 3 0 0 0 0 10 0 1
# long tunnel entrance
po -1.5 0 5 3 0 0 0 3 0 0 2
po 9.5 0 0.95 3 0 0 0 3 0 1 2
# long tunnel entrance behind
po -1.5 0 -5 3 0 0 0 3 0 1 3
po 9.5 0 -0.95 3 0 0 0 3 0 0 3
# small tunnel
qu 9.5 0 -1 0 3 0 0 0 2 0 1
@ -22,9 +31,9 @@ qu 12.5 0 -1 0 3 0 0 0 2 0 1
qu 9.5 3 -1 3 0 0 0 0 2 0 1
# small tunnel entrance
po 9.5 0 1 3 0 0 0 3 0 0 2
po -1.5 0 4.49 3 0 0 0 3 0 1 2
# po 9.5 0 1 3 0 0 0 3 0 0 4
# po -1.5 0 4.99 3 0 0 0 3 0 1 4
#small tunnel entrace behind
po 9.5 0 -1 3 0 0 0 3 0 1 2
po -1.5 0 -4.49 3 0 0 0 3 0 0 2
# #small tunnel entrace behind
# po 9.5 0 -1 3 0 0 0 3 0 1 5
# po -1.5 0 -4.99 3 0 0 0 3 0 0 5

View File

@ -36,7 +36,7 @@ void Camera::updateCameraVectors()
_up = glm::normalize(glm::cross(_right, _forward));
}
void Camera::update(float delta_time)
void Camera::update(Scene *scene, float delta_time)
{
// delta_time = std::min(delta_time, 0.01f);
@ -51,8 +51,46 @@ void Camera::update(float delta_time)
_position += _velocity * delta_time;
_acceleration = glm::vec3(0.0f);
this->portalTeleport(scene);
}
void Camera::portalTeleport(Scene *scene)
{
for (const GPUObject &obj : scene->getObjectData())
{
if (obj.type != (int)Object::Type::PORTAL)
continue;
glm::vec3 portal_to_camera = _position - obj.position;
float distance_plane = glm::dot(portal_to_camera, obj.normal);
glm::vec3 point_projected = _position - distance_plane * obj.normal;
glm::mat2 A = glm::mat2(
glm::dot(obj.vertex1, obj.vertex1), glm::dot(obj.vertex1, obj.vertex2),
glm::dot(obj.vertex1, obj.vertex2), glm::dot(obj.vertex2, obj.vertex2)
);
glm::vec2 b = glm::vec2(
glm::dot(point_projected - obj.position, obj.vertex1),
glm::dot(point_projected - obj.position, obj.vertex2)
);
glm::vec2 alphaBeta = glm::inverse(A) * b;
if (alphaBeta.x >= 0.0f && alphaBeta.x <= 1.0f && alphaBeta.y >= 0.0f && alphaBeta.y <= 1.0f)
{
float distance = glm::length(point_projected - _position);
if (distance < 0.1f)
{
GPUObject linked_portal = scene->getObjectData()[obj.radius];
std::cout << glm::to_string(point_projected) << std::endl;
_position = linked_portal.position + (_position - obj.position) - linked_portal.normal * 0.1f;
std::cout << "Teleporting to " << glm::to_string(_position) << std::endl;
break ;
}
}
}
}
void Camera::processMouse(float xoffset, float yoffset, bool constraint_pitch = true)
{

View File

@ -155,7 +155,7 @@ void Window::display()
void Window::pollEvents()
{
this->process_input();
_scene->getCamera()->update(_delta);
_scene->getCamera()->update(_scene, _delta);
glfwPollEvents();
}