Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-08-2012, 02:07 PM   #1
Registered User
Join Date: Nov 2011
Posts: 6
Question Box2D Bounding Boxes

Hey there,

I'm here today with a good old physics engine question. Been using box2D but still not all that friendly with it. Trying to attach bounding boxes to items seems to be causing me trouble. I am simulating an arrows trajectory and physics with box2D, only I now need to place the arrow in the newly created "Box" I have brought into my "World"

The program compiling properly once the lines that attempt to attach the box to the movieClip are taken out. Was wondering if anyone on these forums could help me out. Wondering if this is the best way to do such a thing like placing a bounding box or am I over complicating things. The Arrows bounding box will also collide with others I place through out the level.

//Main Class
package  {
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	import Box;
	import Box2D.Dynamics.*;
	import Box2D.Dynamics.Joints.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.*;
	import Box2D.Common.Math.*;

	public class Main extends EnvironmentObject 
		public static const PIXEL_RATIO:int =30;
		//Box2D World//
		public var world:World;
		public static var archer:Top;
		public static var Bolt:MovieClip;
		//Arrow Definition = A Box//
		public static var arrowDeff:Box;
		private var body:b2Body;
		public var bird:Bird;
		public static var STAGE:Stage;
		public function Main() 
			archer = new Top();
			bird = new Bird();
			Bolt = new Arrow();
			Bolt.x = 60;
			Bolt.y = 550;
			archer.x = 88;
			archer.y = 640;
			bird.x = 860;
			bird.y = 110;

			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		private function onAddedToStage(e:Event)
			STAGE = stage;
			world  = new World();
			//Defining Size and starting area of Box//
			arrowDeff = new Box((STAGE.stageWidth / 2) / PIXEL_RATIO, 0, 30 / PIXEL_RATIO, 30 / PIXEL_RATIO, world.getWorld());
			//Adding the World//			
		//Returns the Box2D body//
	public function getBody():b2Body
			return body;


//World Class
package  {
	import flash.display.*;
	import flash.events.*;
	import Box2D.Dynamics.*;
	import Box2D.Dynamics.Joints.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.*;
	import Box2D.Common.Math.*;
	public class World extends Sprite
		protected var world:b2World;
		protected var body:b2Body;
		public var groundBody:b2Body;
		public var westWallBody:b2Body;
		public var eastWallBody:b2Body;

		public function World() {
			world = new b2World(new b2Vec2(0, 9.8), true);

			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set((Main.STAGE.stageWidth / 2) / Main.PIXEL_RATIO, Main.STAGE.stageHeight / Main.PIXEL_RATIO + 1);

			groundBody = world.CreateBody(bodyDef);

			var groundBox:b2PolygonShape = new b2PolygonShape();
			groundBox.SetAsBox((Main.STAGE.stageWidth / 2) / Main.PIXEL_RATIO, 20 / Main.PIXEL_RATIO);

			var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
			groundFixtureDef.shape = groundBox;
			groundFixtureDef.density = 1;
			groundFixtureDef.friction = 1;
			groundFixtureDef.restitution = 0;
			bodyDef = new b2BodyDef();
			bodyDef.position.Set(0, (Main.STAGE.stageHeight / 2) / Main.PIXEL_RATIO);
			westWallBody = world.CreateBody(bodyDef);
			var westWall:b2PolygonShape = new b2PolygonShape();
			westWall.SetAsBox(0, (Main.STAGE.stageHeight / 2) / Main.PIXEL_RATIO);
			var westFixtureDef:b2FixtureDef = new b2FixtureDef();
			westFixtureDef.shape = westWall;
			westFixtureDef.density = 1;
			westFixtureDef.friction = 0.5;
			westFixtureDef.restitution = 0;
			bodyDef = new b2BodyDef();
			bodyDef.position.Set(Main.STAGE.stageWidth / Main.PIXEL_RATIO, (Main.STAGE.stageHeight / 2) / Main.PIXEL_RATIO);
			eastWallBody = world.CreateBody(bodyDef);
			var eastWall:b2PolygonShape = new b2PolygonShape();
			eastWall.SetAsBox(0, (Main.STAGE.stageHeight / 2) / Main.PIXEL_RATIO);
			var eastFixtureDef:b2FixtureDef = new b2FixtureDef();
			eastFixtureDef.shape = eastWall;
			eastFixtureDef.density = 1;
			eastFixtureDef.friction = 0.5;
			eastFixtureDef.restitution = 0;
			var debugSprite:Sprite = new Sprite();
			var debugDraw:b2DebugDraw = new b2DebugDraw();
			debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit | b2DebugDraw.e_centerOfMassBit | b2DebugDraw.e_aabbBit);
			addEventListener(Event.ENTER_FRAME, update);
		public function getWorld():b2World
			return world;
		public function getGround():b2Body
			return groundBody;
		protected function update(e:Event):void
			var timeStep:Number = 1 / 60;
			var velocityIterations:int = 6;
			var positionIterations:int = 2;
			 //Attempting tpo attach the newly created Box2D "Box" around the MovieClip//
			//Main.Bolt.y = Main.box.y * Main.PIXEL_RATIO;
			//Main.Bolt.x = Main.box.x * Main.PIXEL_RATIO;

			world.Step(timeStep, velocityIterations, positionIterations);

package						//BOX CLASS
	import Box2D.Dynamics.*;
	import Box2D.Collision.Shapes.*;
	import flash.events.*;
	import flash.display.MovieClip;

	public class Box extends MovieClip
		public static var body:b2Body
		public function Box(x:int, y:int, width:Number, height:Number, world:b2World)
			//Setting the bounding box as a Dynamic Body//
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.type = b2Body.b2_dynamicBody;
			bodyDef.position.Set(x, y);
			body = world.CreateBody(bodyDef);

			var dynamicBox:b2PolygonShape = new b2PolygonShape();
			dynamicBox.SetAsBox(width, height);

			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = dynamicBox;
			fixtureDef.density = 1;
			fixtureDef.friction = 0.3;

		public function getBody():b2Body
			return body;

I know it is a bunch of code to take in at once but any help would be sincerely appreciated since my experience with box2D is lacking. The main focus would be the "Update" function in the "World" class, that is where I placed the code to wrap the box around the movieClip but sadly it dosent seem to work.

Thanks for your time.
Ballocaust is offline   Reply With Quote
Old 02-08-2012, 03:05 PM   #2
Super Moderator
ASWC's Avatar
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528

Not sure what you mean by bounding box since box2D does all the collision detection for you, trying to add a bounding box to anything seems rather unneeded. I had to make an arrow for a game and make it collide with other objects in Box2D. The only way to do it is to build an arrow body (with multiple fixture) and update your movieclip accordingly. If you only make a rectangle then you won't get an accurate result. Anyways check also my tutorial here:
Super Duper!
ASWC is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 02:04 PM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.