A smart way to manage sleeping objects with Box2D

If you followed Box2D: tutorial for the absolute beginners, then you know you can put some object to “sleep” to improve performance by not simulating inactive bodies.

This is CPU saving but can be used for some game concepts too.

Just think about Super Stacker, where you have happy and afraid bricks.

Super Stacker

If you spend some time on this game, you will find happy bricks are the inactive ones while the afraid ones are still falling/moving.

This can be achieved by playing on the IsSleeping status.

Let’s take the same script introduced in Box2D: tutorial for the absolute beginners and make some changes:

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
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	public class demo extends Sprite {
		var the_world:b2World;
		var time_count:Timer=new Timer(3000);
		public function demo() {
			var environment:b2AABB=new b2AABB  ;
			environment.lowerBound.Set(-100.0,-100.0);
			environment.upperBound.Set(100.0,100.0);
			var gravity:b2Vec2=new b2Vec2(0.0,10.0);
			the_world=new b2World(environment,gravity,true);
			var final_body:b2Body;
			var the_body:b2BodyDef;
			var the_box:b2PolygonDef;
			the_body=new b2BodyDef  ;
			the_body.position.Set(8.5,14);
			the_box=new b2PolygonDef  ;
			the_box.SetAsBox(8.5,0.5);
			the_box.friction=0.3;
			the_box.density=0;
			final_body=the_world.CreateBody(the_body);
			final_body.CreateShape(the_box);
			final_body.SetMassFromShapes();
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
			time_count.addEventListener(TimerEvent.TIMER,on_time);
			time_count.start();
		}
		public function on_time(e:Event) {
			var final_body:b2Body;
			var the_body:b2BodyDef;
			var the_box:b2PolygonDef;
			var box_width=Math.random() + 0.1;
			var box_height=Math.random() + 0.1;
			the_body=new b2BodyDef;
			the_body.position.Set(Math.random() * 10 + 2,0);
			the_body.userData=new cubeface();
			the_body.userData.width=box_width * 60;
			the_body.userData.height=box_height * 60;
			the_box=new b2PolygonDef;
			the_box.SetAsBox(box_width,box_height);
			the_box.friction=0.3;
			the_box.density=1;
			final_body=the_world.CreateBody(the_body);
			final_body.CreateShape(the_box);
			final_body.SetMassFromShapes();
			addChild(the_body.userData);
		}
		public function on_enter_frame(e:Event) {
			the_world.Step(1 / 30,10);
			for (var bb:b2Body=the_world.m_bodyList; bb; bb=bb.m_next) {
				if (bb.m_userData is Sprite) {
					bb.m_userData.x=bb.GetPosition().x * 30;
					bb.m_userData.y=bb.GetPosition().y * 30;
					bb.m_userData.rotation=bb.GetAngle() * 180 / Math.PI;
					if (bb.IsSleeping()) {
						bb.m_userData.gotoAndStop(2);
					}
					else {
						bb.m_userData.gotoAndStop(1);
					}
				}
			}
		}
	}
}

In the on_time function I added some lines to assign a cubeface object to the rectangle I create.

The cubeface object is just a MovieClip with two frames, one with an happy box and one with a sad one.

Line 43: Assigning the cubeface object to the rectangle

Lines 44-45: Defining movieclip width and height

Line 53: Adding the movieclip on the stage

Later, in the on_enter_frame function, you can find the classic Box2d function used to update sprites (or movieclips) position and rotation.

Lines 62-67: Checking if the body is sleeping with IsSleeping. If the body is sleeping, then stop the movieclip on frame 2 (the happy square), otherwise stop it on frame 1 (the afraid square).

This is the result

And this is the source code to download.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.50 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» 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.

6 Responses

  1. Uchiha says:

    Thanks!
    I was looking for this.

  2. dVyper says:

    Brilliant example. I was watching it for ages…

  3. Shival says:

    Cool.
    Seriously cool. Can you make something that can make the faces appear upright ignoring their rotation.

  4. cool. keep in mind though, that you are looping trough ALL bodies, while all you need are the rendered bodies. In this example it’s not an issue, but when only rendering a small subset of your physics world, this is overly expensive.
    try keeping a seperate list for the rendered bodies outside of the engine and looping over them.

    The ‘actor’ implementation referenced in the box2d forums is a good way to do this:
    keep a list of actors, which contain a reference to a displayobject that is rendered and the b2body it’s referencing.

  5. [...] playing with timestep is the key to success. In this script, copied/pasted from A smart way to manage sleeping objects with Box2D I am going to stop the simulation with a mouse click, and let it play again with another mouse [...]

Leave a Reply

flash games company