Hi,
I'm trying to simulate bubbles using Box2DFlash but am unable to get the perfect simulation. Am in a hurry to finish this project and so do not have the time to go through the manual etc. Can anyone please guide me through this?
The script that I have is
I basically used an example and tried to create over it. Any help on this would be great.
I'm trying to simulate bubbles using Box2DFlash but am unable to get the perfect simulation. Am in a hurry to finish this project and so do not have the time to go through the manual etc. Can anyone please guide me through this?
The script that I have is
Code:
package{
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.display.Stage;
public class bubbles extends Sprite{
public function bubbles(){
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0, -0.4);
// Allow bodies to sleep
var doSleep:Boolean = true;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
// set debug draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
addChild(dbgSprite);
dbgDraw.m_sprite = dbgSprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.0;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = 0xFFFFFFFF;
m_world.SetDebugDraw(dbgDraw);
// Vars used to create bodies
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
var bodyDef1:b2BodyDef;
var bodyDef2:b2BodyDef;
var bodyDef3:b2BodyDef;
var bodyDef4:b2BodyDef;
// Add ground body
bodyDef = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef.position.Set(20, (stage.stageHeight/30)-0.1);
bodyDef.angle = 0;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(stage.stageWidth, stage.stageWidth);
boxDef.friction = 0;
boxDef.density = 0; // static bodies require zero density
bodyDef1 = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef1.position.Set(0, 12);
bodyDef1.angle = 90/180*Math.PI;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(0, 0.1);
boxDef.friction = 0;
boxDef.density = 0; // static bodies require zero density
bodyDef2 = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef2.position.Set((stage.stageWidth/30)-0.1, 12);
bodyDef2.angle = 90/180*Math.PI;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(20, 0.1);
boxDef.friction = 0;
boxDef.density = 0; // static bodies require zero density
bodyDef3 = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef3.position.Set(0, 0.5);
bodyDef3.angle = 0
boxDef = new b2PolygonDef();
boxDef.SetAsBox(20, 0.1);
boxDef.friction = 10;
boxDef.density = 0; // static bodies require zero density
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body = m_world.CreateBody(bodyDef1);
body.CreateShape(boxDef);
//
body = m_world.CreateBody(bodyDef2);
body.CreateShape(boxDef);
body = m_world.CreateBody(bodyDef3);
body.CreateShape(boxDef);
body.SetMassFromShapes();
// Add some objects
for (var i:int = 1; i < 2; i++){
bodyDef = new b2BodyDef();
bodyDef.position.x = ((Math.random() * 100)*stage.stageWidth/100)/30
bodyDef.position.y = ((Math.random() * 100)*stage.stageWidth/100)/30;
var rX:Number = Math.random() + 0.5;
var rY:Number = Math.random() + 0.5;
circleDef = new b2CircleDef();
circleDef.radius = 1.5
circleDef.density = 0.1;
circleDef.friction = 1;
circleDef.restitution = 1
bodyDef.userData = new bubble();
bodyDef.linearDamping = 0.0;
bodyDef.angularDamping = 0.0;
circleDef.filter.groupIndex = -8
body = m_world.CreateBody(bodyDef);
body.m_mass=-1000000000000;
body.CreateShape(circleDef);
body.SetLinearVelocity(new b2Vec2(1, -0.4))
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = Math.floor(bb.GetPosition().x * 80);
bb.m_userData.y = Math.floor(bb.GetPosition().y * 80)
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
}
}
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.display.Stage;
public class bubbles extends Sprite{
public function bubbles(){
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0, -0.4);
// Allow bodies to sleep
var doSleep:Boolean = true;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
// set debug draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
addChild(dbgSprite);
dbgDraw.m_sprite = dbgSprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.0;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = 0xFFFFFFFF;
m_world.SetDebugDraw(dbgDraw);
// Vars used to create bodies
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
var bodyDef1:b2BodyDef;
var bodyDef2:b2BodyDef;
var bodyDef3:b2BodyDef;
var bodyDef4:b2BodyDef;
// Add ground body
bodyDef = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef.position.Set(20, (stage.stageHeight/30)-0.1);
bodyDef.angle = 0;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(stage.stageWidth, stage.stageWidth);
boxDef.friction = 0;
boxDef.density = 0; // static bodies require zero density
bodyDef1 = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef1.position.Set(0, 12);
bodyDef1.angle = 90/180*Math.PI;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(0, 0.1);
boxDef.friction = 0;
boxDef.density = 0; // static bodies require zero density
bodyDef2 = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef2.position.Set((stage.stageWidth/30)-0.1, 12);
bodyDef2.angle = 90/180*Math.PI;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(20, 0.1);
boxDef.friction = 0;
boxDef.density = 0; // static bodies require zero density
bodyDef3 = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef3.position.Set(0, 0.5);
bodyDef3.angle = 0
boxDef = new b2PolygonDef();
boxDef.SetAsBox(20, 0.1);
boxDef.friction = 10;
boxDef.density = 0; // static bodies require zero density
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body = m_world.CreateBody(bodyDef1);
body.CreateShape(boxDef);
//
body = m_world.CreateBody(bodyDef2);
body.CreateShape(boxDef);
body = m_world.CreateBody(bodyDef3);
body.CreateShape(boxDef);
body.SetMassFromShapes();
// Add some objects
for (var i:int = 1; i < 2; i++){
bodyDef = new b2BodyDef();
bodyDef.position.x = ((Math.random() * 100)*stage.stageWidth/100)/30
bodyDef.position.y = ((Math.random() * 100)*stage.stageWidth/100)/30;
var rX:Number = Math.random() + 0.5;
var rY:Number = Math.random() + 0.5;
circleDef = new b2CircleDef();
circleDef.radius = 1.5
circleDef.density = 0.1;
circleDef.friction = 1;
circleDef.restitution = 1
bodyDef.userData = new bubble();
bodyDef.linearDamping = 0.0;
bodyDef.angularDamping = 0.0;
circleDef.filter.groupIndex = -8
body = m_world.CreateBody(bodyDef);
body.m_mass=-1000000000000;
body.CreateShape(circleDef);
body.SetLinearVelocity(new b2Vec2(1, -0.4))
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = Math.floor(bb.GetPosition().x * 80);
bb.m_userData.y = Math.floor(bb.GetPosition().y * 80)
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
}
}
I basically used an example and tried to create over it. Any help on this would be great.