diff trunk/chipmunkd/chipmunk.d @ 4:7ebbd4d05553

initial commit
author Extrawurst
date Thu, 02 Dec 2010 02:11:26 +0100
parents
children c03a41d47b60
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/chipmunkd/chipmunk.d	Thu Dec 02 02:11:26 2010 +0100
@@ -0,0 +1,174 @@
+
+// written in the D programming language
+
+module chipmunkd.chipmunk;
+//#ifndef CHIPMUNK_HEADER
+//#define CHIPMUNK_HEADER
+//
+//#ifdef __cplusplus
+//extern "C" {
+//#endif
+//
+//void cpMessage(const char *message, const char *condition, const char *file, int line, int isError);
+//#ifdef NDEBUG
+//	#define	cpAssertWarn(condition, message)
+//#else
+//	#define cpAssertWarn(condition, message) if(!(condition)) cpMessage(message, #condition, __FILE__, __LINE__, 0)
+//#endif
+
+debug
+{
+	//TODO:
+	//void cpAssertWarn(bool condition, string message){if(condition) cpMessage(message,condition,__FILE__,__LINE__, 0);}
+	void cpAssertWarn(bool condition, string message){}
+}
+else
+{
+	void cpAssertWarn(bool condition, string message){}
+}
+
+//
+//#ifdef NDEBUG
+//	#define	cpAssert(condition, message)
+//#else
+//	#define cpAssert(condition, message) if(!(condition)) cpMessage(message, #condition, __FILE__, __LINE__, 1)
+//#endif
+//
+import chipmunkd.chipmunk_types_h;
+import core.stdc.stdlib;
+//	
+//#ifndef INFINITY
+//	#ifdef _MSC_VER
+//		union MSVC_EVIL_FLOAT_HACK
+//		{
+//			unsigned __int8 Bytes[4];
+//			float Value;
+//		};
+//		static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
+//		#define INFINITY (INFINITY_HACK.Value)
+//	#endif
+//	
+//	#ifdef __GNUC__
+//		#define INFINITY (__builtin_inf())
+//	#endif
+//	
+//	#ifndef INFINITY
+//		#define INFINITY (1e1000)
+//	#endif
+//#endif
+
+enum INFINITY = cpFloat.infinity;
+
+// Maximum allocated size for various Chipmunk buffer sizes
+enum CP_BUFFER_BYTES = (32*1024);
+
+alias core.stdc.stdlib.malloc cpmalloc;
+alias core.stdc.stdlib.calloc cpcalloc;
+alias core.stdc.stdlib.realloc cprealloc;
+alias core.stdc.stdlib.free cpfree;
+
+public import chipmunkd.cpVect_h,chipmunkd.cpVect;
+public import chipmunkd.cpBB;
+public import chipmunkd.cpArray;
+public import chipmunkd.cpHashSet;
+public import chipmunkd.cpSpaceHash;
+//
+public import chipmunkd.cpBody;
+public import chipmunkd.cpShape;
+public import chipmunkd.cpPolyShape;
+//
+public import chipmunkd.cpArbiter;
+public import chipmunkd.cpCollision;
+//	
+public import chipmunkd.constraints.cpConstraint;
+//
+public import chipmunkd.cpSpace;
+public import chipmunkd.cpSpaceComponent;
+public import chipmunkd.cpSpaceQuery;
+public import chipmunkd.cpSpaceStep;
+
+public import chipmunkd.chipmunk_types_h;
+
+enum cpHashValue CP_HASH_COEF = cast(cpHashValue)(3344921057uL); // ulong to uint ??
+static cpHashValue CP_HASH_PAIR(T)(T A, T B) {return (cast(cpHashValue)(A)*CP_HASH_COEF ^ cast(cpHashValue)(B)*CP_HASH_COEF);}
+
+void
+cpMessage(string message, string condition, string file, int line, int isError)
+{
+	//TODO:
+	//fprintf(stderr, (isError ? "Aborting due to Chipmunk error: %s\n" : "Chipmunk warning: %s\n"), message);
+	//fprintf(stderr, "\tFailed condition: %s\n", condition);
+	//fprintf(stderr, "\tSource:%s:%d\n", file, line);
+	
+	if(isError) abort();
+}
+
+extern const char *cpVersionString;
+void cpInitChipmunk()
+{
+	cpInitCollisionFuncs();
+}
+
+/**
+	Calculate the moment of inertia for a circle.
+	r1 and r2 are the inner and outer diameters. A solid circle has an inner diameter of 0.
+*/
+cpFloat
+cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset)
+{
+	return (1.0f/2.0f)*m*(r1*r1 + r2*r2) + m*cpvdot(offset, offset);
+}
+/**
+	Calculate the moment of inertia for a line segment.
+	Beveling radius is not supported.
+*/
+cpFloat
+cpMomentForSegment(cpFloat m, cpVect a, cpVect b)
+{
+	cpFloat length = cpvlength(cpvsub(b, a));
+	cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f);
+	
+	return m*length*length/12.0f + m*cpvdot(offset, offset);
+}
+
+/**
+	Calculate the moment of inertia for a solid polygon shape.
+*/
+cpFloat
+cpMomentForPoly(cpFloat m, const int numVerts, cpVect *verts, cpVect offset)
+{
+	cpVect *tVerts = cast(cpVect *)cpcalloc(numVerts, cpVect.sizeof);
+	for(int i=0; i<numVerts; i++)
+		tVerts[i] = cpvadd(verts[i], offset);
+	
+	cpFloat sum1 = 0.0f;
+	cpFloat sum2 = 0.0f;
+	for(int i=0; i<numVerts; i++){
+		cpVect v1 = tVerts[i];
+		cpVect v2 = tVerts[(i+1)%numVerts];
+		
+		cpFloat a = cpvcross(v2, v1);
+		cpFloat b = cpvdot(v1, v1) + cpvdot(v1, v2) + cpvdot(v2, v2);
+		
+		sum1 += a*b;
+		sum2 += a;
+	}
+	
+	cpfree(tVerts);
+	return (m*sum1)/(6.0f*sum2);
+}
+
+/**
+	Calculate the moment of inertia for a solid box.
+*/
+cpFloat
+cpMomentForBox(cpFloat m, cpFloat width, cpFloat height)
+{
+	return m*(width*width + height*height)/12.0f;
+}
+
+//#ifdef __cplusplus
+//}
+//#endif
+//
+//#endif