Added Box2D

This commit is contained in:
Julian Nießner
2018-05-06 12:43:20 +02:00
parent cb5469bb89
commit d175d433a8
351 changed files with 123545 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,123 @@
/*
* Copyright (c) 2007 Eric Jordan
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef B2_POLYGON_H
#define B2_POLYGON_H
#include "Box2D.h"
#include "b2Triangle.h"
class b2Polygon;
int32 remainder(int32 x, int32 modulus);
int32 TriangulatePolygon(float32* xv, float32* yv, int32 vNum, b2Triangle* results);
bool IsEar(int32 i, float32* xv, float32* yv, int32 xvLength); //Not for external use
int32 PolygonizeTriangles(b2Triangle* triangulated, int32 triangulatedLength, b2Polygon* polys, int32 polysLength);
int32 DecomposeConvex(b2Polygon* p, b2Polygon* results, int32 maxPolys);
void DecomposeConvexAndAddTo(b2Polygon* p, b2Body* bd, b2FixtureDef* prototype);
b2Polygon ConvexHull(b2Vec2* v, int nVert);
b2Polygon ConvexHull(float32* cloudX, float32* cloudY, int32 nVert);
void ReversePolygon(float32* x, float32* y, int n);
b2Polygon TraceEdge(b2Polygon* p); //For use with self-intersecting polygons, finds outline
class b2Polygon {
public:
const static int32 maxVerticesPerPolygon = b2_maxPolygonVertices;
float32* x; //vertex arrays
float32* y;
int32 nVertices;
float32 area;
bool areaIsSet;
b2Polygon(float32* _x, float32* _y, int32 nVert);
b2Polygon(b2Vec2* v, int32 nVert);
b2Polygon();
~b2Polygon();
float32 GetArea();
void MergeParallelEdges(float32 tolerance);
b2Vec2* GetVertexVecs();
b2Polygon(b2Triangle& t);
void Set(const b2Polygon& p);
bool IsConvex();
bool IsCCW();
bool IsUsable(bool printError);
bool IsUsable();
bool IsSimple();
void AddTo(b2FixtureDef& pd);
b2Polygon* Add(b2Triangle& t);
void print(){
printFormatted();
// for (int32 i=0; i<nVertices; ++i){
// printf("i: %d, x:%f, y:%f\n",i,x[i],y[i]);
// }
}
void printFormatted(){
printf("float xv[] = {");
for (int32 i=0; i<nVertices; ++i){
printf("%ff,",x[i]);
}
printf("};\nfloat yv[] = {");
for (int32 i=0; i<nVertices; ++i){
printf("%ff,",y[i]);
}
printf("};\n");
}
b2Polygon(const b2Polygon& p){
nVertices = p.nVertices;
area = p.area;
areaIsSet = p.areaIsSet;
x = new float32[nVertices];
y = new float32[nVertices];
memcpy(x, p.x, nVertices * sizeof(float32));
memcpy(y, p.y, nVertices * sizeof(float32));
}
};
const int32 MAX_CONNECTED = 32;
const float32 COLLAPSE_DIST_SQR = FLT_EPSILON*FLT_EPSILON;//0.1f;//1000*FLT_EPSILON*1000*FLT_EPSILON;
class b2PolyNode{
public:
b2Vec2 position;
b2PolyNode* connected[MAX_CONNECTED];
int32 nConnected;
bool visited;
b2PolyNode(b2Vec2& pos);
b2PolyNode();
void AddConnection(b2PolyNode& toMe);
void RemoveConnection(b2PolyNode& fromMe);
void RemoveConnectionByIndex(int32 index);
bool IsConnectedTo(b2PolyNode& me);
b2PolyNode* GetRightestConnection(b2PolyNode* incoming);
b2PolyNode* GetRightestConnection(b2Vec2& incomingDir);
};
#endif

View File

@@ -0,0 +1,77 @@
/*
* Copyright (c) 2007 Eric Jordan
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "b2Triangle.h"
//Constructor automatically fixes orientation to ccw
b2Triangle::b2Triangle(float32 x1, float32 y1, float32 x2, float32 y2, float32 x3, float32 y3){
x = new float32[3];
y = new float32[3];
float32 dx1 = x2-x1;
float32 dx2 = x3-x1;
float32 dy1 = y2-y1;
float32 dy2 = y3-y1;
float32 cross = dx1*dy2-dx2*dy1;
bool ccw = (cross>0);
if (ccw){
x[0] = x1; x[1] = x2; x[2] = x3;
y[0] = y1; y[1] = y2; y[2] = y3;
} else{
x[0] = x1; x[1] = x3; x[2] = x2;
y[0] = y1; y[1] = y3; y[2] = y2;
}
}
b2Triangle::b2Triangle(){
x = new float32[3];
y = new float32[3];
}
b2Triangle::~b2Triangle(){
delete[] x;
delete[] y;
}
void b2Triangle::Set(const b2Triangle& toMe) {
for (int32 i=0; i<3; ++i) {
x[i] = toMe.x[i];
y[i] = toMe.y[i];
}
}
bool b2Triangle::IsInside(float32 _x, float32 _y){
if (_x < x[0] && _x < x[1] && _x < x[2]) return false;
if (_x > x[0] && _x > x[1] && _x > x[2]) return false;
if (_y < y[0] && _y < y[1] && _y < y[2]) return false;
if (_y > y[0] && _y > y[1] && _y > y[2]) return false;
float32 vx2 = _x-x[0]; float32 vy2 = _y-y[0];
float32 vx1 = x[1]-x[0]; float32 vy1 = y[1]-y[0];
float32 vx0 = x[2]-x[0]; float32 vy0 = y[2]-y[0];
float32 dot00 = vx0*vx0+vy0*vy0;
float32 dot01 = vx0*vx1+vy0*vy1;
float32 dot02 = vx0*vx2+vy0*vy2;
float32 dot11 = vx1*vx1+vy1*vy1;
float32 dot12 = vx1*vx2+vy1*vy2;
float32 invDenom = 1.0f / (dot00*dot11 - dot01*dot01);
float32 u = (dot11*dot02 - dot01*dot12)*invDenom;
float32 v = (dot00*dot12 - dot01*dot02)*invDenom;
return ((u>=0)&&(v>=0)&&(u+v<=1));
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2007 Eric Jordan
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef B2_TRIANGLE_H
#define B2_TRIANGLE_H
#include "b2Math.h"
class b2Triangle{
public:
float* x;
float* y;
b2Triangle();
b2Triangle(float32 x1, float32 y1, float32 x2, float32 y2, float32 x3, float32 y3);
~b2Triangle();
bool IsInside(float32 _x, float32 _y);
void Set(const b2Triangle& toMe);
};
#endif