hi all,
i was wondering if anyone can help with a box2d project i have.
i'm sure some would find the problem easy to solve.
and i'm definitely missing something with my world set-up to have this problem.
the problem: my objects don't seem to interact (i.e. collide with each other).
i have a b2CircleDef (dynamic with a density of 1) falling onto a b2PolygonDef (static with a density of 0).
the b2CircleDef falls all the way to the edge of the world (upperBound).
but it never hits (or bounces off) the b2PolygonDef.
and when i set the density of the b2PolygonDef to 1, it falls as well: so its set-up in the world seems correct.
also, with the b2DebugDraw, it seems location and dimensions are correctly set-up.
any help or guidance will be appreciated.
thanks, guys!
so everything seems cool, but its obvious that they are not.
here's the swf: http://sites.google.com/site/kuertee2/f ... ation2.swf
and here's the fla: http://sites.google.com/site/kuertee2/f ... ation2.fla
and here's the code:
i was wondering if anyone can help with a box2d project i have.
i'm sure some would find the problem easy to solve.
and i'm definitely missing something with my world set-up to have this problem.
the problem: my objects don't seem to interact (i.e. collide with each other).
i have a b2CircleDef (dynamic with a density of 1) falling onto a b2PolygonDef (static with a density of 0).
the b2CircleDef falls all the way to the edge of the world (upperBound).
but it never hits (or bounces off) the b2PolygonDef.
and when i set the density of the b2PolygonDef to 1, it falls as well: so its set-up in the world seems correct.
also, with the b2DebugDraw, it seems location and dimensions are correctly set-up.
any help or guidance will be appreciated.
thanks, guys!
so everything seems cool, but its obvious that they are not.
here's the swf: http://sites.google.com/site/kuertee2/f ... ation2.swf
and here's the fla: http://sites.google.com/site/kuertee2/f ... ation2.fla
and here's the code:
Code:
package
{
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.display.Sprite;
//
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* @author Jo' Paul Mamuyac
*/
public class Box2D extends MovieClip
{
public function Box2D()
{
addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage (e: Event): void {
removeEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
init ();
}
private function onRemovedFromStage (e: Event): void {
removeEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
clean ();
}
private function init (): void {
createB2World ();
setUpWalls ();
createS ();
addEventListener (Event.ENTER_FRAME, onThisEnterFrame, false, 0, true);
}
private function clean (): void {
removeEventListener (Event.ENTER_FRAME, onThisEnterFrame);
destroyB2World ();
}
private var _b2TimeStep: Number;
private var _b2Iterations: int;
private function onThisEnterFrame (e: Event): void {
b2WorldUpdate ();
}
//{b2World
private var _b2World: b2World;
private var _scale: Number;
private function createB2World (): void {
if (isNaN (_scale)) {
_scale = 0.1;
}
var worldAABB: b2AABB = new b2AABB ();
worldAABB.lowerBound.Set (stage.stageWidth * -_scale, stage.stageHeight * -_scale);
worldAABB.upperBound.Set (stage.stageWidth * _scale, stage.stageHeight * _scale);
var gravity: b2Vec2 = new b2Vec2 (0, 10);
var doSleep: Boolean = true;
_b2World = new b2World (worldAABB, gravity, doSleep);
// debug draw start
var m_sprite:Sprite;
m_sprite = new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite=m_sprite;
dbgDraw.m_drawScale=1/_scale;
dbgDraw.m_alpha=1;
dbgDraw.m_fillAlpha=0.5;
dbgDraw.m_lineThickness=1;
dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
_b2World.SetDebugDraw(dbgDraw);
}
private function destroyB2World (): void {
}
private function b2WorldUpdate (): void {
if (isNaN (_b2TimeStep)) {
_b2TimeStep = 1 / 60;
}
if (isNaN (_b2Iterations)) {
_b2Iterations = 10;
}
_b2World.Step(_b2TimeStep, _b2Iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = _b2World.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is MovieClip){
bb.m_userData.x = bb.GetPosition().x / _scale;
bb.m_userData.y = bb.GetPosition().y / _scale;
bb.m_userData.rotation = bb.GetAngle() * (180 / Math.PI);
trace (bb.m_userData + ": " + bb.GetPosition().x + ", " + bb.GetPosition().y);
}
}
}
//}
//{walls
private function setUpWalls (): void {
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
var mc: MovieClip;
mc = parent ["mcSpringboard"] as MovieClip;
boxDef = new b2PolygonDef ();
boxDef.SetAsBox (mc.width * _scale / 2, mc.height * _scale / 2);
boxDef.density = 0;
boxDef.friction = 0.5;
boxDef.restitution = 1;
bodyDef = new b2BodyDef ();
bodyDef.position.Set (mc.x * _scale, mc.y * _scale);
bodyDef.userData = mc;
body = _b2World.CreateBody (bodyDef);
body.CreateShape (boxDef);
body.SetMassFromShapes ();
addChild (bodyDef.userData);
}
//}
//{create S
private function createS (): void {
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
var mc: MovieClip;
mc = parent ["mcS"] as MovieClip;
circleDef = new b2CircleDef ();
circleDef.radius = mc.width / 2 * _scale;
circleDef.density = 1;
circleDef.friction = 0.5;
circleDef.restitution = 1;
bodyDef = new b2BodyDef ();
bodyDef.position.Set ((mc.x + mc.width / 2) * _scale, (mc.y + mc.height / 2) * _scale);
bodyDef.userData = mc;
body = _b2World.CreateBody (bodyDef);
body.CreateShape (circleDef);
body.SetMassFromShapes ();
}
//}
}
}
{
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.display.Sprite;
//
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* @author Jo' Paul Mamuyac
*/
public class Box2D extends MovieClip
{
public function Box2D()
{
addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage (e: Event): void {
removeEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
init ();
}
private function onRemovedFromStage (e: Event): void {
removeEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
clean ();
}
private function init (): void {
createB2World ();
setUpWalls ();
createS ();
addEventListener (Event.ENTER_FRAME, onThisEnterFrame, false, 0, true);
}
private function clean (): void {
removeEventListener (Event.ENTER_FRAME, onThisEnterFrame);
destroyB2World ();
}
private var _b2TimeStep: Number;
private var _b2Iterations: int;
private function onThisEnterFrame (e: Event): void {
b2WorldUpdate ();
}
//{b2World
private var _b2World: b2World;
private var _scale: Number;
private function createB2World (): void {
if (isNaN (_scale)) {
_scale = 0.1;
}
var worldAABB: b2AABB = new b2AABB ();
worldAABB.lowerBound.Set (stage.stageWidth * -_scale, stage.stageHeight * -_scale);
worldAABB.upperBound.Set (stage.stageWidth * _scale, stage.stageHeight * _scale);
var gravity: b2Vec2 = new b2Vec2 (0, 10);
var doSleep: Boolean = true;
_b2World = new b2World (worldAABB, gravity, doSleep);
// debug draw start
var m_sprite:Sprite;
m_sprite = new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite=m_sprite;
dbgDraw.m_drawScale=1/_scale;
dbgDraw.m_alpha=1;
dbgDraw.m_fillAlpha=0.5;
dbgDraw.m_lineThickness=1;
dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
_b2World.SetDebugDraw(dbgDraw);
}
private function destroyB2World (): void {
}
private function b2WorldUpdate (): void {
if (isNaN (_b2TimeStep)) {
_b2TimeStep = 1 / 60;
}
if (isNaN (_b2Iterations)) {
_b2Iterations = 10;
}
_b2World.Step(_b2TimeStep, _b2Iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = _b2World.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is MovieClip){
bb.m_userData.x = bb.GetPosition().x / _scale;
bb.m_userData.y = bb.GetPosition().y / _scale;
bb.m_userData.rotation = bb.GetAngle() * (180 / Math.PI);
trace (bb.m_userData + ": " + bb.GetPosition().x + ", " + bb.GetPosition().y);
}
}
}
//}
//{walls
private function setUpWalls (): void {
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
var mc: MovieClip;
mc = parent ["mcSpringboard"] as MovieClip;
boxDef = new b2PolygonDef ();
boxDef.SetAsBox (mc.width * _scale / 2, mc.height * _scale / 2);
boxDef.density = 0;
boxDef.friction = 0.5;
boxDef.restitution = 1;
bodyDef = new b2BodyDef ();
bodyDef.position.Set (mc.x * _scale, mc.y * _scale);
bodyDef.userData = mc;
body = _b2World.CreateBody (bodyDef);
body.CreateShape (boxDef);
body.SetMassFromShapes ();
addChild (bodyDef.userData);
}
//}
//{create S
private function createS (): void {
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
var mc: MovieClip;
mc = parent ["mcS"] as MovieClip;
circleDef = new b2CircleDef ();
circleDef.radius = mc.width / 2 * _scale;
circleDef.density = 1;
circleDef.friction = 0.5;
circleDef.restitution = 1;
bodyDef = new b2BodyDef ();
bodyDef.position.Set ((mc.x + mc.width / 2) * _scale, (mc.y + mc.height / 2) * _scale);
bodyDef.userData = mc;
body = _b2World.CreateBody (bodyDef);
body.CreateShape (circleDef);
body.SetMassFromShapes ();
}
//}
}
}