This commit is contained in:
aozhiwei 2019-03-19 14:38:49 +08:00
parent 28b391af48
commit 285250d83f
2 changed files with 96 additions and 95 deletions

View File

@ -6,101 +6,6 @@
#include "entity.h" #include "entity.h"
#include "collider.h" #include "collider.h"
#if 0
struct Segment
{
Vector2D p0;
Vector2D p1;
Vector2D dir;
Segment(const Vector2D& p0_, const Vector2D& p1_)
{
p0 = p0_;
p1 = p1_;
dir = p1 - p0;
}
};
struct Polygon
{
std::vector<Vector2D> vertices;
std::vector<Segment> edges;
std::tuple<float, float> Project(Vector2D axis)
{
axis = axis.Normalize();
float min_v = vertices[0].Dot(axis);
float max_v = min_v;
for (size_t i = 0; i < vertices.size(); ++i) {
float proj = vertices[i].Dot(axis);
if (proj < min_v) {
min_v = proj;
}
if (proj > max_v) {
max_v = proj;
}
}
return std::make_tuple(min_v, max_v);
}
bool Contains(float n, std::tuple<float, float>& range)
{
float a = std::get<0>(range);
float b = std::get<1>(range);
if (b < a) {
a = b;
b = std::get<0>(range);
}
return n >= a && n <= b;
}
bool Overlap(std::tuple<float, float>& a, std::tuple<float, float>& b)
{
return
Contains(std::get<0>(a), b) ||
Contains(std::get<1>(a), b) ||
Contains(std::get<0>(b), a) ||
Contains(std::get<1>(b), a);
}
bool Sat(Polygon& b)
{
for (size_t i = 0; i < vertices.size(); ++i) {
Vector2D axis = edges[i].dir;
axis = axis.Perp();
auto a_ = Project(axis);
auto b_ = b.Project(axis);
if (!Overlap(a_, b_)) {
return false;
}
}
for (size_t i = 0; i < b.vertices.size(); ++i) {
Vector2D axis = b.edges[i].dir;
axis = axis.Perp();
auto a_ = Project(axis);
auto b_ = b.Project(axis);
if (!Overlap(a_, b_)) {
return false;
}
}
return true;
}
};
static Polygon newPolygon(std::vector<Vector2D>& vertices)
{
Polygon shape;
shape.vertices = vertices;
for (int i = 0; i < vertices.size() - 1; ++i) {
shape.edges.push_back(Segment(vertices[i + 1], vertices[i]));
}
shape.edges.push_back(Segment(vertices[vertices.size() - 1], vertices[0]));
return shape;
}
#endif
static bool IntersectSegmentCircle(Vector2D& p0, Vector2D& p1, Vector2D& pos, float rad) static bool IntersectSegmentCircle(Vector2D& p0, Vector2D& p1, Vector2D& pos, float rad)
{ {
Vector2D t = p1 - p0; Vector2D t = p1 - p0;

View File

@ -70,3 +70,99 @@ float Vector2D::Norm()
{ {
return fabs(sqrt(x*x + y*y)); return fabs(sqrt(x*x + y*y));
} }
#if 0
struct Segment
{
Vector2D p0;
Vector2D p1;
Vector2D dir;
Segment(const Vector2D& p0_, const Vector2D& p1_)
{
p0 = p0_;
p1 = p1_;
dir = p1 - p0;
}
};
struct Polygon
{
std::vector<Vector2D> vertices;
std::vector<Segment> edges;
std::tuple<float, float> Project(Vector2D axis)
{
axis = axis.Normalize();
float min_v = vertices[0].Dot(axis);
float max_v = min_v;
for (size_t i = 0; i < vertices.size(); ++i) {
float proj = vertices[i].Dot(axis);
if (proj < min_v) {
min_v = proj;
}
if (proj > max_v) {
max_v = proj;
}
}
return std::make_tuple(min_v, max_v);
}
bool Contains(float n, std::tuple<float, float>& range)
{
float a = std::get<0>(range);
float b = std::get<1>(range);
if (b < a) {
a = b;
b = std::get<0>(range);
}
return n >= a && n <= b;
}
bool Overlap(std::tuple<float, float>& a, std::tuple<float, float>& b)
{
return
Contains(std::get<0>(a), b) ||
Contains(std::get<1>(a), b) ||
Contains(std::get<0>(b), a) ||
Contains(std::get<1>(b), a);
}
bool Sat(Polygon& b)
{
for (size_t i = 0; i < vertices.size(); ++i) {
Vector2D axis = edges[i].dir;
axis = axis.Perp();
auto a_ = Project(axis);
auto b_ = b.Project(axis);
if (!Overlap(a_, b_)) {
return false;
}
}
for (size_t i = 0; i < b.vertices.size(); ++i) {
Vector2D axis = b.edges[i].dir;
axis = axis.Perp();
auto a_ = Project(axis);
auto b_ = b.Project(axis);
if (!Overlap(a_, b_)) {
return false;
}
}
return true;
}
};
static Polygon newPolygon(std::vector<Vector2D>& vertices)
{
Polygon shape;
shape.vertices = vertices;
for (int i = 0; i < vertices.size() - 1; ++i) {
shape.edges.push_back(Segment(vertices[i + 1], vertices[i]));
}
shape.edges.push_back(Segment(vertices[vertices.size() - 1], vertices[0]));
return shape;
}
#endif