Introduction to AlternativaPhysics
From AlternativaPlatform Wiki
AlternativaPhysics is engine that allows to simulate the physical interaction of objects in Flash.
At this tutorial we will implement a single interaction - drop the body on a static surface.
We will use the CollisionBox primitive as the body and the CollisionRect primitive as the surface.
AlternativaPhysics world consists of objects, that are not displayed themselves on the screen. So, you need to implement the visualization of these objects. It means that you must specify objects appearance and add the lights. PhysicsSprite implements physics simulation and renders physics objects. Declare class PhysicsTest that extends PhysicsSprite, and override setScene() method:
public class PhysicsTest extends PhysicsSprite { public function PhysicsTest():void { super(); } override protected function setScene():void { }
Create lights and add them to the scene:
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);
Next, create a plane:
//Create a static object. var plane:PhysicalSimObject = new StaticObject(); //Add a physical primitive with standard material and geometry of rectangle. It is purely physical object. You can't see it without visualization. plane.addPhysicsPrimitive( new PhysicsPrimitive(new CollisionRect(10, 10, CollisionType.STATIC)) ); //Add a graphical representation of the physical object. plane.addAppearanceComponent( MeshUtils.createRectangle(10, 10, new VertexLightMaterial(0x777777)) ); //Add the object to the scene. addSimObject(plane);
Create a cube:
//Object transformation. Translate it along the Z axis at 1. var transform:Matrix4 = new Matrix4(); transform.setPositionXYZ(0, 0, 1); //Create dynamic object with this transformation and velocity vector equals to (0,0,10). var box:PhysicalSimObject = new DynamicObject(transform, new Vector3(0, 0, 10)); //Add a physical primitive (weight: 10, standard material, geometry of cube). box.addPhysicsPrimitive( new PhysicsPrimitive(new CollisionBox(new Vector3(1, 1, 1), CollisionType.DYNAMIC), 10) ); //Add a graphical representation of the cube. box.addAppearanceComponent( MeshUtils.createBox(new Vector3(2, 2, 2), new VertexLightMaterial(0x77AA77)) ); //Add the object to the scene. addSimObject(box);
Finally:
[SWF (backgroundColor="0x000000", width="1024", height="768", frameRate="60")] public class PhysicsTest extends PhysicsSprite { public function PhysicsTest():void { super(); } override protected function setScene():void { addChild(camera.diagram); 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); var plane:PhysicalSimObject = new StaticObject(); plane.addPhysicsPrimitive( new PhysicsPrimitive(new CollisionRect(10, 10, CollisionType.STATIC)) ); plane.addAppearanceComponent( MeshUtils.createRectangle(10, 10, new VertexLightMaterial(0x777777)) ); addSimObject(plane); var transform:Matrix4 = new Matrix4(); transform.setPositionXYZ(0, 0, 1); var box:PhysicalSimObject = new DynamicObject(transform, new Vector3(0, 0, 10)); box.addPhysicsPrimitive( new PhysicsPrimitive(new CollisionBox(new Vector3(1, 1, 1), CollisionType.DYNAMIC), 10) ); box.addAppearanceComponent( MeshUtils.createBox(new Vector3(2, 2, 2), new VertexLightMaterial(0x77AA77)) ); addSimObject(box); }