Basic Filler engine with Box2D – part 3

Here we are with the 3rd part… this time we’ll manage collision detection.

I recommend to read step 1 and 2 before continuing.

You should how to manage collisions with Box2D, and if not go and read Understanding how Box2D manages collisions, but this time we won’t use the collision handler.

Since all collisions we have to manage are among circles, I’ll simply use the Pythagoras theorem.

The only thing that differs from the original concept is when you touch a ball or the boundaries, the ball you are drawing does not disappear but falls down. Just to make something different…

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	public class colorballz extends Sprite {
		public var m_dbgSprite;
		public var m_world:b2World;
		public var m_phys_scale:Number=30;
		public var m_timestep:Number=1.0/30.0;
		public var m_iterations:Number=10.0;
		public var initX:Number=0.0;
		public var initY:Number=0.0;
		public var drawing:Boolean=false;
		public var b:b2Body;
		public var the_ball:ball;
		public var the_enemy:enemy;
		public function colorballz() {
			var gravity:b2Vec2=new b2Vec2(0,9.8);
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-1000,-1000);
			worldAABB.upperBound.Set(1000,1000);
			m_world=new b2World(worldAABB,gravity,true);
			//Add Our Ground
			AddStaticBox(250/m_phys_scale,510/m_phys_scale,250/m_phys_scale,10/m_phys_scale);
			AddStaticBox(250/m_phys_scale,-10/m_phys_scale,250/m_phys_scale,10/m_phys_scale);
			AddStaticBox(-10/m_phys_scale,250/m_phys_scale,10/m_phys_scale,250/m_phys_scale);
			AddStaticBox(510/m_phys_scale,250/m_phys_scale,10/m_phys_scale,250/m_phys_scale);
			//Add enemies
			for (var x:int=1; x<=3; x++) {
				// the first parameter is enemy's radius
				addEnemy(10);
			}
			addEventListener(Event.ENTER_FRAME,Update);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
			stage.addEventListener(MouseEvent.MOUSE_UP,mouseReleased);
		}
		public function mousePressed(e:MouseEvent) {
			initX=e.localX;
			initY=e.localY;
			the_ball = new ball();
			the_ball.x=mouseX;
			the_ball.y=mouseY;
			the_ball.width=1;
			the_ball.height=1;
			addChild(the_ball);
			drawing=true;
		}
		public function mouseMoved(e:MouseEvent) {
			if (drawing) {
				the_ball.x=mouseX;
				the_ball.y=mouseY;
			}
		}
		public function mouseReleased(e:MouseEvent) {
			if (drawing) {
				drawing=false;
				addCircle(mouseX,mouseY,the_ball.width/2,the_ball);
			}
		}
		public function addCircle(_x:Number, _y:Number, _radius:Number,ballclip:ball) {
			var bd:b2BodyDef = new b2BodyDef();
			var cd:b2CircleDef = new b2CircleDef();
			var area:Number=Math.floor(_radius*_radius*Math.PI/25)/100;
			cd.radius=Math.abs(_radius)/m_phys_scale;
			cd.density=2;
			cd.restitution=0.7;
			cd.friction=2;
			bd.position.Set(_x/m_phys_scale, _y/ m_phys_scale);
			bd.userData=ballclip;
			b=m_world.CreateBody(bd);
			b.CreateShape(cd);
			b.SetMassFromShapes();
		}
		public function addEnemy(_radius:Number) {
			var x_speed:Number=Math.random()*10;
			var bd:b2BodyDef = new b2BodyDef();
			var cd:b2CircleDef = new b2CircleDef();
			the_enemy = new(enemy);
			cd.radius=Math.abs(_radius)/m_phys_scale;
			cd.density=20;
			// setting restitution to 1 should grant the enemy won't lose speed when bouncing
			// on STATIC objects
			cd.restitution=1;
			cd.friction=2;
			// randomly placing the enemy
			bd.position.Set((Math.random()*300+50)/m_phys_scale, (Math.random()*300+50)/ m_phys_scale);
			bd.userData=the_enemy;
			bd.userData.name="enemy";
			bd.userData.width=_radius*2;
			bd.userData.height=_radius*2;
			addChild(the_enemy);
			b=m_world.CreateBody(bd);
			b.CreateShape(cd);
			b.SetMassFromShapes();
			// assigning random speed to enemy
			b.m_linearVelocity.x=x_speed;
			b.m_linearVelocity.y=10-x_speed;
		}
		public function AddStaticBox(_x:Number,_y:Number,_halfwidth:Number,_halfheight:Number) {
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(_x,_y);
			var boxDef:b2PolygonDef = new b2PolygonDef();
			boxDef.SetAsBox(_halfwidth,_halfheight);
			boxDef.density=0.0;
			var body:b2Body=m_world.CreateBody(bodyDef);
			body.CreateShape(boxDef);
			body.SetMassFromShapes();
		}
		public function Update(e:Event) {
			var dist_x:Number;
			var dist_y:Number;
			var distance:Number;
			// variable to handle enemy speed
			var enemy_speed:Number;
			// variable to handle the speed offset:it's the ratio between
			// the real enemy speed and the minimum allowed speed
			var speed_offset:Number;
			if (drawing) {
				the_ball.width+=2;
				the_ball.height+=2;
				// checking if the ball is touching boundaries
				if (the_ball.x-the_ball.width/2<0||the_ball.x+the_ball.width/2>500||the_ball.y-the_ball.width/2<0||the_ball.y+the_ball.width/2>500) {
					drawing=false;
					addCircle(mouseX,mouseY,the_ball.width/2,the_ball);
				}
			}
			m_world.Step(m_timestep,m_iterations);
			for (var bb:b2Body=m_world.m_bodyList; bb; bb=bb.m_next) {
				if (bb.m_userData is Sprite) {
					if (bb.m_userData.name=="enemy") {
						// determining enemy speed
						enemy_speed=Math.abs(bb.GetLinearVelocity().x)+Math.abs(bb.GetLinearVelocity().y);
						// if the speed is too slow... (lower than 10)
						if (enemy_speed<10) {
							// calculating the offset
							speed_offset=10/enemy_speed;
							// multiplying the horizontal and vertical components of the speed
							// by the offset
							bb.m_linearVelocity.x=bb.GetLinearVelocity().x*speed_offset;
							bb.m_linearVelocity.y=bb.GetLinearVelocity().y*speed_offset;
						}
					}
					bb.m_userData.x=bb.GetPosition().x*30;
					bb.m_userData.y=bb.GetPosition().y*30;
					// if I am drawing the ball...
					if (drawing) {
						// determining the distance between the current ball and the one I am drawing
						dist_x=the_ball.x-bb.m_userData.x;
						dist_y=the_ball.y-bb.m_userData.y;
						// notice I am not using any square root to make the script lighter
						distance=dist_x*dist_x+dist_y*dist_y;
						// if balls collide...
						if (distance<(the_ball.width/2+bb.m_userData.width/2)*(the_ball.width/2+bb.m_userData.width/2)) {
							// release the drawing ball and set drawing to false, as if I released the mouse
							// here you should lose a life...
							drawing=false;
							addCircle(mouseX,mouseY,the_ball.width/2,the_ball);
						}
					}
				}
			}
		}
	}
}

And this is the result:

You should already know ho to play…

Download the source code and make your own filler game

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 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 4 comments

  1. Uchiha

    on August 5, 2009 at 5:20 pm

    This actually seems to work better than the original engine when it comes to handle the big balls collisions.

  2. Andrew Jacobs

    on August 6, 2009 at 10:10 pm

    Thanks for posting this. It’s great to get insight into using the Box2D engine.

  3. Simplify your Box2D projects with QuickBox2D : Emanuele Feronato

    on August 25, 2009 at 6:12 pm

    [...] know with Box2D you can make interesting games like Mazeroll, SamePhysics or Filler, but even if I published a Box2D tutorial for absolute beginners, everyday I am receiving emails [...]

  4. Santi

    on July 23, 2010 at 5:32 am

    Can u explain how to make the differents colours of balls and not always the sameone??
    And other thing, can u show how to take the % of that ball in the square??

    Thnx

    Santi