Ver código fonte

added more shapes support

ncannasse 7 anos atrás
pai
commit
83dbf88412

+ 90 - 0
libs/bullet/bullet.cpp

@@ -85,8 +85,98 @@ HL_PRIM Shape *HL_NAME(create_sphere_shape)( double radius ) {
 	return alloc_ref(s, finalize_shape);
 }
 
+HL_PRIM Shape *HL_NAME(create_capsule_shape)( int axis, double radius, double length ) {
+	btCollisionShape *s;
+	switch( axis ) {
+	case 0:
+		s = new btCapsuleShapeX(radius, length);
+		break;
+	case 1:
+		s = new btCapsuleShape(radius, length);
+		break;
+	default:
+		s = new btCapsuleShapeZ(radius, length);
+		break;
+	}
+	return alloc_ref(s, finalize_shape);
+}
+
+HL_PRIM Shape *HL_NAME(create_cylinder_shape)( int axis, double sizeX, double sizeY, double sizeZ ) {
+	btCollisionShape *s;
+	switch( axis ) {
+	case 0:
+		s = new btCylinderShapeX(btVector3(sizeX*0.5,sizeY*0.5,sizeZ*0.5));
+		break;
+	case 1:
+		s = new btCylinderShape(btVector3(sizeX*0.5,sizeY*0.5,sizeZ*0.5));
+		break;
+	default:
+		s = new btCylinderShapeZ(btVector3(sizeX*0.5,sizeY*0.5,sizeZ*0.5));
+		break;
+	}
+	return alloc_ref(s, finalize_shape);
+}
+
+HL_PRIM Shape *HL_NAME(create_cone_shape)( int axis, double radius, double height ) {
+	btCollisionShape *s;
+	switch( axis ) {
+	case 0:
+		s = new btConeShapeX(radius, height);
+		break;
+	case 1:
+		s = new btConeShape(radius, height);
+		break;
+	default:
+		s = new btConeShapeZ(radius, height);
+		break;
+	}
+	return alloc_ref(s, finalize_shape);
+}
+
+HL_PRIM Shape *HL_NAME(create_compound_shape)( varray *shapes, double *data ) {
+	btCompoundShape *s = new btCompoundShape(true,shapes->size);
+	int i;
+	btScalar *masses = new btScalar[shapes->size];
+	double *original_data = data;
+	for(i=0;i<shapes->size;i++) {
+		double x = *data++;
+		double y = *data++;
+		double z = *data++;
+		double qx = *data++;
+		double qy = *data++;
+		double qz = *data++;
+		double qw = *data++;
+		masses[i] = *data++;
+		s->addChildShape(btTransform(btQuaternion(qx,qy,qz,qw),btVector3(x,y,z)),hl_aptr(shapes,Shape*)[i]->value);
+	}
+	btTransform principal;
+	btVector3 inertia;
+	s->calculatePrincipalAxisTransform(masses, principal, inertia);
+	delete masses;
+
+	data = original_data;
+	*data++ = principal.getOrigin().x();
+	*data++ = principal.getOrigin().y();
+	*data++ = principal.getOrigin().z();
+	*data++ = principal.getRotation().x();
+	*data++ = principal.getRotation().y();
+	*data++ = principal.getRotation().z();
+	*data++ = principal.getRotation().w();
+
+	btTransform inverse = principal.inverse();
+	for(i=0;i<shapes->size;i++)
+		s->updateChildTransform(i, inverse*s->getChildTransform(i), i == shapes->size - 1);
+
+	// todo : keep references to added shapes ?
+	return alloc_ref((btCollisionShape*)s, finalize_shape);
+}
+
 DEFINE_PRIM(_SHAPE, create_box_shape, _F64 _F64 _F64);
 DEFINE_PRIM(_SHAPE, create_sphere_shape, _F64);
+DEFINE_PRIM(_SHAPE, create_capsule_shape, _I32 _F64 _F64);
+DEFINE_PRIM(_SHAPE, create_cylinder_shape, _I32 _F64 _F64 _F64);
+DEFINE_PRIM(_SHAPE, create_cone_shape, _I32 _F64 _F64);
+DEFINE_PRIM(_SHAPE, create_compound_shape, _ARR _BYTES);
 
 // --- RIGID BODY ----------------------------------------------------------------
 

