Tuesday, 20 August 2013

PhysInjector examples

PhysInjector is a wrapper class in Actionscript 3 that speed up the process of creating Box2D objects. The class significantly reduces the code needed to create Box2D objects. Normally when you create a Box2D object you require a body definition (b2bodyDef) to hold the body information, a shape, a fixture to bind the shape to body definition and a body to add to the world. PhyInjector simply 'injects' a display object on the stage with physic properties. This can reduces the code down to one line.

I have some quick examples below of how to get started with PhyInjector.

Adding PhyInjector to movie clips

import com.reyco1.physinjector.PhysInjector;
import com.reyco1.physinjector.data.PhysicsObject;
import com.reyco1.physinjector.data.PhysicsProperties;
import Box2D.Common.Math.b2Vec2;

addEventListener(Event.ENTER_FRAME, ef);

var physics:PhysInjector = new PhysInjector(stage, new b2Vec2(0, 30), true);


var floor:Floor = new Floor();
floor.y = stage.stageHeight - floor.height;
addChild(floor);

var floorObject:PhysicsObject = physics.injectPhysics(floor, PhysInjector.SQUARE, new PhysicsProperties({isDynamic:false}));   

for(var i:uint = 0; i < 3; i++){
 var square:Square = new Square();
 square.x = (stage.stageWidth  /  2 + square.width)  -  i * (square.width + 10);
 square.y = 0 - Math.random() * 100;
 addChild(square);
 
 var squareProps:PhysicsProperties = new PhysicsProperties();
 squareProps.isDynamic = true;
 squareProps.density = 5;
 squareProps.friction = 0.5;
 squareProps.restitution = 0.3;
 
 var squareObject:PhysicsObject = physics.injectPhysics(square, PhysInjector.SQUARE,squareProps);        
}


         
function ef(e:Event):void
{
 physics.update();
}


Removing PhyInjector movie clips
import com.reyco1.physinjector.PhysInjector;
import com.reyco1.physinjector.data.PhysicsObject;
import com.reyco1.physinjector.data.PhysicsProperties;
import Box2D.Common.Math.b2Vec2;


addEventListener(Event.ENTER_FRAME, ef);
stage.addEventListener(MouseEvent.CLICK, clicked);

var physics:PhysInjector = new PhysInjector(stage, new b2Vec2(0, 30), true);

var floor:Floor = new Floor();
floor.y = stage.stageHeight - floor.height;
addChild(floor);

var floorObject:PhysicsObject = physics.injectPhysics(floor, PhysInjector.SQUARE, new PhysicsProperties({isDynamic:false}));   


for(var i:uint = 0; i < 4; i++){
 var square:Square = new Square();
 square.x = (stage.stageWidth  /  2 + square.width)  -  i * (square.width + 10);
 square.y = 0 - Math.random() * 100;
 square.gotoAndStop( (i & 1) == 0 ? 2 : 1 );
 addChild(square);
 
 var squareProps:PhysicsProperties = new PhysicsProperties();
 squareProps.isDynamic = true;
 squareProps.density = 5;
 squareProps.friction = 0.5;
 squareProps.restitution = 0.3;
 squareProps.name = (i & 1) == 0 ? 'remove' : 'stay';
 
 var squareObject:PhysicsObject = physics.injectPhysics(square, PhysInjector.SQUARE,squareProps);        
}


function clicked(e:MouseEvent):void
{
 var mc = e.target;
  
 if(mc is MovieClip && physics.getPhysicsObject(mc).name == 'remove' && mc != null){
  physics.removePhysics(mc);
  removeChild(mc); 
 }
}

         
function ef(e:Event):void
{
 physics.update();
}


Handling collision with Phyinjector
import com.reyco1.physinjector.PhysInjector;
import com.reyco1.physinjector.data.PhysicsObject;
import com.reyco1.physinjector.data.PhysicsProperties;
import com.reyco1.physinjector.contact.ContactManager;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.Contacts.b2Contact;

var physics:PhysInjector;
var platformArray:Array = [];
var squaresArray:Array = [];
var floor:Floor;
var square:Square;
var squareProps:PhysicsProperties;

function init():void
{
 physics = new PhysInjector(stage, new b2Vec2(0, 10), true);
 
 addEventListener(Event.ENTER_FRAME, ef);
 stage.addEventListener(MouseEvent.CLICK, clicked);
 
 addObjects();
 addPhysics();
}

function addObjects():void
{
 //platform
 for(var i:uint = 0; i < 3; i++){
  var platform:Platform = new Platform();
  platform.x = 25 + i * (platform.width + 100);
  platform.y = stage.stageHeight - 78;
  addChild(platform);
  platformArray.push(platform);
 }
 
 //floor
 floor = new Floor();
 floor.y = stage.stageHeight;
 addChild(floor);
}

function addPhysics():void
{
 //platform physics
 var platformProp:PhysicsProperties = new PhysicsProperties();
 platformProp.isDynamic = false;
 platformProp.name = "platform";
 
 
 for(var i:uint = 0; i < 3; i++){
  var platformObject:PhysicsObject = physics.injectPhysics(platformArray[i], PhysInjector.SQUARE, platformProp); 
 }
 

 //floor physic
 var floorProps:PhysicsProperties = new PhysicsProperties();
 floorProps.isDynamic = false;
 floorProps.name = "floor";
 
 
 var floorObject:PhysicsObject = physics.injectPhysics(floor, PhysInjector.SQUARE, floorProps); 
 
 //square physic 
 squareProps  = new PhysicsProperties();
 squareProps.isDynamic = true;
 squareProps.density = 0.5;
 squareProps.friction = 0.5;
 squareProps.restitution = 0.3;
 squareProps.name = "square";
}


function contactHandler(objectA:PhysicsObject, objectB:PhysicsObject, contact:b2Contact):void
{
 physics.removePhysics(objectA.displayObject);
 removeChild(objectA.displayObject);
}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP