1
This commit is contained in:
parent
ff1eedc0cb
commit
dfc2c950b9
357
a8/collision.cc
357
a8/collision.cc
@ -11,357 +11,6 @@
|
|||||||
|
|
||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
bool IntersectSegmentCircle(a8::Vec2 p0, a8::Vec2 p1, a8::Vec2 pos, float rad)
|
|
||||||
{
|
|
||||||
a8::Vec2 t = p1 - p0;
|
|
||||||
float d = std::max(t.Norm(), 0.0001f);
|
|
||||||
a8::Vec2 n = p0 - pos;
|
|
||||||
float v = n.Dot(t);
|
|
||||||
float z = n.Dot(n) - rad*rad;
|
|
||||||
if (z > 0 && v > 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
float u = v*v - z;
|
|
||||||
if (u < 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
float i = sqrt(u);
|
|
||||||
float x = -v - i;
|
|
||||||
if (x < 0 && (x = -v + i), x <= d) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectSegmentAabb(a8::Vec2 p0, a8::Vec2 p1, a8::Vec2 _min, a8::Vec2 _max)
|
|
||||||
{
|
|
||||||
a8::Vec2 c = (_min + _max) * 0.5f;
|
|
||||||
a8::Vec2 e = _max - c;
|
|
||||||
a8::Vec2 m = (p0 + p1) * 0.5f;
|
|
||||||
a8::Vec2 d = p1 - m;
|
|
||||||
m = m - c;
|
|
||||||
|
|
||||||
float adx = std::abs(d.x);
|
|
||||||
if (std::abs(m.x) > e.x + adx) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
float ady = std::abs(d.y);
|
|
||||||
if (std::abs(m.y) > e.y + ady) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (p0.x > p1.x) {
|
|
||||||
float tmp = p0.x;
|
|
||||||
p0.x = p1.x;
|
|
||||||
p1.x = tmp;
|
|
||||||
}
|
|
||||||
if (p0.y > p1.y) {
|
|
||||||
float tmp = p0.y;
|
|
||||||
p0.y = p1.y;
|
|
||||||
p1.y = tmp;
|
|
||||||
}
|
|
||||||
return IntersectAabbAabb(p0, p1, _min, _max);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectAabbCircle(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_pos, float b_rad)
|
|
||||||
{
|
|
||||||
if (b_pos.x >= a_min.x && b_pos.x <= a_max.x &&
|
|
||||||
b_pos.y >= a_min.y && b_pos.y <= a_max.y) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
a8::Vec2 nearest_point(a8::Clamp(b_pos.x, a_min.x, a_max.x),
|
|
||||||
a8::Clamp(b_pos.y, a_min.y, a_max.y));
|
|
||||||
a8::Vec2 i = b_pos - nearest_point;
|
|
||||||
float n = a8::LengthSqr(i);
|
|
||||||
return n < b_rad*b_rad;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectAabbAabb(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_min, a8::Vec2 b_max)
|
|
||||||
{
|
|
||||||
a8::Vec2 a_v = (a_max - a_min) * 0.5f;
|
|
||||||
a8::Vec2 a_center = a_min + a_v;
|
|
||||||
a8::Vec2 b_v = (b_max - b_min) * 0.5f;
|
|
||||||
a8::Vec2 b_center = b_min + b_v;
|
|
||||||
a8::Vec2 z = b_center - a_center;
|
|
||||||
float u1 = a_v.x + b_v.x - std::abs(z.x);
|
|
||||||
float u2 = a_v.y + b_v.y - std::abs(z.y);
|
|
||||||
return u1 > 0.000001f && u2 > 0.000001;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectAabbObb(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_pos, float rotate)
|
|
||||||
{
|
|
||||||
struct Segment
|
|
||||||
{
|
|
||||||
a8::Vec2 point1;
|
|
||||||
a8::Vec2 point2;
|
|
||||||
a8::Vec2 dir;
|
|
||||||
};
|
|
||||||
std::vector<Segment> a_edges;
|
|
||||||
std::vector<Segment> b_edges;
|
|
||||||
for (int i = 0; i < 3; ++i) {
|
|
||||||
Segment seg;
|
|
||||||
switch (i) {
|
|
||||||
case 0:
|
|
||||||
{
|
|
||||||
seg.point1 = a_min;
|
|
||||||
seg.point2 = a_min + a8::Vec2(a_max.x - a_min.x, 0);
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
{
|
|
||||||
seg.point1 = a_min + a8::Vec2(a_max.x - a_min.x, 0);
|
|
||||||
seg.point2 = a_max;
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
{
|
|
||||||
seg.point1 = a_max;
|
|
||||||
seg.point2 = a_min + a8::Vec2(0, a_max.y - a_min.y);
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
{
|
|
||||||
seg.point1 = a_min + a8::Vec2(0, a_max.y - a_min.y);
|
|
||||||
seg.point2 = a_min;
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
a_edges.push_back(seg);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 3; ++i) {
|
|
||||||
Segment seg;
|
|
||||||
switch (i) {
|
|
||||||
case 0:
|
|
||||||
{
|
|
||||||
seg.point1 = a_min;
|
|
||||||
seg.point2 = a_min + a8::Vec2(a_max.x - a_min.x, 0);
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
{
|
|
||||||
seg.point1 = a_min + a8::Vec2(a_max.x - a_min.x, 0);
|
|
||||||
seg.point2 = a_max;
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
{
|
|
||||||
seg.point1 = a_max;
|
|
||||||
seg.point2 = a_min + a8::Vec2(0, a_max.y - a_min.y);
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
{
|
|
||||||
seg.point1 = a_min + a8::Vec2(0, a_max.y - a_min.y);
|
|
||||||
seg.point2 = a_min;
|
|
||||||
seg.dir = seg.point2 - seg.point1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b_edges.push_back(seg);
|
|
||||||
}
|
|
||||||
#if 0
|
|
||||||
auto GetProjectionWithAxis =
|
|
||||||
[] (std::vector<a8::Vec2> vertices, a8::Vec2 axis) -> a8::Vec2 {
|
|
||||||
axis.Normalize();
|
|
||||||
float _min = vertices[0].Dot(axis);
|
|
||||||
float _max = _min;
|
|
||||||
for (a8::Vec2& v : vertices) {
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectAabbArc(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_pos, float width)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectCircleArc(a8::Vec2 a_pos,
|
|
||||||
float a_rad,
|
|
||||||
a8::Vec2 b_pos,
|
|
||||||
float b_rad,
|
|
||||||
float b_angle,
|
|
||||||
float b_width,
|
|
||||||
float b_height)
|
|
||||||
{
|
|
||||||
float distance = b_pos.Distance(a_pos);
|
|
||||||
float _min = b_rad - b_height / 2 - a_rad;
|
|
||||||
float _max = b_rad + b_height / 2 + a_rad;
|
|
||||||
if (distance < _min) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (distance > _max) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//float angle = (a_pos - b_pos).CalcAngleEx(a8::Vec2::UP);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectCircleCircle(a8::Vec2 a_pos, float a_rad, a8::Vec2 b_pos, float b_rad)
|
|
||||||
{
|
|
||||||
float t = a_rad + b_rad;
|
|
||||||
a8::Vec2 d = b_pos - a_pos;
|
|
||||||
float n = a8::LengthSqr(d);
|
|
||||||
return n < t*t;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectSectorCircle(a8::Vec2 a_pos, float angle, float a_rad, a8::Vec2 b_pos, float b_rad)
|
|
||||||
{
|
|
||||||
{
|
|
||||||
a8::Vec2 d = b_pos - a_pos;
|
|
||||||
float n = a8::LengthSqr(d);
|
|
||||||
if (n < a_rad || n < b_rad) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (n > a_rad + b_rad) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectSectorAabb(a8::Vec2 a_pos, float angle, float a_rad, a8::Vec2 b_min, a8::Vec2 b_max)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CircleContainCircle(glm::vec2 a_pos, float a_rad, glm::vec2 b_pos, float b_rad)
|
|
||||||
{
|
|
||||||
float distance = glm::length(a_pos - b_pos);
|
|
||||||
return distance < a_rad - b_rad;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CalcCircleAabbSafePoint(a8::Vec2 a_pos, float b_rad, a8::Vec2 b_min, a8::Vec2 b_max,
|
|
||||||
a8::Vec2& new_pos)
|
|
||||||
{
|
|
||||||
new_pos = a_pos;
|
|
||||||
bool at_left = std::abs(a_pos.x - b_min.x) < std::abs(a_pos.x - b_max.x);
|
|
||||||
bool at_down = std::abs(a_pos.y - b_min.y) < std::abs(a_pos.y - b_max.y);
|
|
||||||
float x_len = at_left ? std::abs(a_pos.x - b_min.x) : std::abs(a_pos.x - b_max.x);
|
|
||||||
float y_len = at_down ? std::abs(a_pos.y - b_min.y) : std::abs(a_pos.y - b_max.y);
|
|
||||||
if (at_left) {
|
|
||||||
if (x_len < y_len) {
|
|
||||||
//左
|
|
||||||
new_pos.x = b_min.x - b_rad - 1;
|
|
||||||
} else {
|
|
||||||
if (at_down) {
|
|
||||||
new_pos.y = b_min.y - b_rad - 1;
|
|
||||||
} else {
|
|
||||||
new_pos.y = b_max.y + b_rad + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (x_len < y_len) {
|
|
||||||
//右
|
|
||||||
new_pos.x = b_max.x + b_rad + 1;
|
|
||||||
} else {
|
|
||||||
if (at_down) {
|
|
||||||
new_pos.y = b_min.y - b_rad - 1;
|
|
||||||
} else {
|
|
||||||
new_pos.y = b_max.y + b_rad + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CalcCircleCircleSafePoint(a8::Vec2 a_pos, float a_rad, a8::Vec2 b_pos, float b_rad,
|
|
||||||
a8::Vec2& new_pos)
|
|
||||||
{
|
|
||||||
a8::Vec2 dir = a_pos - b_pos;
|
|
||||||
dir.Normalize();
|
|
||||||
new_pos = b_pos + dir*(a_rad + b_rad) + 1;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CalcAabbAabbSafePoint(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_min, a8::Vec2 b_max,
|
|
||||||
a8::Vec2& new_pos)
|
|
||||||
{
|
|
||||||
a8::Vec2 a_pos = a_min + (a_max - a_min)/2.0f;
|
|
||||||
new_pos = a_pos;
|
|
||||||
bool at_left = std::abs(a_pos.x - b_min.x) < std::abs(a_pos.x - b_max.x);
|
|
||||||
bool at_down = std::abs(a_pos.y - b_min.y) < std::abs(a_pos.y - b_max.y);
|
|
||||||
float x_len = at_left ? std::abs(a_pos.x - b_min.x) : std::abs(a_pos.x - b_max.x);
|
|
||||||
float y_len = at_down ? std::abs(a_pos.y - b_min.y) : std::abs(a_pos.y - b_max.y);
|
|
||||||
if (at_left) {
|
|
||||||
if (x_len < y_len) {
|
|
||||||
//左
|
|
||||||
new_pos.x = b_min.x - (a_max.x - a_min.x)/2.0f - 1;
|
|
||||||
} else {
|
|
||||||
if (at_down) {
|
|
||||||
new_pos.y = b_min.y - (a_max.y - a_min.y)/2.0f - 1;
|
|
||||||
} else {
|
|
||||||
new_pos.y = b_max.y + (a_max.y - a_min.y)/2.0f + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (x_len < y_len) {
|
|
||||||
//右
|
|
||||||
new_pos.x = b_max.x + (a_max.x - a_min.x)/2.0f + 1;
|
|
||||||
} else {
|
|
||||||
if (at_down) {
|
|
||||||
new_pos.y = b_min.y - (a_max.y - a_min.y)/2.0f - 1;
|
|
||||||
} else {
|
|
||||||
new_pos.y = b_max.y + (a_max.y - a_min.y)/2.0f + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CalcAabbCircleSafePoint(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_pos, float b_rad,
|
|
||||||
a8::Vec2& new_pos)
|
|
||||||
{
|
|
||||||
a8::Vec2 a_pos = a_min + (a_max - a_min)/2.0f;
|
|
||||||
new_pos = a_pos;
|
|
||||||
bool at_left = std::abs(a_min.x - b_pos.x) < std::abs(a_max.x - b_pos.x);
|
|
||||||
bool at_down = std::abs(a_min.y - b_pos.y) < std::abs(a_max.y - b_pos.y);
|
|
||||||
float x_len = at_left ? std::abs(a_min.x - b_pos.x) : std::abs(a_max.x - b_pos.x);
|
|
||||||
float y_len = at_down ? std::abs(a_min.y - b_pos.y) : std::abs(a_max.y - b_pos.y);
|
|
||||||
if (at_left) {
|
|
||||||
if (x_len < y_len) {
|
|
||||||
//左
|
|
||||||
new_pos.x = b_pos.x - (a_max.x - a_min.x)/2.0f - 1;
|
|
||||||
} else {
|
|
||||||
if (at_down) {
|
|
||||||
new_pos.y = b_pos.y - (a_max.y - a_min.y)/2.0f - 1;
|
|
||||||
} else {
|
|
||||||
new_pos.y = b_pos.y + (a_max.y - a_min.y)/2.0f + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (x_len < y_len) {
|
|
||||||
//右
|
|
||||||
new_pos.x = b_pos.x + (a_max.x - a_min.x)/2.0f + 1;
|
|
||||||
} else {
|
|
||||||
if (at_down) {
|
|
||||||
new_pos.y = b_pos.y - (a_max.y - a_min.y)/2.0f - 1;
|
|
||||||
} else {
|
|
||||||
new_pos.y = b_pos.y + (a_max.y - a_min.y)/2.0f + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntersectCylinderCylinder(const glm::vec3& a_pos, float a_rad, float a_height,
|
bool IntersectCylinderCylinder(const glm::vec3& a_pos, float a_rad, float a_height,
|
||||||
const glm::vec3& b_pos, float b_rad, float b_height)
|
const glm::vec3& b_pos, float b_rad, float b_height)
|
||||||
@ -374,4 +23,10 @@ namespace a8
|
|||||||
return (a_pos.y - b_pos.y) < (a_height + b_height) / 2.0f;
|
return (a_pos.y - b_pos.y) < (a_height + b_height) / 2.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CircleContainCircle(glm::vec2 a_pos, float a_rad, glm::vec2 b_pos, float b_rad)
|
||||||
|
{
|
||||||
|
float distance = glm::length(a_pos - b_pos);
|
||||||
|
return distance < a_rad - b_rad;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <a8/vec2.h>
|
#include <glm/vec2.hpp>
|
||||||
|
#include <glm/vec3.hpp>
|
||||||
|
|
||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
bool IntersectSegmentCircle(a8::Vec2 p0, a8::Vec2 p1, a8::Vec2 pos, float rad);
|
|
||||||
bool IntersectSegmentAabb(a8::Vec2 p0, a8::Vec2 p1, a8::Vec2 _min, a8::Vec2 _max);
|
|
||||||
bool IntersectAabbCircle(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_pos, float b_rad);
|
|
||||||
bool IntersectAabbAabb(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_min, a8::Vec2 b_max);
|
|
||||||
bool IntersectAabbObb(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_pos, float rotate);
|
|
||||||
bool IntersectAabbArc(a8::Vec2 a_min,
|
|
||||||
a8::Vec2 a_max,
|
|
||||||
a8::Vec2 b_pos,
|
|
||||||
float b_rad,
|
|
||||||
float b_width,
|
|
||||||
float b_height);
|
|
||||||
bool IntersectCircleCircle(a8::Vec2 a_pos, float a_rad, a8::Vec2 b_pos, float b_rad);
|
|
||||||
bool IntersectCylinderCylinder(const glm::vec3& a_pos, float a_rad, float a_height,
|
bool IntersectCylinderCylinder(const glm::vec3& a_pos, float a_rad, float a_height,
|
||||||
const glm::vec3& b_pos, float b_rad, float b_height);
|
const glm::vec3& b_pos, float b_rad, float b_height);
|
||||||
|
|
||||||
bool IntersectCircleArc(a8::Vec2 a_pos,
|
|
||||||
float a_rad,
|
|
||||||
a8::Vec2 b_pos,
|
|
||||||
float b_rad,
|
|
||||||
float b_angle,
|
|
||||||
float b_width,
|
|
||||||
float b_height);
|
|
||||||
bool IntersectSectorCircle(a8::Vec2 a_pos, float angle, float a_rad, a8::Vec2 b_pos, float b_rad);
|
|
||||||
bool IntersectSectorAabb(a8::Vec2 a_pos, float angle, float a_rad, a8::Vec2 b_min, a8::Vec2 b_max);
|
|
||||||
bool CircleContainCircle(glm::vec2 a_pos, float a_rad, glm::vec2 b_pos, float b_rad);
|
bool CircleContainCircle(glm::vec2 a_pos, float a_rad, glm::vec2 b_pos, float b_rad);
|
||||||
bool CalcCircleAabbSafePoint(a8::Vec2 a_pos, float a_rad, a8::Vec2 b_min, a8::Vec2 b_max,
|
|
||||||
a8::Vec2& new_pos);
|
|
||||||
bool CalcCircleCircleSafePoint(a8::Vec2 a_pos, float a_rad, a8::Vec2 b_pos, float b_rad,
|
|
||||||
a8::Vec2& new_pos);
|
|
||||||
bool CalcAabbAabbSafePoint(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_min, a8::Vec2 b_max,
|
|
||||||
a8::Vec2& new_pos);
|
|
||||||
bool CalcAabbCircleSafePoint(a8::Vec2 a_min, a8::Vec2 a_max, a8::Vec2 b_pos, float b_rad,
|
|
||||||
a8::Vec2& new_pos);
|
|
||||||
}
|
}
|
||||||
|
127
a8/vec2.cc
127
a8/vec2.cc
@ -1,127 +0,0 @@
|
|||||||
#include <math.h>
|
|
||||||
#include <a8/a8.h>
|
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
#include <glm/vec2.hpp>
|
|
||||||
#include <Eigen/Core>
|
|
||||||
#include <Eigen/Dense>
|
|
||||||
|
|
||||||
#include <a8/vec2.h>
|
|
||||||
|
|
||||||
namespace a8
|
|
||||||
{
|
|
||||||
const Vec2 Vec2::UP = Vec2(0.0f, 1.0f);
|
|
||||||
const Vec2 Vec2::RIGHT = Vec2(1.0f, 0.0f);
|
|
||||||
const Vec2 Vec2::DOWN = Vec2(0.0f, -1.0f);
|
|
||||||
const Vec2 Vec2::LEFT = Vec2(-1.0f, 0.0f);
|
|
||||||
|
|
||||||
void Vec2::Normalize()
|
|
||||||
{
|
|
||||||
glm::vec2 v = glm::normalize(glm::vec2(x, y));
|
|
||||||
if (isnan(v[0])) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
if (isnan(v[1])) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
x = v[0];
|
|
||||||
y = v[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Vec2::operator == (const Vec2& b) const
|
|
||||||
{
|
|
||||||
return std::abs(x - b.x) < 0.01f && std::abs(y - b.y) < 0.01f;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 Vec2::operator + (const Vec2& b) const
|
|
||||||
{
|
|
||||||
glm::vec2 v = glm::vec2(x, y) + glm::vec2(b.x, b.y);
|
|
||||||
return Vec2(v[0], v[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 Vec2::operator - (const Vec2& b) const
|
|
||||||
{
|
|
||||||
glm::vec2 v = glm::vec2(x, y) - glm::vec2(b.x, b.y);
|
|
||||||
return Vec2(v[0], v[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 Vec2::operator * (float scale) const
|
|
||||||
{
|
|
||||||
glm::vec2 v = glm::vec2(x, y) * scale;
|
|
||||||
return Vec2(v[0], v[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 Vec2::operator / (float scale) const
|
|
||||||
{
|
|
||||||
glm::vec2 v = glm::vec2(x, y) / scale;
|
|
||||||
return Vec2(v[0], v[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Vec2::Rotate(float angle)
|
|
||||||
{
|
|
||||||
Eigen::Vector3f v(x, y, 0);
|
|
||||||
v = Eigen::AngleAxisf(angle * 3.1415926f, Eigen::Vector3f::UnitZ()) * v;
|
|
||||||
x = v[0];
|
|
||||||
y = v[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
float Vec2::CalcAngle(const Vec2& b) const
|
|
||||||
{
|
|
||||||
float a1 = acos(Dot(b) / Norm() / b.Norm());
|
|
||||||
bool at_right_side = Vec2::RIGHT.Dot(*this) > 0.0001f;
|
|
||||||
if (at_right_side) {
|
|
||||||
a1 = -a1;
|
|
||||||
}
|
|
||||||
return a1 / 3.1415926f;
|
|
||||||
}
|
|
||||||
|
|
||||||
float Vec2::CalcAngleEx(const Vec2& b) const
|
|
||||||
{
|
|
||||||
float a1 = acos(Dot(b) / Norm() / b.Norm());
|
|
||||||
return a1 / 3.1415926f;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 Vec2::FromAngle(float angle)
|
|
||||||
{
|
|
||||||
Vec2 vec2;
|
|
||||||
float hu = angle * 3.1415926f / 180.0f;
|
|
||||||
vec2.x = cos(hu);
|
|
||||||
vec2.y = sin(hu);
|
|
||||||
vec2.Normalize();
|
|
||||||
return vec2;
|
|
||||||
}
|
|
||||||
|
|
||||||
float Vec2::Distance(const Vec2& b) const
|
|
||||||
{
|
|
||||||
Vec2 v = b - *this;
|
|
||||||
return v.Norm();
|
|
||||||
}
|
|
||||||
|
|
||||||
float Vec2::ManhattanDistance(const Vec2& b)
|
|
||||||
{
|
|
||||||
float distance = std::fabs(x - b.x) + std::fabs(y - b.y);
|
|
||||||
return distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec2 Vec2::Perp()
|
|
||||||
{
|
|
||||||
return Vec2(y, -x);
|
|
||||||
}
|
|
||||||
|
|
||||||
float Vec2::Dot(const Vec2& v) const
|
|
||||||
{
|
|
||||||
return x*v.x + y*v.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
float Vec2::Norm() const
|
|
||||||
{
|
|
||||||
return fabs(sqrt(x*x + y*y));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Vec2::IsZero() const
|
|
||||||
{
|
|
||||||
return fabs(x) < 0.00001f && fabs(y) < 0.00001f;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
35
a8/vec2.h
35
a8/vec2.h
@ -1,35 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
namespace a8
|
|
||||||
{
|
|
||||||
struct Vec2
|
|
||||||
{
|
|
||||||
float x = 0.0f;
|
|
||||||
float y = 0.0f;
|
|
||||||
|
|
||||||
Vec2(float _x = 0.0f, float _y = 0.0f):x(_x), y(_y) {};
|
|
||||||
|
|
||||||
void Normalize();
|
|
||||||
void Rotate(float angle);
|
|
||||||
float CalcAngle(const Vec2& b) const;
|
|
||||||
float CalcAngleEx(const Vec2& b) const;
|
|
||||||
static Vec2 FromAngle(float angle);
|
|
||||||
float Distance(const Vec2& b) const;
|
|
||||||
float ManhattanDistance(const Vec2& b);
|
|
||||||
|
|
||||||
bool operator == (const Vec2& b) const;
|
|
||||||
Vec2 operator + (const Vec2& b) const;
|
|
||||||
Vec2 operator - (const Vec2& b) const;
|
|
||||||
Vec2 operator * (float scale) const;
|
|
||||||
Vec2 operator / (float scale) const;
|
|
||||||
Vec2 Perp();
|
|
||||||
float Dot(const Vec2& v) const;
|
|
||||||
float Norm() const;
|
|
||||||
bool IsZero() const;
|
|
||||||
|
|
||||||
static const Vec2 UP;
|
|
||||||
static const Vec2 DOWN;
|
|
||||||
static const Vec2 LEFT;
|
|
||||||
static const Vec2 RIGHT;
|
|
||||||
};
|
|
||||||
}
|
|
44
a8/vec3.cc
44
a8/vec3.cc
@ -1,44 +0,0 @@
|
|||||||
#include <math.h>
|
|
||||||
#include <a8/a8.h>
|
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
#include <glm/vec3.hpp>
|
|
||||||
|
|
||||||
#include <Eigen/Core>
|
|
||||||
#include <Eigen/Dense>
|
|
||||||
|
|
||||||
|
|
||||||
#include <a8/vec3.h>
|
|
||||||
|
|
||||||
namespace a8
|
|
||||||
{
|
|
||||||
|
|
||||||
void Vec3::RotateX(float angle)
|
|
||||||
{
|
|
||||||
Eigen::Vector3f v(x, y, z);
|
|
||||||
v = Eigen::AngleAxisf(angle * 3.1415926f, Eigen::Vector3f::UnitX()) * v;
|
|
||||||
x = v[0];
|
|
||||||
y = v[1];
|
|
||||||
z = v[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
void Vec3::RotateY(float angle)
|
|
||||||
{
|
|
||||||
Eigen::Vector3f v(x, y, z);
|
|
||||||
v = Eigen::AngleAxisf(angle * 3.1415926f, Eigen::Vector3f::UnitY()) * v;
|
|
||||||
x = v[0];
|
|
||||||
y = v[1];
|
|
||||||
z = v[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
void Vec3::RotateZ(float angle)
|
|
||||||
{
|
|
||||||
Eigen::Vector3f v(x, y, z);
|
|
||||||
v = Eigen::AngleAxisf(angle * 3.1415926f, Eigen::Vector3f::UnitZ()) * v;
|
|
||||||
x = v[0];
|
|
||||||
y = v[1];
|
|
||||||
z = v[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user