198 lines
5.2 KiB
C++
198 lines
5.2 KiB
C++
/**
|
|
This file is a part of our_dick
|
|
Copyright (C) 2020 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "engine/collision.hpp"
|
|
#include "engine/base_types.hpp"
|
|
|
|
namespace egn{
|
|
|
|
namespace collision{
|
|
aabb::aabb(const egn::aabb& b):
|
|
egn::aabb(b){}
|
|
aabb& aabb::operator=(const egn::aabb& b){
|
|
egn::aabb::operator=(b);
|
|
return *this;
|
|
}
|
|
sphere::sphere(const egn::sphere& b):
|
|
egn::sphere(b){}
|
|
sphere& sphere::operator=(const egn::sphere& b){
|
|
egn::sphere::operator=(b);
|
|
return *this;
|
|
}
|
|
|
|
}
|
|
|
|
/* bool check_collision(const collidable_iface& l, const collidable_iface& r, float epsilon){
|
|
return l.check_collision(r, epsilon);
|
|
}*/
|
|
bool check_collision(const aabb& l, const aabb& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const aabb& l, const sphere& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const aabb& l, const line& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const aabb& l, const plane& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const aabb& l, const point& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const sphere& l, const sphere& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const sphere& l, const aabb& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const sphere& l, const line& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const sphere& l, const plane& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const sphere& l, const point& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const line& l, const line& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const line& l, const sphere& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const line& l, const aabb& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const line& l, const plane& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const line& l, const point& r, float epsilon){
|
|
/*
|
|
C (the point)
|
|
.
|
|
/|
|
|
/ |
|
|
/ |
|
|
/ |
|
|
.-----------------.
|
|
A b B
|
|
(line start) (line end)
|
|
|
|
L(t) = A + t*(B - A)
|
|
ab = (B - A)
|
|
ac = (C - A)
|
|
solve for b
|
|
L(b) = A + b*(ab)
|
|
b = (ac * ab) / (ab * ab)
|
|
u = ab / |ab|
|
|
dist = |C - L(b)|
|
|
*/
|
|
math::vec3<float> ab = (l.point2 - l.point1);
|
|
math::vec3<float> ac = (r - l.point1);
|
|
|
|
//check if point is within the line segment
|
|
//don't need to divide b by (ab * ab) to check if it's less than 0
|
|
float b = ac * ab;
|
|
if(b < (0 - epsilon))
|
|
return false;
|
|
|
|
//don't need to divide b by (ab * ab) to check if it's greater than (ab * ab)
|
|
float ab2 = ab * ab;
|
|
if(b > (ab2 + epsilon))
|
|
return false;
|
|
|
|
//now divide once we know that the point is within this line segment
|
|
b = b / ab2;
|
|
|
|
//check if the point is zero(ish) distance from the line
|
|
math::vec3<float> pb = l.point1 + (ab * b);
|
|
return math::fuzzy_eq(r - pb, math::vec3<float>(math::zero_initialize), epsilon);
|
|
}
|
|
bool check_collision(const plane& l, const plane& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const plane& l, const aabb& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const plane& l, const sphere& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const plane& l, const line& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const plane& l, const point& r, float epsilon){
|
|
(void)l;
|
|
(void)r;
|
|
(void)epsilon;
|
|
return false;
|
|
}
|
|
bool check_collision(const point& l, const point& r, float epsilon){
|
|
return math::fuzzy_eq(l, r, epsilon);
|
|
}
|
|
bool check_collision(const point& l, const aabb& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const point& l, const sphere& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const point& l, const line& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
bool check_collision(const point& l, const plane& r, float epsilon){
|
|
return check_collision(r, l, epsilon);
|
|
}
|
|
|
|
}
|