Voici une petite démo de physique dans flash. Une balle rebondissante. Le sprite est soumis à des forces. Le poids, la friction, la poussée de la souris. Voici comment simuler la mécanique des forces en actionscript.

package Displays{ import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.utils.getTimer; import Events.*; import Conf.*; public class Ball extends MovieClip { //______________________________________________________________ SETTINGS private var dragging :Boolean; private var _SPEED :Object; private var _LAST_TIME :Number; private var tmpX :Number; private var tmpY :Number; //FORCES private var _FRICTION :Object; private var _POIDS :Object; private var _POUSSEE :Object; public function Ball() { initForces(); initObjects(); addEventListener(Event.ADDED_TO_STAGE, initListeners); } /** * Get Global force resulting all the forces other the object * * @param forces all forces as Array of Objects * @return obj the resulting force object **/ private function getGlobalForce(... forces:Array):Object { var r:Object={x:0, y:0}; for(var i:int=0; i<forces.length; i++) { r.x+=forces[i].x; r.y+=forces[i].y; } return(r); } /** * Get the Acceleration * * @param timeElapsed time elapsed since last call * @param force the resulting force * @param weight the object weight * @return obj the acceleration object **/ private function getAceleration(timeElapsed:Number, force:Object, weight:Number):Object { var velocity:Object = { x:0, y:0 }; velocity.x = force.x / weight; velocity.y = force.y / weight; velocity.x *= timeElapsed; velocity.y *= timeElapsed; return(velocity); } /*************************** * THE RENDER BALL FUNCTION * *************************/ public function render():void { //update friction _FRICTION = {x:-_SPEED.x*50, y:-_SPEED.y*50}; if(dragging){ _POUSSEE={x:(stage.mouseX-tmpX)*_POIDS.y ,y:(stage.mouseY-tmpY)*_POIDS.y}; }else { _POUSSEE.x *= 0.9; _POUSSEE.y *= 0.9; } var globalForce:Object = getGlobalForce(_POIDS, _FRICTION, _POUSSEE); var acceleration:Object=getAceleration((getTimer()-_LAST_TIME)/1000, globalForce, Constantes.BALL_WEIGHT); _LAST_TIME=getTimer(); _SPEED.x += acceleration.x; _SPEED.y += acceleration.y; collision(); if (dragging) { // if dragging, the ball follows the mouse cursor this.x = stage.mouseX; this.y = stage.mouseY; }else{ // else the ball get the acceleration this.x += Math.round(_SPEED.x); this.y += Math.round(_SPEED.y); } tmpX = stage.mouseX; tmpY = stage.mouseY; } /*************************** * THE COLLISION TESTS * *************************/ private function collision():void { if (this.y +this.height / 2 > stage.stageHeight ) { this.y = stage.stageHeight - this.height / 2 ; _SPEED.y *= -0.8; } if (this.y -this.height / 2 < 0 ) { this.y = this.height / 2 ; _SPEED.y *= -0.8; } if (this.x +this.width / 2 > stage.stageWidth ) { this.x = stage.stageWidth - this.width / 2 ; _SPEED.x *= -0.8; } if (this.x -this.width / 2 < 0 ) { this.x = this.width / 2 ; _SPEED.x *= -0.8; } } /*************************** * INIT OBJECT AND FORCE * *************************/ private function initForces():void { _SPEED = { x:0, y:0 }; _LAST_TIME = getTimer(); _POIDS = { x:0, y:Constantes.GRAVITY * Constantes.BALL_WEIGHT }; _POUSSEE = { x:0, y:0 }; } private function initObjects():void { this.graphics.lineStyle(1, 0); this.graphics.beginFill(0xFFFFFF); this.graphics.drawCircle(0, 0, 20); this.graphics.endFill(); } /*************************** * INIT LISTENERS * *************************/ private function initListeners(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, initListeners); addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); } private function onMouseUp(e:MouseEvent):void { dragging = false; } private function onMouseDown(e:MouseEvent):void { dragging = true; } } }

