/**
* @license
*
* The MIT License
* Copyright (c) 2010-2011 Ibon Tolosana, Hyperandroid || http://labs.hyperandroid.com/
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
(function() {
/**
* Startup it all up when the document is ready.
* Change for your favorite frameworks initialization code.
*/
window.addEventListener(
'load',
function() {
CAAT.modules.initialization.init(
800, 500,
'experiment-holder',
[
{id:'ball', url:'http://labs.hyperandroid.com/static/CAAT-Samples/demos/demo-resources/img/naranja.png'},
{id:'rugby', url:'http://labs.hyperandroid.com/static/CAAT-Samples/demos/demo-resources/img/rugby.png'}
],
__box2d
);
},
false);
function __box2d(director) {
CAAT.PMR= director.width/10; // set Point meter ratio to 10 meters.
var scene= new MenuScene().initialize(director);
};
MenuScene= function() {
return this;
};
MenuScene.prototype= {
gravity: 9.8,
world: null, // box2d world
scene: null, // CAAT scene
dx: 0,
dy: -512,
initialize : function(director) {
this.createScene(director);
this.createWorld(director);
this.createButtons(director);
this.createBackground(director);
},
createScene : function(director) {
var me= this;
this.scene = director.createScene();
this.scene.onRenderStart= function() {
me.world.Step(1.0/60, 1,1);
me.world.ClearForces();
};
this.scene.onRenderEnd = function() {
me.world.DrawDebugData();
}
},
createWorld : function(director) {
var gravity = new Box2D.Common.Math.b2Vec2(0, this.gravity);
var doSleep = true;
var BOUNDS_DENSITY= 1.2;
var BOUNDS_FRICTION= .9;
var BOUNDS_RESTITUTION= .66;
var dx0= this.dx;
var dy0= this.dy;
var dx1= director.width;
var dy1= director.height;
var i;
var bounds= [
dx0,dy0,
dx1,dy0,
dx1,dy1,
dx0,dy1,
dx0,dy0
];
this.world = new Box2D.Dynamics.b2World(gravity, doSleep);
this.world.SetContinuousPhysics(true);
CAAT.enableBox2DDebug( true, director, this.world );
for( i=0; i<4; i++ ) {
var actor0= new CAAT.B2DPolygonBody().createBody(
this.world,
{
bodyType: Box2D.Dynamics.b2Body.b2_staticBody,
density: BOUNDS_DENSITY,
friction: BOUNDS_FRICTION,
restitution:BOUNDS_RESTITUTION,
polygonType:CAAT.B2DPolygonBody.Type.EDGE,
bodyDef: [
{x: bounds[i*2 ], y: bounds[i*2+1] },
{x: bounds[i*2+2], y: bounds[i*2+3] }
],
userData: {}
});
}
},
createButtons : function(director) {
var buttons= [
new CAAT.Actor().setFillStyle('orange').setSize(100,150),
new CAAT.Actor().setFillStyle('orange').setSize(100,150),
new CAAT.Actor().setFillStyle('orange').setSize(100,150)
];
CAAT.modules.LayoutUtils.row(
this.scene,
buttons,
{
padding_left: 150,
padding_right: 150,
top: 300
});
for( var i=0; i<buttons.length; i++ ) {
this.scene.addChild( buttons[i].enableEvents(false) );
this.bodyFromActor( buttons[i] );
}
},
bodyFromActor : function(actor) {
// Call helper function
CAAT.B2DPolygonBody.createPolygonBody(
this.world,
{
x: actor.x,
y: actor.y,
bodyType: Box2D.Dynamics.b2Body.b2_staticBody,
density: 1,
restitution: 1,
friction: 1,
image: null,
polygonType: CAAT.B2DPolygonBody.Type.BOX,
bodyDef: [
{ x: actor.x, y: actor.y },
{ x: actor.x + actor.width, y: actor.y + actor.height }
],
bodyDefScale: 1,
bodyDefScaleTolerance: 0,
userData: {}
}
)
},
/**
* Create a background bouncing ball every 1 second.
*
* @param director {CAAT.Director}
*/
createBackground : function(director) {
var me= this;
var bg= new CAAT.ActorContainer().
setBounds(0,0,director.width,director.height);
bg.mouseClick= function(e) {
for (var b = me.world.m_bodyList; b; b = b.m_next) {
//var pos= b.GetPosition();
var pos;
var fix= b.GetFixtureList();
if ( fix ) {
var poly= fix.GetShape();
if ( poly instanceof Box2D.Collision.Shapes.b2PolygonShape ) {
pos= Box2D.Common.Math.b2Math.MulX(b.m_xf, poly.m_centroid);
} else {
pos= b.GetPosition();
}
}
var ix= e.x/CAAT.PMR;
var iy= e.y/CAAT.PMR;
var mag= Math.sqrt( (pos.x - ix)*(pos.x - ix) + (pos.y-iy)*(pos.y-iy) );
var angle= Math.atan2( pos.y-iy, pos.x-ix );
var maxD= 5;
if ( mag>maxD ) {
mag=maxD;
}
mag= maxD-mag;
b.ApplyImpulse(
new Box2D.Common.Math.b2Vec2(
mag*Math.cos(angle),
mag*Math.sin(angle)
),
pos );
}
};
this.scene.addChildAt(bg,0);
this.scene.createTimer(
0,
500,
function(scene_time, task_elapsed_time, timertask_instance) {
var x0 = Math.random() * director.width;
var y0 = Math.random() * director.height;
var x1 = Math.random() * director.width;
var y1 = Math.random() * director.height;
var ball = Math.random() < .5 ?
me.createBall(
director,
me.world,
x0,
y0,
{
radius: 18,
density: 2,
restitution: .3,
friction: .2,
tolerance: .1,
image: 'ball'
}) :
me.createPolygon(
director,
me.world,
x0,
y0,
{
density: 2,
restitution: .7,
friction: .3,
tolerance: .1,
polygonScale: 1,
polygonDef: [
{x:-17.6, y:13.2},
{x:-28.5, y:0.8},
{x:-18.9, y:-13.8},
{x:3.9, y:-16.4},
{x:22.0, y:-12.7},
{x:29.5, y:-1.0},
{x:22.6, y:11.1},
{x:0.5, y:16.5}
],
image: 'rugby'
});
var velocity= {
x: (Math.random()<.5?1:-1) * (5+Math.random()*10),
y: -(5+Math.random()*10)
};
ball.addListener({
actorLifeCycleEvent : function(actor, event, time) {
if (event === 'expired') {
var body = actor.worldBody;
me.world.DestroyBody(body);
}
}
});
ball.setFrameTime( scene_time, 7500 );
ball.worldBody.SetLinearVelocity(
new Box2D.Common.Math.b2Vec2(
velocity.x,
velocity.y) );
ball.worldBody.SetAngularVelocity( Math.PI*Math.random() );
bg.addChild(ball);
timertask_instance.reset( scene_time );
},
null,
null);
},
createBall : function(director,world,x,y,data) {
return new CAAT.B2DCircularBody().enableEvents(false).createBody(
world,
{
x: x,
y: y,
bodyType: Box2D.Dynamics.b2Body.b2_dynamicBody,
radius: data.radius,
density: data.density,
restitution: data.restitution,
friction: data.friction,
bodyDefScaleTolerance: data.tolerance,
image: director.getImage(data.image),
userData: null
});
},
createPolygon : function(director,world,x,y,data) {
return new CAAT.B2DPolygonBody().enableEvents(false).createBody(
world,
{
x: x,
y: y,
bodyType: Box2D.Dynamics.b2Body.b2_dynamicBody,
density: data.density,
restitution: data.restitution,
friction: data.friction,
image: director.getImage(data.image),
polygonType: CAAT.B2DPolygonBody.Type.POLYGON,
bodyDef: data.polygonDef,
bodyDefScale: data.polygonScale,
bodyDefScaleTolerance: data.tolerance,
userData: null
}
);
}
};
})();
<div id='experiment-holder'> </div>