+ 34 - 0
libs/bullet/bullet.sln

@@ -0,0 +1,34 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 14
+VisualStudioVersion = 14.0.25420.1
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bullet", "bullet.vcxproj", "{6534D221-34DF-404A-AFCD-6DEC9BBC9798}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|x64 = Debug|x64
+		Debug|x86 = Debug|x86
+		Release|x64 = Release|x64
+		Release|x86 = Release|x86
+		ReleaseVS2013|x64 = ReleaseVS2013|x64
+		ReleaseVS2013|x86 = ReleaseVS2013|x86
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Debug|x64.ActiveCfg = Debug|x64
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Debug|x64.Build.0 = Debug|x64
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Debug|x86.ActiveCfg = Debug|Win32
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Debug|x86.Build.0 = Debug|Win32
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Release|x64.ActiveCfg = Release|x64
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Release|x64.Build.0 = Release|x64
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Release|x86.ActiveCfg = Release|Win32
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.Release|x86.Build.0 = Release|Win32
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.ReleaseVS2013|x64.ActiveCfg = ReleaseVS2013|x64
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.ReleaseVS2013|x64.Build.0 = ReleaseVS2013|x64
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.ReleaseVS2013|x86.ActiveCfg = ReleaseVS2013|Win32
+		{6534D221-34DF-404A-AFCD-6DEC9BBC9798}.ReleaseVS2013|x86.Build.0 = ReleaseVS2013|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

+ 47 - 1
libs/bullet/bullet/Shape.hx

@@ -1,7 +1,13 @@
 package bullet;
 
