Falling stones
/**
* Copyright Yukulele ( http://wonderfl.net/user/Yukulele )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/t0w7
*/
package {
import Box2D.Dynamics.Joints.b2JointEdge;
import Box2D.Dynamics.Joints.b2MouseJoint;
import Box2D.Dynamics.Joints.b2MouseJointDef;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import net.hires.debug.Stats;
import flash.utils.getTimer;
[SWF(backgroundColor=0x000000,frameRate=40)]
public class Main extends Sprite {
private var the_world:b2World;
private var timerOld:int;
private var time_count:Timer = new Timer(100);
private var mouseJoint:b2MouseJoint;
private var mousePVec:b2Vec2 = new b2Vec2();
private var myBoundaryListener:BoundaryListener = new BoundaryListener();
public function Main() {
timerOld = getTimer();
var environment:b2AABB = new b2AABB();
environment.lowerBound.Set(0-2, -10);
environment.upperBound.Set(15.5 + 2, 17);
var gravity:b2Vec2=new b2Vec2(0.0,9.81);
the_world=new b2World(environment,gravity,false);
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.m_sprite=debug_sprite;
debug_draw.m_drawScale=30;
debug_draw.m_fillAlpha = 0.5;
debug_draw.m_lineThickness=0;
debug_draw.m_drawFlags =
//b2DebugDraw.e_aabbBit|
//b2DebugDraw.e_centerOfMassBit|
//b2DebugDraw.e_coreShapeBit|
b2DebugDraw.e_jointBit|
//b2DebugDraw.e_obbBit|
//b2DebugDraw.e_pairBit|
b2DebugDraw.e_shapeBit|
0;
the_world.SetDebugDraw(debug_draw);
var final_body:b2Body;
var the_body:b2BodyDef;
var the_box:b2PolygonDef;
the_body = new b2BodyDef();
the_body.position.Set(7.75, 15);
the_body.isBullet=true;
the_box = new b2PolygonDef();
the_box.SetAsBox(7, 0.5);
the_box.friction = .5;
the_box.restitution = .5;
the_box.density=0;
final_body=the_world.CreateBody(the_body);
final_body.CreateShape(the_box);
final_body.SetMassFromShapes();
the_world.SetBoundaryListener(myBoundaryListener);
addEventListener(Event.ENTER_FRAME, on_enter_frame);
time_count.addEventListener(TimerEvent.TIMER, on_time);
time_count.start();
addChild(new Stats);
stage.addEventListener(MouseEvent.MOUSE_DOWN, createMouse);
stage.addEventListener(MouseEvent.MOUSE_UP, destroyMouse);
}
private function on_time(e:Event):void {
creePoly();
}
private function creePoly():void
{
var final_body:b2Body;
var the_body:b2BodyDef;
the_body = new b2BodyDef();
the_body.position.Set(7.75/*Math.random() * 17*/, -5);
the_body.angle = Math.random() * 6;
var the_box:b2PolygonDef;
the_box = new b2PolygonDef();
var nbrangle:int = Math.floor(3+Math.random()*5);
var rayon:Number = Math.random()+.2;
the_box.vertexCount = nbrangle;
for (var i:int = 0; i < nbrangle; i++)
{
the_box.vertices[i]=new b2Vec2((Math.cos(2*Math.PI*i/nbrangle)+(Math.random()-.5)*.3)*rayon, (Math.sin(2*Math.PI*i/nbrangle)+(Math.random()-.5)*.3)*rayon);
}
the_box.isSensor
the_box.restitution = .5;
the_box.friction = .5;
the_box.density = 100;
final_body = the_world.CreateBody(the_body);
final_body.SetAngularVelocity((Math.random() - .5) * 16);
final_body.SetLinearVelocity(new b2Vec2((Math.random() - .5) * 10, 0));
final_body.CreateShape(the_box);
final_body.SetMassFromShapes();
}
private var temps:int;
private function on_enter_frame(e:Event):void {
temps = getTimer() - timerOld;
timerOld = getTimer();
the_world.Step(temps/1000, 10);
myBoundaryListener.lastBodys().forEach(function(elm:b2Body, num:int, vect:Vector.<b2Body>):void {
var joint:b2JointEdge = elm.GetJointList();
while(joint)
{
if (joint.joint == mouseJoint)
mouseJoint = null;
the_world.DestroyJoint(joint.joint);
joint = joint.next;
}
the_world.DestroyBody(elm);
});
/*
var the_body:b2Body = the_world.GetBodyList();
var next:b2Body;
while(the_body)
{
next = the_body.GetNext();
if (the_body.GetPosition().y > 17 || the_body.GetPosition().x < -2||the_body.GetPosition().x > 17.5)
{
var joint:b2JointEdge = the_body.GetJointList();
while(joint)
{
the_world.DestroyJoint(joint.joint);
joint = joint.next;
}
the_world.DestroyBody(the_body);
}
the_body = next;
}
*/
if (mouseJoint) {
var mouseXWorldPhys:Number=mouseX/30;
var mouseYWorldPhys:Number=mouseY/30;
var p2:b2Vec2=new b2Vec2(mouseXWorldPhys,mouseYWorldPhys);
mouseJoint.SetTarget(p2);
}
}
public function createMouse(evt:MouseEvent):void {
var body:b2Body=GetBodyAtMouse();
if (body) {
var mouseJointDef:b2MouseJointDef=new b2MouseJointDef ;
mouseJointDef.body1=the_world.GetGroundBody();
mouseJointDef.body2=body;
mouseJointDef.target.Set(mouseX/30, mouseY/30);
mouseJointDef.maxForce=100000;
mouseJointDef.timeStep=temps/1000;
mouseJoint=the_world.CreateJoint(mouseJointDef) as b2MouseJoint;
}
}
public function destroyMouse(evt:MouseEvent=null):void {
if (mouseJoint) {
the_world.DestroyJoint(mouseJoint);
mouseJoint=null;
}
}
public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body {
var mouseXWorldPhys:Number = (mouseX)/30;
var mouseYWorldPhys:Number = (mouseY)/30;
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
var k_maxCount:int=10;
var shapes:Array = new Array();
var count:int=the_world.Query(aabb,shapes,k_maxCount);
var body:b2Body=null;
for (var i:int = 0; i < count; ++i) {
if (shapes[i].GetBody().IsStatic()==false||includeStatic) {
var tShape:b2Shape=shapes[i] as b2Shape;
var inside:Boolean=tShape.TestPoint(tShape.GetBody().GetXForm(),mousePVec);
if (inside) {
body=tShape.GetBody();
break;
}
}
}
return body;
}
}
}
import Box2D.Dynamics.b2BoundaryListener;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2World;
class BoundaryListener extends b2BoundaryListener
{
private var bodys:Vector.<b2Body>;
public function BoundaryListener()
{
bodys = new Vector.<b2Body>;
}
public override function Violation(body:b2Body):void
{
bodys.push(body);
}
public function lastBodys():Vector.<b2Body>
{
var bodys2:Vector.<b2Body> = bodys;
bodys = new Vector.<b2Body>;
return bodys2;
}
}