Dawn/src/dawn/physics/2d/Ray2D.cpp
2023-03-21 14:42:54 -07:00

27 lines
725 B
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Ray2D.hpp"
using namespace Dawn;
Ray2D::Ray2D(glm::vec2 pos, glm::vec2 dir) :
position(pos),
direction(dir)
{
}
bool_t Ray2D::intersects(struct Ray2D ray, glm::vec2 *out) {
glm::vec2 j = this->position - ray.position;
float_t f = -ray.direction.x * direction.y + direction.x * ray.direction.y;
float_t s = (-direction.y * j.x + direction.x * j.y) / f;
float_t t = (ray.direction.x * j.y - ray.direction.y * j.x) / f;
if(s >= 0 && s <= 1 && t >= 0 && t <= 1) {
*out = position + (t * direction);
return true;
}
return false;
}