+@:enum abstract Axis(Int) {
+	var X = 0;
+	var Y = 1;
+	var Z = 1;
+}
+
 abstract Shape(hl.Abstract<"bshape">) {
-	
+
 	@:hlNative("bullet","create_box_shape")
 	public static function createBox( sizeX : Float, sizeY : Float, sizeZ : Float ) : Shape {
 		return null;
@@ -12,4 +18,44 @@ abstract Shape(hl.Abstract<"bshape">) {
 		return null;
 	}
 
+	@:hlNative("bullet","create_capsule_shape")
+	public static function createCapsule( axis : Axis, radius : Float, length : Float ) : Shape {
+		return null;
+	}
+
+	@:hlNative("bullet","create_cylinder_shape")
+	public static function createCylinder( axis : Axis, sizeX : Float, sizeY : Float, sizeZ : Float ) : Shape {
+		return null;
+	}
+
+	@:hlNative("bullet","create_cone_shape")
+	public static function createCone( axis : Axis, radius : Float, length : Float ) : Shape {
+		return null;
+	}
+
+	public static function createCompound( shapes : Array<{ shape : Shape, mass : Float, position : h3d.col.Point, rotation : h3d.Quat }> ) {
+		var svalues = new hl.NativeArray<Shape>(shapes.length);
+		var arr = new Array<Float>();
+		var pos = 0;
+		for( i in 0...shapes.length ) {
+			var s = shapes[i];
+			svalues[i] = s.shape;
+			arr[pos++] = s.position.x;
+			arr[pos++] = s.position.y;
+			arr[pos++] = s.position.z;
+			arr[pos++] = s.rotation.x;
+			arr[pos++] = s.rotation.y;
+			arr[pos++] = s.rotation.z;
+			arr[pos++] = s.rotation.w;
+			arr[pos++] = s.mass;
+		}
+		var shape = _createCoumpound(svalues, hl.Bytes.getArray(arr));
+		return { shape : shape, rotation : new h3d.Quat(arr[3],arr[4],arr[5],arr[6]), position : new h3d.col.Point(arr[0], arr[1], arr[2]) };
+	}
+
+	@:hlNative("bullet", "create_compound_shape")
+	static function _createCoumpound( shapes : hl.NativeArray<Shape>, data : hl.Bytes ) : Shape {
+		return null;
+	}
+
 }

+ 29 - 9
libs/bullet/sample/Main.hx

@@ -11,23 +11,42 @@ class Main extends hxd.App {
 		var floor = new bullet.Body(bullet.Shape.createBox(100,100,1),0);
 		world.addRigidBody(floor);
 
-		var floorGfx = new h3d.prim.Cube(100, 100, 1);
-		floorGfx.translate( -50, -50, -0.5);
+		var floorGfx = new h3d.prim.Cube(100, 100, 1, true);
 		var floorMesh = new h3d.scene.Mesh(floorGfx, s3d);
 		floorMesh.material.color.setColor(0x800000);
 		bodies.push({ b : floor, m : floorMesh });
 
-		new h3d.scene.DirLight(new h3d.Vector(1, 2, -4));
+		new h3d.scene.DirLight(new h3d.Vector(1, 2, -4), s3d);
 
-		var sp = bullet.Shape.createSphere(1);
-		var prim = new h3d.prim.Sphere(1,10,10);
-		prim.addNormals();
-		for( i in 0...20 ) {
-			var m = new h3d.scene.Mesh(prim, s3d);
+		var shapes = [bullet.Shape.createSphere(0.5), bullet.Shape.createBox(1,1,1)];
+		var prims = [new h3d.prim.Sphere(0.5), new h3d.prim.Cube(1, 1, 1, true)];
+		prims[1].unindex();
+
+		var comp = bullet.Shape.createCompound([
+			{ shape : shapes[0], mass : 1, position : new h3d.col.Point(0, 0, 0), rotation : new h3d.Quat() },
+			{ shape : shapes[1], mass : 1, position : new h3d.col.Point(0, 0, 1), rotation : new h3d.Quat() }
+		]);
+		shapes.push(comp.shape);
+		var c = new h3d.prim.Cube(1, 1, 2, true);
+		c.unindex();
+		prims.push(c);
+
+		for( p in prims )
+			p.addNormals();
+		for( i in 0...100 ) {
+			var id = Std.random(shapes.length);
+			var m = new h3d.scene.Mesh(prims[id], s3d);
 			m.x = Math.random() * 10;
 			m.y = Math.random() * 10;
 			m.z = 2 + Math.random() * 10;
-			var b = new bullet.Body(sp, 0.5);
+
+			var mt = new h3d.Matrix();
+			mt.identity();
+			mt.colorHue(Math.random() * Math.PI * 2);
+			m.material.color.set(0.5, 0.3, 0);
+			m.material.color.transform(mt);
+
+			var b = new bullet.Body(shapes[id], 0.5);
 			b.setTransform(new h3d.col.Point(m.x, m.y, m.z));
 			world.addRigidBody(b);
 			bodies.push({ b : b, m : m });
@@ -38,6 +57,7 @@ class Main extends hxd.App {
 			b.m.material.shadows = true;
 		}
 
+
 		new h3d.scene.CameraController(80, s3d);
 	}
 

+ 2 - 1
libs/bullet/sample/sample.hxml

@@ -1,6 +1,7 @@
 -lib heaps
 -lib hlbullet
 -lib hldx
--hl sample.hl
+-cp .
 -main Main
+-hl sample.hl
 

+ 4 - 4
libs/bullet/sample/sample.hxproj

@@ -2,7 +2,7 @@
 <project version="2">
   <!-- Output SWF options -->
   <output>
-    <movie outputType="Application" />
+    <movie outputType="CustomBuild" />
     <movie input="" />
     <movie path="sample.hxml" />
     <movie fps="0" />
@@ -10,12 +10,12 @@
     <movie height="0" />
     <movie version="0" />
     <movie minorVersion="0" />
-    <movie platform="hxml" />
+    <movie platform="Custom" />
     <movie background="#000000" />
   </output>
   <!-- Other classes to be compiled into your SWF -->
   <classpaths>
-    <class path="." />
+    <class path="" />
   </classpaths>
   <!-- Build options -->
   <build>
@@ -41,7 +41,7 @@
     <hidden path="obj" />
   </hiddenPaths>
   <!-- Executed before build -->
-  <preBuildCommand>cmd /c haxe $(OutputFile)</preBuildCommand>
+  <preBuildCommand>haxe --connect 6000 sample.hxml</preBuildCommand>
   <!-- Executed after build -->
   <postBuildCommand alwaysRun="False" />
   <!-- Other project options -->