GODOT IS OPEN SOURCE

This commit is contained in:
Juan Linietsky
2014-02-09 22:10:30 -03:00
parent 0e49da1687
commit 0b806ee0fc
3138 changed files with 1294441 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
Import('env')
Export('env');
env.add_source_files(env.drivers_sources,"*.cpp")
+170
View File
@@ -0,0 +1,170 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* 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 B2GLUE_H
#define B2GLUE_H
#include "math_2d.h"
#include <limits>
namespace b2ConvexDecomp {
typedef real_t float32;
typedef int32_t int32;
static inline float32 b2Sqrt(float32 val) { return Math::sqrt(val); }
#define b2_maxFloat FLT_MAX
#define b2_epsilon CMP_EPSILON
#define b2_pi 3.14159265359f
#define b2_maxPolygonVertices 16
#define b2Max MAX
#define b2Min MIN
#define b2Clamp CLAMP
#define b2Abs ABS
/// A small length used as a collision and constraint tolerance. Usually it is
/// chosen to be numerically significant, but visually insignificant.
#define b2_linearSlop 0.005f
/// A small angle used as a collision and constraint tolerance. Usually it is
/// chosen to be numerically significant, but visually insignificant.
#define b2_angularSlop (2.0f / 180.0f * b2_pi)
/// A 2D column vector.
struct b2Vec2
{
/// Default constructor does nothing (for performance).
b2Vec2() {}
/// Construct using coordinates.
b2Vec2(float32 x, float32 y) : x(x), y(y) {}
/// Set this vector to all zeros.
void SetZero() { x = 0.0f; y = 0.0f; }
/// Set this vector to some specified coordinates.
void Set(float32 x_, float32 y_) { x = x_; y = y_; }
/// Negate this vector.
b2Vec2 operator -() const { b2Vec2 v; v.Set(-x, -y); return v; }
/// Read from and indexed element.
float32 operator () (int32 i) const
{
return (&x)[i];
}
/// Write to an indexed element.
float32& operator () (int32 i)
{
return (&x)[i];
}
/// Add a vector to this vector.
void operator += (const b2Vec2& v)
{
x += v.x; y += v.y;
}
/// Subtract a vector from this vector.
void operator -= (const b2Vec2& v)
{
x -= v.x; y -= v.y;
}
/// Multiply this vector by a scalar.
void operator *= (float32 a)
{
x *= a; y *= a;
}
/// Get the length of this vector (the norm).
float32 Length() const
{
return b2Sqrt(x * x + y * y);
}
/// Get the length squared. For performance, use this instead of
/// b2Vec2::Length (if possible).
float32 LengthSquared() const
{
return x * x + y * y;
}
bool operator==(const b2Vec2& p_v) const {
return x==p_v.x && y==p_v.y;
}
b2Vec2 operator+(const b2Vec2& p_v) const {
return b2Vec2(x+p_v.x,y+p_v.y);
}
b2Vec2 operator-(const b2Vec2& p_v) const {
return b2Vec2(x-p_v.x,y-p_v.y);
}
b2Vec2 operator*(float32 f) const {
return b2Vec2(f*x,f*y);
}
/// Convert this vector into a unit vector. Returns the length.
float32 Normalize()
{
float32 length = Length();
if (length < b2_epsilon)
{
return 0.0f;
}
float32 invLength = 1.0f / length;
x *= invLength;
y *= invLength;
return length;
}
///// Does this vector contain finite coordinates?
//bool IsValid() const
//{
// return b2IsValid(x) && b2IsValid(y);
//}
float32 x, y;
};
inline b2Vec2 operator*(float32 f,const b2Vec2& p_v) {
return b2Vec2(f*p_v.x,f*p_v.y);
}
/// Perform the dot product on two vectors.
inline float32 b2Dot(const b2Vec2& a, const b2Vec2& b)
{
return a.x * b.x + a.y * b.y;
}
/// Perform the cross product on two vectors. In 2D this produces a scalar.
inline float32 b2Cross(const b2Vec2& a, const b2Vec2& b)
{
return a.x * b.y - a.y * b.x;
}
/// Perform the cross product on a vector and a scalar. In 2D this produces
/// a vector.
inline b2Vec2 b2Cross(const b2Vec2& a, float32 s)
{
return b2Vec2(s * a.y, -s * a.x);
}
}
#endif
File diff suppressed because it is too large Load Diff
+131
View File
@@ -0,0 +1,131 @@
/*
* 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 "b2Triangle.h"
#include "stdio.h"
#include <string.h>
#include <limits>
namespace b2ConvexDecomp {
static bool B2_POLYGON_REPORT_ERRORS = false;
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);
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 = CMP_EPSILON*CMP_EPSILON;//0.1f;//1000*CMP_EPSILON*1000*CMP_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);
};
b2Polygon ConvexHull(b2Vec2* v, int nVert);
b2Polygon ConvexHull(float32* cloudX, float32* cloudY, int32 nVert);
}
#endif
+82
View File
@@ -0,0 +1,82 @@
/*
* 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"
namespace b2ConvexDecomp {
//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));
}
}
+41
View File
@@ -0,0 +1,41 @@
/*
* 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 "b2Glue.h"
namespace b2ConvexDecomp {
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
+134
View File
@@ -0,0 +1,134 @@
#include "b2d_decompose.h"
#include "b2Polygon.h"
namespace b2ConvexDecomp {
void add_to_res(Vector< Vector<Vector2> >& res,const b2Polygon& p_poly) {
Vector<Vector2> arr;
for(int i=0;i<p_poly.nVertices;i++) {
arr.push_back(Vector2(p_poly.x[i],p_poly.y[i]));
}
res.push_back(arr);
}
static Vector< Vector<Vector2> > _b2d_decompose(const Vector<Vector2>& p_polygon) {
Vector< Vector<Vector2> > res;
if (p_polygon.size()<3)
return res;
b2Vec2 *polys = memnew_arr(b2Vec2,p_polygon.size());
for(int i=0;i<p_polygon.size();i++)
polys[i]=b2Vec2(p_polygon[i].x,p_polygon[i].y);
b2Polygon *p = new b2Polygon(polys,p_polygon.size());
b2Polygon* decomposed = new b2Polygon[p->nVertices - 2]; //maximum number of polys
memdelete_arr(polys);
int32 nPolys = DecomposeConvex(p, decomposed, p->nVertices - 2);
//int32 extra = 0;
for (int32 i = 0; i < nPolys; ++i) {
// b2FixtureDef* toAdd = &pdarray[i+extra];
// *toAdd = *prototype;
//Hmm, shouldn't have to do all this...
b2Polygon curr = decomposed[i];
//TODO ewjordan: move this triangle handling to a better place so that
//it happens even if this convenience function is not called.
if (curr.nVertices == 3){
//Check here for near-parallel edges, since we can't
//handle this in merge routine
for (int j=0; j<3; ++j){
int32 lower = (j == 0) ? (curr.nVertices - 1) : (j - 1);
int32 middle = j;
int32 upper = (j == curr.nVertices - 1) ? (0) : (j + 1);
float32 dx0 = curr.x[middle] - curr.x[lower]; float32 dy0 = curr.y[middle] - curr.y[lower];
float32 dx1 = curr.x[upper] - curr.x[middle]; float32 dy1 = curr.y[upper] - curr.y[middle];
float32 norm0 = sqrtf(dx0*dx0+dy0*dy0); float32 norm1 = sqrtf(dx1*dx1+dy1*dy1);
if ( !(norm0 > 0.0f && norm1 > 0.0f) ) {
//Identical points, don't do anything!
goto Skip;
}
dx0 /= norm0; dy0 /= norm0;
dx1 /= norm1; dy1 /= norm1;
float32 cross = dx0 * dy1 - dx1 * dy0;
float32 dot = dx0*dx1 + dy0*dy1;
if (fabs(cross) < b2_angularSlop && dot > 0) {
//Angle too close, split the triangle across from this point.
//This is guaranteed to result in two triangles that satify
//the tolerance (one of the angles is 90 degrees)
float32 dx2 = curr.x[lower] - curr.x[upper]; float32 dy2 = curr.y[lower] - curr.y[upper];
float32 norm2 = sqrtf(dx2*dx2+dy2*dy2);
if (norm2 == 0.0f) {
goto Skip;
}
dx2 /= norm2; dy2 /= norm2;
float32 thisArea = curr.GetArea();
float32 thisHeight = 2.0f * thisArea / norm2;
float32 buffer2 = dx2;
dx2 = dy2; dy2 = -buffer2;
//Make two new polygons
//printf("dx2: %f, dy2: %f, thisHeight: %f, middle: %d\n",dx2,dy2,thisHeight,middle);
float32 newX1[3] = { curr.x[middle]+dx2*thisHeight, curr.x[lower], curr.x[middle] };
float32 newY1[3] = { curr.y[middle]+dy2*thisHeight, curr.y[lower], curr.y[middle] };
float32 newX2[3] = { newX1[0], curr.x[middle], curr.x[upper] };
float32 newY2[3] = { newY1[0], curr.y[middle], curr.y[upper] };
b2Polygon p1(newX1,newY1,3);
b2Polygon p2(newX2,newY2,3);
if (p1.IsUsable()){
add_to_res(res,p1);
//++extra;
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
p1.print();
}
if (p2.IsUsable()){
add_to_res(res,p2);
//p2.AddTo(pdarray[i+extra]);
//bd->CreateFixture(toAdd);
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
p2.print();
}
goto Skip;
}
}
}
if (decomposed[i].IsUsable()){
add_to_res(res,decomposed[i]);
//decomposed[i].AddTo(*toAdd);
//bd->CreateFixture((const b2FixtureDef*)toAdd);
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
decomposed[i].print();
}
Skip:
;
}
// delete[] pdarray;
delete[] decomposed;
delete p;
return res;// pdarray; //needs to be deleted after body is created
}
}
Vector< Vector<Vector2> > b2d_decompose(const Vector<Vector2>& p_polygon) {
return b2ConvexDecomp::_b2d_decompose(p_polygon);
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef B2D_DECOMPOSE_H
#define B2D_DECOMPOSE_H
#include "math_2d.h"
#include "vector.h"
Vector< Vector<Vector2> > b2d_decompose(const Vector<Vector2>& p_polygon);
#endif // B2D_DECOMPOSE_H