Alternativa3d 8 physics bowling pin
Alternativa3d have just released their own physics library to go hand in hand with their already excellent flash 3d framework. I thought I would give it a quick try to see what its like. The physics engine has its own set of classes that seem to integrate or extend alternativa3D 8. This means that all the scene and camera creation is already done and you can just override the setScene() and then concentrate on handling of the physics and the rest of the code. Its setup is familiar to me and reminds me of the jiglib flash libraries so its quite easy to get up and running with. The examples that come with the physics engine don’t actually show the loading of a 3d model and then applying physics to it but it is very simple task. Just load the .3ds file or .a3d file as you would as per the examples from the alternativa3d 8 library. Once its all loaded you setup a transform, a physical object and a collision area then add it to the scene.
override protected function setScene():void
{
//add fps
addChild(camera.diagram);
//create and add light source
var light:Light3D;
light = new OmniLight(0xFFFFFF, 1, 7000);
light.x = 0;
light.y = 200;
light.z = 1000;
light.intensity = 0.8;
addObject3D(light);
light = new AmbientLight(0xFFFFFF);
light.intensity = 0.2;
addObject3D(light);
//create a loader to load the pin
var loader3DS:URLLoader = new URLLoader();
loader3DS.dataFormat = URLLoaderDataFormat.BINARY;
loader3DS.load(new URLRequest("pin.3ds"));
loader3DS.addEventListener(Event.COMPLETE, on3DSLoad);
}
private function on3DSLoad(e:Event):void
{
var parser:Parser3DS = new Parser3DS();
parser.parse((e.target as URLLoader).data);
var mesh:Mesh;
for each (var object:Object3D in parser.objects) {
if (object.name == "Mesh.007") {
mesh = object as Mesh;
break;
}
}
uploadResources(mesh.getResources(false, Geometry));
// Setup materials
var textures:Vector. = new Vector.();
for (var i:int = 0; i < mesh.numSurfaces; i++) {
var surface:Surface = mesh.getSurface(i);
var material:ParserMaterial = surface.material as ParserMaterial;
if (material != null) {
var diffuse:ExternalTextureResource = material.textures["diffuse"];
if (diffuse != null) {
diffuse.url = "" + diffuse.url;
textures.push(diffuse);
surface.material = new TextureMaterial(diffuse);
}
}
}
// Loading of textures
var texturesLoader:TexturesLoader = new TexturesLoader(stage3d.context3D);
texturesLoader.loadResources(textures);
// setup loaded model in this case bowling pin
var transform:Matrix4 = new Matrix4();
transform.setPositionXYZ(0, 0, 1);
var pin1:PhysicalSimObject = new DynamicObject(transform, new Vector3(0, 0, 2));
pin1.addPhysicsPrimitive(
new PhysicsPrimitive(new CollisionTriangleMesh(MeshUtils.createGeometryMeshFromMesh3d(mesh), CollisionType.DYNAMIC), 1)
);
pin1.addAppearanceComponent(mesh);
addSimObject(pin1);
}
private function uploadResources(resources:Vector.):void {
for each (var resource:Resource in resources) {
resource.upload(stage3d.context3D);
}
}
The boundaries for the collision are taken from the actual mesh this time instead of using a rectangle or other primitive shape. To do this we use the CollisionTriangleMesh and the meshutility to convert the mesh class to the accepted parameter class geometrymesh.
pin1.addPhysicsPrimitive(
new PhysicsPrimitive(new CollisionTriangleMesh(MeshUtils.createGeometryMeshFromMesh3d(mesh), CollisionType.DYNAMIC), 1)
);
While this did work I had some issues with the bowling pins sinking through the floor I created. Its like the origin point for the mesh had changed once it was run through the meshutility. So to get the effect i wanted I switched it around and used a cone shape for collision.
pin1.addPhysicsPrimitive(
new PhysicsPrimitive(new CollisionCone(0.1, 0.3, 3, CollisionType.DYNAMIC), 0.5)
);
You can download the files here or view it on the demo page here.
Comments
Comments are currently closed