AlternativaPhysics events
From AlternativaPlatform Wiki
The ability to manage the process from the outside is important part of any simulation. In AlternativaPhysics it is implemented through the events system.
Due to the events we can track the interaction between bodies, manage the simulation process, add new objects during the simulation process.
Today we discuss the usage of main AlternativaPhysics events. Namely, we will contoll the ball using the keyboard and move flags from one place to another.
Ball will be controlled by W, A, S, D buttons and arrows.
Now we extend the PhysicsSprite class and override its setScene method as in the first lesson.
public class BallDemo extends PhysicsSprite { public function BallDemo() { super(); } override protected function setScene():void { } }
Add lights:
var light:Light3D = new OmniLight(0xAAAAAA, 1, 10000); light.x = 100; light.y = 100; light.z = 1000; light.intensity = 1; addObject3D(light); light = new AmbientLight(0xAAAAAA); light.intensity = 0.3; addObject3D(light);
Add to the scene all objects: ball, plane, flags and platforms we will bring flags to.
//Add a plane. addSimObject(new SimPlane(10, 10)); //Specify the ball transformation and add the ball. var transform:Matrix4 = new Matrix4(); transform.setPositionXYZ(0, 0, 0.8); addSimObject(ball = new SimBall(0.8, transform)); //Add flags. transform = new Matrix4(); transform.setMatrix(4, 4, 0, 0, 0, Math.PI*(1 + 1/6)); addSimObject(new SimFlag(transform, 0xFB0AFF, takeFlag)); transform = new Matrix4(); transform.setMatrix(-4, -4, 0, 0, 0, -Math.PI/6); addSimObject(new SimFlag(transform, 0xFF00, takeFlag)); //Add platforms for the flags. transform = new Matrix4(); transform.setPositionXYZ(-4, 4, 0); addSimObject(new SimTargetTrigger(transform, placeFlag)); transform.setPositionXYZ(4, -4, 0); addSimObject(new SimTargetTrigger(transform, placeFlag));
Set the camera controller:
//Disable moving of the camera. cameraController.disable(); //Set the camera position. cameraController.setObjectPosXYZ(0, -10, 5); //Set the direction of view. cameraController.lookAtXYZ(0, 0, 0);
Add event listeners:
//Add the keyboard event handlers. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); //Add the handler for OnBeforeUpdate event. physicsScene.addEventListener(PhysicsEvent.OnBeforeUpdate, moveBall);
And specify those events handlers:
//onKeyDown handler. private function onKeyDown(event:KeyboardEvent):void { switch (event.keyCode) { case Keyboard.LEFT: case Keyboard.A: left = true; break; case Keyboard.RIGHT: case Keyboard.D: right = true; break; case Keyboard.UP: case Keyboard.W: up = true; break; case Keyboard.DOWN: case Keyboard.S: down = true; break; } } //onKeyUp handler. private function onKeyUp(event:KeyboardEvent):void { switch (event.keyCode) { case Keyboard.LEFT: case Keyboard.A: left = false; break; case Keyboard.RIGHT: case Keyboard.D: right = false; break; case Keyboard.UP: case Keyboard.W: up = false; break; case Keyboard.DOWN: case Keyboard.S: down = false; break; } } //Handler of ball's movement. private function moveBall(event:PhysicsEvent):void { //Calculates the moving direction vector on pressed buttons. dir.reset(); if (left) { dir.x--; } if (right) { dir.x++; } if (up) { dir.y++; } if (down) { dir.y--; } if (dir.length() > 0.5) { //The ball rotates only in the direction of motion. var rotation:Vector3 = ball.body.state.rotation; dir.normalize(); t.cross2(dir, rotation); var dot:Number = t.z; if (dot < 0) { dot = 0; } t.cross2(Vector3.Z_AXIS, dir); rotation.copy(t).scale(dot); dir.scale(20); //Add the force to ball for the next simulation step. ball.body.addForce(dir); } else { //Stop the ball. ball.body.state.rotation.reset(); } } //Handler of taking the flag. private function takeFlag(event:ContactEvent):void { //If you are carrying one flag and try to get the another, then the first one goes back. if (flag != null) { flag.visible = true; } //Get information about taken flag from the user data. flag = (event.userData as SimFlag); //Set color of taken flag to ball. ball.setColor(context, flag.flagColor); //Flag disappears. flag.visible = false; } //Handler of bringing a flag to a platform private function placeFlag(event:ContactEvent):void { //If flag is not get, then nothing else to do if (flag == null) { return; } //Get the flag place from the user data. var target:SimTargetTrigger = event.userData as SimTargetTrigger; //Set the color of the ball to the place target.setColor(context, flag.flagColor); //Set the ball color. ball.setColor(context, 0xFFFF40); //Now the ball does not move the flag. flag = null; }
At the end, if you run this example you'll see a scene with the ball. You can move the ball on the platform and move flags.
Sources