Create Box2D levels in a quick with Bison Kick

Some time ago I published a Basic Box2D editor using Flash movieclips.

Now it’s time to show you something way more interesting called Bison Kick by Jacob Schatz who runs the blog jacobschatz.com (bookmark it! It contains a lot of useful information).

It’s an online Box2D editor with live preview.

Besides it’s still under development, with more options to be added and a few bugs to be removed, it’s very fun and simple to use.

I tried to design a level to be used in a game like Totem Destroyer and I managed to export it both in AS3:

//order: x , y , height, width, rotation, isDynamic, shape, friction,density,restitution,ID 
 var map:Array = [
[175,324,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[325,325,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,375,50,500,0,false,'SQUARE' , 1,0.5,0.5,'ground']
,[250,275,50,300,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,200,100,150,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[125,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,125,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[375,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,50,100,50,0,true,'SQUARE' , 1,0.25,0.5,'totem']
];

and in XML:

<level>
  <completeShape>
    <xprop>175</xprop>
    <yprop>324</yprop>
    <height>50</height>
    <width>50</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,0.5,0.5,'normal'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>325</xprop>
    <yprop>325</yprop>
    <height>50</height>
    <width>50</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,0.5,0.5,'normal'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>250</xprop>
    <yprop>375</yprop>
    <height>50</height>
    <width>500</width>
    <rotation>0</rotation>
    <isDynamic>false</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,0.5,0.5,'ground'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>250</xprop>
    <yprop>275</yprop>
    <height>50</height>
    <width>300</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,0.5,0.5,'normal'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>250</xprop>
    <yprop>200</yprop>
    <height>100</height>
    <width>150</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,0.5,0.5,'normal'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>125</xprop>
    <yprop>225</yprop>
    <height>50</height>
    <width>50</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,2,0.5,'heavy'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>250</xprop>
    <yprop>125</yprop>
    <height>50</height>
    <width>50</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,2,0.5,'heavy'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>375</xprop>
    <yprop>225</yprop>
    <height>50</height>
    <width>50</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,2,0.5,'heavy'</physicsandID>
  </completeShape>
  <completeShape>
    <xprop>250</xprop>
    <yprop>50</yprop>
    <height>100</height>
    <width>50</width>
    <rotation>0</rotation>
    <isDynamic>true</isDynamic>
    <shape>SQUARE</shape>
    <physicsandID>1,0.25,0.5,'totem'</physicsandID>
  </completeShape>
</level>

Then, creating a Box2D world from these data was really easy thanks to the snippet of code provided by Jacob:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package 
{	
	import Box2D.Dynamics.Joints.*;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
 
	public class Main extends Sprite {
		public var world:b2World=new b2World(new b2Vec2(0,10.0),true);
		public var world_scale:int = 30;
 
		public static const SQUARE:String = "SQUARE";
		public static const CIRCLE:String = "CIRCLE";
		public var fixtureDef:b2FixtureDef = new b2FixtureDef();
 
		public var specialB2Body:b2Body;
 
		public var motorOn:Boolean;
 
		//order: x , y , height, width, rotation, isDynamic, shape, friction,density,restitution,ID 
 var map:Array = [
[175,324,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[325,325,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,375,50,500,0,false,'SQUARE' , 1,0.5,0.5,'ground']
,[250,275,50,300,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,200,100,150,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[125,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,125,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[375,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,50,100,50,0,true,'SQUARE' , 1,0.25,0.5,'totem']
];		//
		public function Main():void 
		{
			debug_draw();
 
			for (var i:uint = 0; i < map.length; i++)
			{
				var sx:Number = map[i][0];
				var sy:Number = map[i][1];
				var sizerh:Number = map[i][2];
				var sizerw:Number = map[i][3];
				var srot:Number = map[i][4];
				var sIsDynamic:* = map[i][5];
				var sShape:String = map[i][6];
				var sFric:Number = map[i][7];
				var sMass:Number = map[i][8];
				var sRest:Number = map[i][9];
				var boxId:int = map[i][10];
 
				if (sShape == SQUARE)
				{	
					draw_box(sx, sy, sizerw, sizerh, sIsDynamic,srot,sFric,sMass,sRest,boxId);
				}
				else if (sShape == CIRCLE)
				{
					draw_circle(sx, sy, sizerw / 2, sIsDynamic,srot,sFric,sMass,sRest,boxId);
				}
			}
			addEventListener(Event.ENTER_FRAME, update);
		}
 
		public function draw_circle(px:Number, py:Number, r:Number, d:Boolean, rot:Number,
		fric:Number,mass:Number,rest:Number,boxId:int):void {
			var my_body:b2BodyDef= new b2BodyDef();
			my_body.position.Set(px / world_scale, py / world_scale);
			if (d) 
			{
				my_body.type=b2Body.b2_dynamicBody;
			}
			my_body.angle = rot;
			var my_circle:b2CircleShape=new b2CircleShape(r/world_scale);
			var my_fixture:b2FixtureDef = new b2FixtureDef();
			my_fixture.density = mass;
			my_fixture.friction = fric;
			my_fixture.restitution = rest;
			my_fixture.shape=my_circle;
			var world_body:b2Body = world.CreateBody(my_body);
			world_body.CreateFixture(my_fixture);
		}
 
		public function draw_box(px:Number, py:Number, w:Number, h:Number, d:Boolean, rot:Number,
		fric:Number,mass:Number,rest:Number,boxId:int):void {
			var my_body:b2BodyDef = new b2BodyDef();
 
			my_body.position.Set(px / world_scale, py / world_scale);
			my_body.angle = rot;
			if (d) 
			{
				my_body.type=b2Body.b2_dynamicBody;
			}
			var my_box:b2PolygonShape = new b2PolygonShape();
			my_box.SetAsBox(w/2/world_scale, h/2/world_scale);
			var my_fixture:b2FixtureDef = new b2FixtureDef();
			my_fixture.density = mass;
			my_fixture.friction = fric;
			my_fixture.restitution = rest;
			my_fixture.shape=my_box;
			var world_body:b2Body=world.CreateBody(my_body);
			world_body.CreateFixture(my_fixture);
		}
 
		public function debug_draw():void {
			var debug_draw:b2DebugDraw = new b2DebugDraw();
			var debug_sprite:Sprite = new Sprite();
			addChild(debug_sprite);
			debug_draw.SetSprite(debug_sprite);
			debug_draw.SetFillAlpha(0.3);
			debug_draw.SetDrawScale(world_scale);
			debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
			world.SetDebugDraw(debug_draw);
		}
		public function update(e : Event):void {
			world.Step(1 / 30, 10, 10);			
			world.ClearForces();
			world.DrawDebugData();
		}
	}
}

… and this is what you’ll get:

This the same scene of the screenshot you can see at the beginning of this post.

Next time, I’ll show you how to generate the same thing reading an external XML, and how to add more features.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5.00 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 7 comments

  1. Fighterlegend

    on August 17, 2010 at 1:38 am

    Awesome! This will come in user later on :P

  2. Harry

    on September 6, 2010 at 8:05 pm

    Great, this may come in useful.

  3. Box2d Flash Alchemy Port + World Construction Kit - Emanuele Feronato

    on September 7, 2010 at 10:50 am

    [...] Create Box2D levels in a quick with Bison Kick [...]

  4. Nicolas

    on September 24, 2010 at 2:52 pm

    Does anyone know of a C++/C parser for the generated XML ? want to know if I will have to write one myself

  5. bumb0x

    on October 7, 2010 at 1:25 pm

    good stuff, thnx

  6. NateJC

    on November 8, 2010 at 8:48 pm

    Great stuff!

    You said “Next time, I’ll show you how to generate the same thing reading an external XML, and how to add more features”

    I can’t seem to find this post. Did you ever get around to doing the followup?

  7. MahendraSoni

    on February 14, 2011 at 9:16 am

    Hi..good stuff, thanks.

    i tried this, working fine.Now i want to color these boxes and add some background image. could anyone please help me on this.i m new in Box2d..

    thanks in advance:)