<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Emanuele Feronato &#187; Box2D</title>
	<atom:link href="http://www.emanueleferonato.com/category/box2d/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Thu, 11 Mar 2010 00:11:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Understanding Box2D&#8217;s one-way platforms, aka CLOUDS</title>
		<link>http://www.emanueleferonato.com/2010/03/02/understanding-box2ds-one-way-platforms-aka-clouds/</link>
		<comments>http://www.emanueleferonato.com/2010/03/02/understanding-box2ds-one-way-platforms-aka-clouds/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:24:20 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2448</guid>
		<description><![CDATA[One of the new features introduced with Box2D 2.1a is the improved contact listener class which comes in hand when we want to create one-way platforms, or &#8220;clouds&#8221;.
This can be made thanks to a function called before the contact is processed&#8230; something like &#8220;hey, two bodies are about to collide, what should I do?&#8221;&#8230; so [...]]]></description>
			<content:encoded><![CDATA[<p>One of the new features introduced with <a href="http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/">Box2D 2.1a</a> is the improved contact listener class which comes in hand when we want to create one-way platforms, or &#8220;clouds&#8221;.</p>
<p>This can be made thanks to a function called <strong>before</strong> the contact is processed&#8230; something like &#8220;hey, two bodies are about to collide, what should I do?&#8221;&#8230; so you can decide to disable the contact for every collision you want.</p>
<p>The function used to do this task is <code>PreSolve</code>, working for all awake bodies that aren&#8217;t sensors.</p>
<p>If you don&#8217;t know what is a Box2D sensor, check <a href="http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/">Box2D Flash game creation tutorial – part 2</a>.</p>
<p>So the concept is: listen for collisions, if a collision involves the cloud wall and the player, then check if the player is higher or lower than the cloud. If it&#8217;s lower, don&#8217;t process the collision and let the player fly through the cloud.</p>
<p>Let&#8217;s see the script, directly taken from <a href="http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/">Box2D Flash game creation tutorial – part 2</a>:<span id="more-2448"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ball02 extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">// world creation</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> player<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #009900;">// force to apply to the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
		<span style="color: #009900;">// variables to store whether the keys are pressed or not</span>
		<span style="color: #009900;">// true = pressed;</span>
		<span style="color: #009900;">// false = unpressed</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">left</span>,<span style="color: #004993;">right</span>,up,down<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #009900;">// declaring my custom contact listener class</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> contact_listener=<span style="color: #0033ff; font-weight: bold;">new</span> custom_contact_listener<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ball02<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// assigning the contact listener to the world</span>
			world.SetContactListener<span style="color: #000000;">&#40;</span>contact_listener<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// calling debug draw function</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// drawing the boundaries</span>
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;roof&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">300</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;middle&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the player at 250,200</span>
			add_player<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">350</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding some coins</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>=<span style="color: #000000; font-weight:bold;">5</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				draw_coin<span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">150</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">25</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// listeners needed for the game to work</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,on_key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,on_key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key pressed, set the proper variable to &quot;true&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key released, set the proper variable to &quot;false&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to draw a coin</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_coin<span style="color: #000000;">&#40;</span>px,py,r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span>r<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			<span style="color: #009900;">// look! it's a sensor!!</span>
			my_fixture.isSensor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// simple function to draw a box</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to add the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> add_player<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			player=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			player.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;player&quot;</span><span style="color: #000000;">&#41;</span>;
			player.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// debug draw</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to be executed at every frame</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// setting the force to null</span>
			force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// according to the key(s) pressed, add the proper vector force</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">left</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">right</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>up<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>down<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// if there is any force, then apply it</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>force.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">||</span>force.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				player.ApplyForce<span style="color: #000000;">&#40;</span>force,player.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// scanning through all bodies</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> worldbody<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetBodyList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; worldbody; worldbody = worldbody.GetNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// if a body is marked as &quot;remove&quot;...</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>worldbody.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">// ... just remove it!!</span>
					world.DestroyBody<span style="color: #000000;">&#40;</span>worldbody<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 34</strong>: with the old function introduced at <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a> I create a box marked as &#8220;middle&#8221;.</p>
<p>No other changes on the main file, now let&#8217;s see the <code>custom_contact_listener.as</code> file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #9900cc; font-weight: bold;">class</span> custom_contact_listener extends b2ContactListener <span style="color: #000000;">&#123;</span>
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> BeginContact<span style="color: #000000;">&#40;</span>contact<span style="color: #000000; font-weight: bold;">:</span>b2Contact<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// getting the fixtures that collided</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureA<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureB<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// if the fixture is a sensor, mark the parent body to be removed</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureB.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureA.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PreSolve<span style="color: #000000;">&#40;</span>contact<span style="color: #000000; font-weight: bold;">:</span>b2Contact, oldManifold<span style="color: #000000; font-weight: bold;">:</span>b2Manifold<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// getting the fixtures that collided</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureA<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureB<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// variable to handle bodies y position</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> player_y_position<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> platform_y_position<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #009900;">// checking if the collision bodies are the ones marked as &quot;middle&quot; and &quot;player&quot;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;middle&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;player&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">||</span><span style="color: #000000;">&#40;</span>fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;player&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;middle&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// determining if the fixtureA represents the platform (&quot;middle&quot;) or the player</span>
				<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;middle&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						<span style="color: #009900;">// determining y positions</span>
						player_y_position=fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						platform_y_position=fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;player&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						<span style="color: #009900;">// determining y positions</span>
						player_y_position=fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						platform_y_position=fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
<span style="color: #000000; font-weight: bold;">&lt;</span>strong<span style="color: #000000; font-weight: bold;">&gt;</span>						<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #009900;">// checking distance between bodies</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">distance</span> = player_y_position<span style="color: #000000; font-weight: bold;">-</span>platform_y_position;
				<span style="color: #009900;">// if the distance is greater than player radius + half of the platform height...</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>distance<span style="color: #000000; font-weight: bold;">&gt;-</span><span style="color: #000000; font-weight:bold;">14.5</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">// don't manage the contact</span>
					contact.SetEnabled<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 22</strong>: beginning of the PreSolve function, the core of this example</p>
<p><strong>Lines 24-25</strong>: getting the fixtures that generated the contact</p>
<p><strong>Lines 27-28</strong>: declaring two variables to store y position of both bodies</p>
<p><strong>Line 30</strong>: here I am checking if the fixtures are the one associated to the player and the one associated to the cloud</p>
<p><strong>Lines 32-43</strong>: according to the fixture associated to the player and the one associated to the cloud, I am saving in the variables declared at <strong>lines 27-28</strong> the y position of both bodies. I am multiplying directly by <code>30</code> without passing the right <code>world_scale</code> value declared at <strong>line 13</strong> of the main class because it&#8217;s not the purpose of this tutorial.</p>
<p><strong>Line 45</strong>: Determining the vertical distance from the player and the cloud</p>
<p><strong>Lines 47-50</strong>: If the player is not at least <code>14.5</code> pixels higher than the cloud, then disable the contact. Why <code>14.5</code>? It&#8217;s the sum of the ball radius (10) and half the cloud height (5)&#8230; and I am not using <code>15</code> because I found sometimes the distance when the ball falls on the cloud is <code>14.93</code>, so the cloud won&#8217;t &#8220;hold&#8221; the ball, letting it fall down. With <code>14.5</code>, I am sure this won&#8217;t happen.</p>
<p>And this is the result&#8230; </p>
<p><embed src="/wp-content/uploads/2010/03/oneway.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Move the ball by tapping arrow keys and watch the static object in the center of the stage act as a cloud&#8230; you can fly through it from bottom to top, but you can&#8217;t do it from top to bottom.</p>
<p><a href="/wp-content/uploads/2010/03/oneway.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/02/understanding-box2ds-one-way-platforms-aka-clouds/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Box2D Flash game creation tutorial &#8211; part 2</title>
		<link>http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/</link>
		<comments>http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 09:56:23 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2434</guid>
		<description><![CDATA[After seeing the character creation in Box2D Flash game creation tutorial &#8211; part 1, it&#8217;s time to add some coins to collect.
This process will involve some interesting Box2D features, like sensors and custom collision management.
I would suggest to read the basics of sensors at Erase Box: the tutorial and custom collision management at Creation of [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing the character creation in <a href="http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/">Box2D Flash game creation tutorial &#8211; part 1</a>, it&#8217;s time to add some coins to collect.</p>
<p>This process will involve some interesting Box2D features, like sensors and custom collision management.</p>
<p>I would suggest to read the basics of sensors at <a href="http://www.emanueleferonato.com/2009/02/25/erase-box-the-tutorial/">Erase Box: the tutorial</a> and custom collision management at <a href="http://www.emanueleferonato.com/2009/11/23/creation-of-a-flash-stabilize-clone-using-box2d-part-4/">Creation of a Flash Stabilize! clone using Box2D – part 4</a>.</p>
<p>Although they are both referred to an older Box2D version, they&#8217;ll introduce you to sensor and collisions.</p>
<p>Now the concept is simple: we are placing some circular sensors around the stage (the coins), then we&#8217;ll create a custom contact listener class to check whether the player is over a coin or not. If it&#8217;s over, we&#8217;ll remove the coin.</p>
<p>So this is the main script:<span id="more-2434"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ball02 extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">// world creation</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> player<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #009900;">// force to apply to the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
		<span style="color: #009900;">// variables to store whether the keys are pressed or not</span>
		<span style="color: #009900;">// true = pressed;</span>
		<span style="color: #009900;">// false = unpressed</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">left</span>,<span style="color: #004993;">right</span>,up,down<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #009900;">// declaring my custom contact listener class</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> contact_listener=<span style="color: #0033ff; font-weight: bold;">new</span> custom_contact_listener<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ball02<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// assigning the contact listener to the world</span>
			world.SetContactListener<span style="color: #000000;">&#40;</span>contact_listener<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// calling debug draw function</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// drawing the boundaries</span>
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;roof&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the player at 250,200</span>
			add_player<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding some coins</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>=<span style="color: #000000; font-weight:bold;">5</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				draw_coin<span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">300</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// listeners needed for the game to work</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,on_key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,on_key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key pressed, set the proper variable to &quot;true&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key released, set the proper variable to &quot;false&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to draw a coin</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_coin<span style="color: #000000;">&#40;</span>px,py,r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span>r<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			<span style="color: #009900;">// look! it's a sensor!!</span>
			my_fixture.isSensor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// simple function to draw a box</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to add the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> add_player<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			player=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			player.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// debug draw</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to be executed at every frame</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// setting the force to null</span>
			force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// according to the key(s) pressed, add the proper vector force</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">left</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">right</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>up<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>down<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// if there is any force, then apply it</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>force.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">||</span>force.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				player.ApplyForce<span style="color: #000000;">&#40;</span>force,player.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// scanning through all bodies</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> worldbody<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetBodyList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; worldbody; worldbody = worldbody.GetNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// if a body is marked as &quot;remove&quot;...</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>worldbody.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">// ... just remove it!!</span>
					world.DestroyBody<span style="color: #000000;">&#40;</span>worldbody<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s see the interesting lines:</p>
<p><strong>Line 22</strong>: declaring my <code>contact_listener</code> variable, as <code>custom_contact_listener</code> type</p>
<p><strong>Line 25</strong>: assigning my custom contact listener class to Box2D world</p>
<p><strong>Lines 36-38</strong>: calling five times the <code>draw_coin</code> function passing three parameters: x position, y position and radius.</p>
<p><strong>Lines 79-89</strong>: the draw_coin function&#8230; just a basic function that draw a circle&#8230; just notice at <strong>line 86</strong> how I am declaring the circle as a sensor. This way the circle exists in the world but won&#8217;t physically collide with anything. Also, a sensor should be a static body, or it will fall down outside the stage as it won&#8217;t collide with anything.</p>
<p><strong>Lines 150-156</strong>: scanning through all bodies to find, and eventually remove, bodies marked with <code>remove</code>. Such marker is handled by the custom contact listener class, located in the <code>custom_contact_listener.as</code> file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #9900cc; font-weight: bold;">class</span> custom_contact_listener extends b2ContactListener <span style="color: #000000;">&#123;</span>
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> BeginContact<span style="color: #000000;">&#40;</span>contact<span style="color: #000000; font-weight: bold;">:</span>b2Contact<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// getting the fixtures that collided</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureA<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureB<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// if the fixture is a sensor, mark the parent body to be removed</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureB.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureA.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><code>BeginContact</code> function will give us the fixtures involved in the collision. Then at <strong>lines 15-17</strong> and <strong>18-20</strong> I am checking if the fixture is a sensor, then eventually mark its parent body to be removed.</p>
<p><strong>Important</strong>: don&#8217;t try to remove the body inside this function, because bodies have a <code>locked</code> status while they are in the middle of a timestep, so you should remove a body only after you performed the <code>Step</code> (<strong>line 147</strong> of the main file). This gave me a little headache, so you&#8217;ve been warned!</p>
<p>This is the result:</p>
<p><embed src="/wp-content/uploads/2010/02/ball02.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Pick up all little circles moving the player tapping on arrow keys.</p>
<p><a href="/wp-content/uploads/2010/02/ball02.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Box2D Flash game creation tutorial &#8211; part 1</title>
		<link>http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/</link>
		<comments>http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 00:16:24 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2395</guid>
		<description><![CDATA[My first Flash game tutorial ever was Flash game creation tutorial – part 1.
It was the first of a series to create a game like jmtb&#8217;s ball games. It was an old AS2 series, and some steps have been ported in AS3 with Create a Flash ball game using AS3.
The &#8220;ball&#8221; game is so simple [...]]]></description>
			<content:encoded><![CDATA[<p>My first Flash game tutorial ever was <a href="http://www.emanueleferonato.com/2006/10/29/flash-game-creation-tutorial-part-1/">Flash game creation tutorial – part 1</a>.</p>
<p>It was the first of a series to create a game like <a href="http://jmtb02.com/" target = "_blank">jmtb</a>&#8217;s ball games. It was an old AS2 series, and some steps have been ported in AS3 with <a href="http://www.emanueleferonato.com/2008/03/24/create-a-flash-ball-game-using-as3/">Create a Flash ball game using AS3</a>.</p>
<p>The &#8220;ball&#8221; game is so simple yet addictive and customizable that I think it&#8217;s the perfect game to start a tutorial series based on a new language.</p>
<p>This time I am not covering a new language but the famous Box2D library, but I am going to add all necessary features to make it an interesting game to play.</p>
<p>In this first chapter, I am going to create the ball and the way you control it, by tapping arrow keys.</p>
<p>I am using the basics of <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a> and <a href="http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/">Box2D tutorial for the absolute beginners – revamped</a>, which I recommend you to read.</p>
<p>Now this is the code:<span id="more-2395"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ball01 extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">// world creation</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> player<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #009900;">// force to apply to the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
		<span style="color: #009900;">// variables to store whether the keys are pressed or not</span>
		<span style="color: #009900;">// true = pressed;</span>
		<span style="color: #009900;">// false = unpressed</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">left</span>,<span style="color: #004993;">right</span>,up,down<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ball01<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// calling thebug draw function</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// drawing the boundaries</span>
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;roof&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the player at 250,200</span>
			<span style="color: #009900;">// some listeners</span>
			add_player<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,on_key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,on_key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key pressed, set the proper variable to &quot;true&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key released, set the proper variable to &quot;false&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// simple function to draw a box</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to add the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> add_player<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			player=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			player.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// debug draw</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// functiojn to be executed at every frame</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// setting the force to null</span>
			force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// according to the key(s) pressed, add the proper vector force</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">left</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">right</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>up<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>down<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// if there is any force, then apply it</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>force.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">||</span>force.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				player.ApplyForce<span style="color: #000000;">&#40;</span>force,player.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, I did not change that much from <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a> and <a href="http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/">Box2D tutorial for the absolute beginners – revamped</a>.</p>
<p><code>draw_circle </code> function at <strong>lines 19-30</strong> in <a href="http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/">Box2D tutorial for the absolute beginners – revamped</a> now is called add_player (<strong>lines 86-95</strong>) and places a circle in the (<code>px</code>,<code>py</code>) coordinates.</p>
<p>The body is named <code>player</code> and is declared in the class at <strong>line 14</strong>, then is used at <strong>line 125</strong> to have the force applied with ApplyForce explained at <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a>.</p>
<p>Notice how different forces are applied according to the arrow key pressed&#8230; playing with these values will change the gameplay&#8230; which values would you use?</p>
<p>This is the result:</p>
<p><embed src="/wp-content/uploads/2010/02/ball01.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Tap arrow keys to move the ball.</p>
<p><a href="/wp-content/uploads/2010/02/ball01.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Understanding Box2D applicable forces</title>
		<link>http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/</link>
		<comments>http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 12:19:07 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2378</guid>
		<description><![CDATA[In Box2d, bodies aren&#8217;t only affected by gravity and collisions, but you can also apply forces to them.
Knowing the right force to apply is very important when you want to control a body, as you may want to do in a Flash game
Let&#8217;s see the forces you can apply:
Applying a force
public function ApplyForce(force:b2Vec2, point:b2Vec2):void
Apply a [...]]]></description>
			<content:encoded><![CDATA[<p>In Box2d, bodies aren&#8217;t only affected by gravity and collisions, but you can also apply forces to them.</p>
<p>Knowing the right force to apply is very important when you want to control a body, as you may want to do in a Flash game</p>
<p>Let&#8217;s see the forces you can apply:</p>
<p><strong>Applying a force</strong></p>
<p><code>public function ApplyForce(force:b2Vec2, point:b2Vec2):void</code></p>
<p>Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body, so you don&#8217;t have to wake it up by yourself.</p>
<p>The force is applied inNewtons (N). </p>
<p><strong>Applying an impulse</strong></p>
<p><code>public function ApplyImpulse(impulse:b2Vec2, point:b2Vec2):void</code></p>
<p>Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body, so you don&#8217;t have to wake it up by yourself.</p>
<p>The impulse is applied in Newton-seconds or kg-m/s. </p>
<p><strong>Setting a linear velocity</strong></p>
<p><code>public function SetLinearVelocity(v:b2Vec2):void</code></p>
<p>Set the linear velocity of the center of mass. This function will <strong>not</strong> work if the body is sleeping, so you must wake it up with <code>SetAwake</code> before applying the velocity.</p>
<p><strong>What force should I apply?</strong></p>
<p>You should choose the force according to the result you want to achieve. Let&#8217;s suppose you want to apply a vertical force, to make something jump in the air, and let&#8217;s suppose you can apply the force at any time, even when you just applied it.</p>
<p>You will have three cases:</p>
<p><strong>Applying the force when the body is not moving</strong>: this happens when you apply the force when the body lies on the ground or when it&#8217;s in the air, in the moment it finished its &#8220;jumping&#8221; speed and it&#8217;s about to fall down. In this case there aren&#8217;t any noticeable difference among applied forces.</p>
<p><strong>Applying the force when the body is moving up</strong>: this happens when the body is in the air and still has &#8220;jumping&#8221; speed. In this case, applying a force or an impulse will sum the &#8220;jumping&#8221; speed to the applied force to produce a &#8220;boost&#8221;, while setting the velocity again will just apply the velocity again, no matter the &#8220;jumping speed&#8221;</p>
<p><strong>Applying the force when the body is falling down</strong>: this happens when the body finished its &#8220;jumping&#8221; speed and it&#8217;s falling down. As in the previous case, applying a force or an impulse will sum the falling speed to the applied force, and according to falling speed and applied force, the body will continue falling or jump a little (how little? the difference between the falling force and the applied one). Setting the velocity will just stop the body to fall and make it jumping again, no matter of the falling speed.<span id="more-2378"></span></p>
<p><strong>The script</strong></p>
<p>This script will generate three tiny boxes. The left one is moved with a force, the middle one with an impulse, and the third one with a set velocity. You will apply forces clicking the mouse</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> forces extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> forces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">&quot;middle&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>,applyforces<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> applyforces<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> worldbody<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetBodyList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; worldbody; worldbody = worldbody.GetNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>worldbody.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;left&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">450</span><span style="color: #000000;">&#41;</span>;
						worldbody.ApplyForce<span style="color: #000000;">&#40;</span>force,worldbody.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;middle&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span>;
						worldbody.ApplyImpulse<span style="color: #000000;">&#40;</span>force,worldbody.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;right&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span>;
						worldbody.SetAwake<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
						worldbody.SetLinearVelocity<span style="color: #000000;">&#40;</span>force<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And this is the result:</p>
<p><embed src="/wp-content/uploads/2010/02/forces.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Apply forces by clicking the mouse in various times, to see the difference among <code>ApplyForce</code>, <code>ApplyImpulse</code> and <code>SetLinearVelocity</code>.</p>
<p><a href="/wp-content/uploads/2010/02/forces.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Create incredible particle effects with Partigen 2</title>
		<link>http://www.emanueleferonato.com/2010/02/08/create-incredible-particle-effects-with-partigen-2/</link>
		<comments>http://www.emanueleferonato.com/2010/02/08/create-incredible-particle-effects-with-partigen-2/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 10:10:12 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Reviews]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2080</guid>
		<description><![CDATA[Do you remember Partigen?
Andrew Fitzgerald from Desuade released the new version, of his amazing particle effects engine: Partigen 2.

Featuring over 120 exclusive preset effects, Partigen 2 is the first and only extension for Flash that let&#8217;s you to create complex particle effects in just a click.
The list of features is huge, so I am listing [...]]]></description>
			<content:encoded><![CDATA[<p>Do you remember <a href="http://www.emanueleferonato.com/2007/08/20/create-amazing-particle-effects-in-flash-with-partigen-component/">Partigen</a>?</p>
<p><strong>Andrew Fitzgerald</strong> from Desuade released the new version, of his amazing particle effects engine: <strong><a href="http://desuade.com/partigen" target = "_blank">Partigen 2</a></strong>.</p>
<p><embed src="/wp-content/uploads/2010/02/chimney_swf.swf" allowscriptaccess="always" menu="false" quality="high" width="520" height="268" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Featuring over 120 exclusive preset effects, <a href="http://desuade.com/partigen" target = "_blank">Partigen 2</a> is the first and only extension for Flash that let&#8217;s you to create complex particle effects in just a click.</p>
<p>The list of features is huge, so I am listing the ones I found most interesting:</p>
<ul>
<li>Full Package XML-Serialization</li>
<li>Fully documented AS3 API</li>
<li>Emitters can be created with either the IDE component or via ActionScript</li>
<li>Particles can be any display object that inherits the Particle class</li>
<li>Renderers can be shared across multiple Emitters</li>
<li>Pools can handle the internal creation and storage of Particle objects in memory</li>
</ul>
<p>You can read the full list of features <a href="http://desuade.com/partigen/specs" target = "_blank">here</a></p>
<p>The documentation is awesome, the best I&#8217;ve seen so far in a product like this one. You can access the <a target  ="_blank" href="http://api.desuade.com/en_as3/">full API documentation</a>, and from <a href="http://desuade.com/partigen/demos" target = "_blank">this link</a> you can access a 42 minutes long video covering the entire Partigen 2 component, and you can create beautiful effects using the component in less than a minute.</p>
<p>Anyway, we&#8217;re not here to talk about the component, but to test the AS3 API.<span id="more-2080"></span></p>
<p><strong>Playing with AS3 API</strong></p>
<p>In this example, I am going to create a particle effect with the user interface provided in the component, then I&#8217;ll export it with XML and finally add it &#8220;on the fly&#8221; to the <a href="http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/">Box2D car example</a>.</p>
<p>Playing with the presets is easy and fun, and once you&#8217;re done, just press &#8220;Copy to XML&#8221; to have the XML copied into your dashboard</p>
<p><img src="/wp-content/uploads/2010/02/part.png" /></p>
<p>Then, since we don&#8217;t want external files in our game, this is the code we need:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #009900;">//</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #009900;">// partigen libraries</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com.desuade.partigen.emitters.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> com.desuade.partigen.renderers.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> partigen extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> car_body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_wheel<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_wheel<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_motor<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_motor<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> fixtureDef<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_motor_added<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJoint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_motor_added<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJoint;
		<span style="color: #009900;">// partigen variables</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> the_particle<span style="color: #000000; font-weight: bold;">:</span>particle_mc = <span style="color: #0033ff; font-weight: bold;">new</span> particle_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> particle_emitter<span style="color: #000000; font-weight: bold;">:</span>Emitter = <span style="color: #0033ff; font-weight: bold;">new</span> Emitter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> partigen<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			world=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
			<span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> boxDef<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> circleDef<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> revoluteJointDef<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">600</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			fixtureDef.shape=boxDef;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			fixtureDef.shape=boxDef;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			fixtureDef.shape=boxDef;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			bodyDef.userData = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			fixtureDef.shape=boxDef;
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			fixtureDef.restitution=<span style="color: #000000; font-weight:bold;">0.1</span>;
			car_body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			car_body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">5</span>;
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">0.1</span>;
			fixtureDef.shape=circleDef;
			bodyDef.allowSleep=<span style="color: #0033ff; font-weight: bold;">false</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			front_wheel=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			front_wheel.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			rear_wheel=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			rear_wheel.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;
			front_motor.enableMotor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			front_motor.maxMotorTorque=<span style="color: #000000; font-weight:bold;">10</span>;
			front_motor.Initialize<span style="color: #000000;">&#40;</span>car_body, front_wheel, front_wheel.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			front_motor_added=world.CreateJoint<span style="color: #000000;">&#40;</span>front_motor<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2RevoluteJoint;
			rear_motor.enableMotor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			rear_motor.maxMotorTorque=<span style="color: #000000; font-weight:bold;">1</span>;
			rear_motor.Initialize<span style="color: #000000;">&#40;</span>car_body, rear_wheel, rear_wheel.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			rear_motor_added=world.CreateJoint<span style="color: #000000;">&#40;</span>rear_motor<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2RevoluteJoint;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>, key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// partigen code</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> particle_container<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>particle_container<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> particle_renderer<span style="color: #000000; font-weight: bold;">:</span>Renderer=<span style="color: #0033ff; font-weight: bold;">new</span> StandardRenderer<span style="color: #000000;">&#40;</span>particle_container,<span style="color: #990000;">'top'</span><span style="color: #000000;">&#41;</span>;
			particle_emitter.renderer=particle_renderer;
			particle_emitter.particle=particle_mc;
			<span style="color: #6699cc; font-weight: bold;">var</span> emmx<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = 
			<span style="color: #000000; font-weight: bold;">&lt;</span>IDEEmitter particle=<span style="color: #990000;">&quot;particle_mc&quot;</span> eps=<span style="color: #990000;">&quot;10&quot;</span> burst=<span style="color: #990000;">&quot;2&quot;</span> life=<span style="color: #990000;">&quot;1&quot;</span> lifeSpread=<span style="color: #990000;">&quot;*0.5&quot;</span> group=<span style="color: #990000;">&quot;GroupParticle&quot;</span> groupAmount=<span style="color: #990000;">&quot;0&quot;</span> groupProximity=<span style="color: #990000;">&quot;10&quot;</span> <span style="color: #004993;">angle</span>=<span style="color: #990000;">&quot;0&quot;</span> angleSpread=<span style="color: #990000;">&quot;*360&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			  <span style="color: #000000; font-weight: bold;">&lt;</span>Controllers<span style="color: #000000; font-weight: bold;">&gt;</span>
			    <span style="color: #000000; font-weight: bold;">&lt;</span>EmitterController<span style="color: #000000; font-weight: bold;">/&gt;</span>
			    <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleController<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;rotation&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*200&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> <span style="color: #004993;">value</span>=<span style="color: #990000;">&quot;0&quot;</span> spread=<span style="color: #990000;">&quot;*100&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;</span>ParticlePhysicsController duration=<span style="color: #990000;">&quot;0&quot;</span> flip=<span style="color: #990000;">&quot;false&quot;</span> useAngle=<span style="color: #990000;">&quot;true&quot;</span> property=<span style="color: #990000;">&quot;x&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;velocity&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> <span style="color: #004993;">value</span>=<span style="color: #990000;">&quot;3&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;acceleration&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;friction&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticlePhysicsController<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;alpha&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> <span style="color: #004993;">value</span>=<span style="color: #990000;">&quot;0.1&quot;</span> spread=<span style="color: #990000;">&quot;*0.8&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;scale&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> <span style="color: #004993;">value</span>=<span style="color: #990000;">&quot;0.3&quot;</span> spread=<span style="color: #990000;">&quot;*0.5&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> <span style="color: #004993;">value</span>=<span style="color: #990000;">&quot;0&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;</span>ParticlePhysicsController duration=<span style="color: #990000;">&quot;0&quot;</span> flip=<span style="color: #990000;">&quot;true&quot;</span> useAngle=<span style="color: #990000;">&quot;true&quot;</span> property=<span style="color: #990000;">&quot;y&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;velocity&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> <span style="color: #004993;">value</span>=<span style="color: #990000;">&quot;3&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;acceleration&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;</span>ParticleTweenController duration=<span style="color: #990000;">&quot;0&quot;</span> property=<span style="color: #990000;">&quot;friction&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;</span>KeyframeContainer tweenClass=<span style="color: #990000;">&quot;BasicTween&quot;</span> precision=<span style="color: #990000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;0&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;begin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			            <span style="color: #000000; font-weight: bold;">&lt;</span>Keyframe <span style="color: #004993;">position</span>=<span style="color: #990000;">&quot;1&quot;</span> ease=<span style="color: #990000;">&quot;linear&quot;</span> spread=<span style="color: #990000;">&quot;*0&quot;</span> label=<span style="color: #990000;">&quot;end&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
			          <span style="color: #000000; font-weight: bold;">&lt;/</span>KeyframeContainer<span style="color: #000000; font-weight: bold;">&gt;</span>
			        <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleTweenController<span style="color: #000000; font-weight: bold;">&gt;</span>
			      <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticlePhysicsController<span style="color: #000000; font-weight: bold;">&gt;</span>
			    <span style="color: #000000; font-weight: bold;">&lt;/</span>ParticleController<span style="color: #000000; font-weight: bold;">&gt;</span>
			  <span style="color: #000000; font-weight: bold;">&lt;/</span>Controllers<span style="color: #000000; font-weight: bold;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;/</span>IDEEmitter<span style="color: #000000; font-weight: bold;">&gt;</span>;
			particle_emitter.fromXML<span style="color: #000000;">&#40;</span>emmx<span style="color: #000000;">&#41;</span>;
			particle_emitter.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					rear_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					front_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					rear_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					front_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> m_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>;
			m_sprite = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>m_sprite<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgDraw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgSprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_sprite.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>dbgSprite<span style="color: #000000;">&#41;</span>;
			dbgDraw.SetSprite<span style="color: #000000;">&#40;</span>m_sprite<span style="color: #000000;">&#41;</span>;
			dbgDraw.SetDrawScale<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			dbgDraw.SetFillAlpha<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span>;
			dbgDraw.SetLineThickness<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
			dbgDraw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>dbgDraw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> bb<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetBodyList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; bb; bb = bb.GetNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>bb.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					particle_emitter.<span style="color: #004993;">x</span>=bb.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
					particle_emitter.<span style="color: #004993;">y</span>=bb.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the partigen code is very small, the most of the code is the particle XML itself.</p>
<p>And this is the result: I wanted fake bodies to spread from the car&#8217;s body.</p>
<p><embed src="/wp-content/uploads/2010/02/partigen.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="200" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Move the car with left/right arrow keys and enjoy.</p>
<p><strong>Final considerations</strong></p>
<p><a href="http://desuade.com/partigen" target = "_blank">Partigen 2</a> is the most complete tool to create particle effects with Flash IDE and API, in a couple of minutes you will be able to create amazing effects, saving hours of trial and error.</p>
<p>Desuade is so confident that you&#8217;ll fall even more in love with Partigen 2, it comes with a <strong>100% satisfaction, 60 day money-back guarantee</strong>. A single license costs <strong>$97</strong>, and it&#8217;s positively a must-have.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/08/create-incredible-particle-effects-with-partigen-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Box2D tutorial for the absolute beginners &#8211; revamped</title>
		<link>http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/</link>
		<comments>http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 21:55:11 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2301</guid>
		<description><![CDATA[About a year ago I published a Box2D tutorial for the absolute beginners.
With 2.1 release, a lot of things changed, so it&#8217;s time to publish another tutorial for the absolute beginners.
In this tutorial we&#8217;ll cover needed libraries to import, world creation, debug draw and the creation of static and dynamic boxes and circles. And obviously [...]]]></description>
			<content:encoded><![CDATA[<p>About a year ago I published a <a href="http://www.emanueleferonato.com/2009/01/27/box2d-tutorial-for-the-absolute-beginners/">Box2D tutorial for the absolute beginners</a>.</p>
<p>With <a href="http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/">2.1 release</a>, a lot of things changed, so it&#8217;s time to publish another tutorial for the absolute beginners.</p>
<p>In this tutorial we&#8217;ll cover needed libraries to import, world creation, debug draw and the creation of static and dynamic boxes and circles. And obviously the simulation itself</p>
<p>This is the movie we are going to make:</p>
<p><embed src="/wp-content/uploads/2010/02/absolute.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></p>
<p>And this is the script used to do it:<span id="more-2301"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> absolute extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> absolute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">300</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			draw_circle<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
			draw_circle<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_circle<span style="color: #000000;">&#40;</span>px,py,r,<span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span>r<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Libraries</strong></p>
<p>Libraries are imported at <strong>lines 4-7</strong>. <code>Box2D</code> folder and your <code>.fla</code> and <code>.as</code> files must be in the same folder</p>
<p><strong>World creation</strong></p>
<p>The only line needed to create an empty world is <strong>line 9</strong>. The world class is <code>b2World</code> and you must pass two parameters:</p>
<p>* the first one is a vector representing the world gravity.</p>
<p>* the second one is a boolean that allows you to improve performance by not simulating inactive bodies. In Box2D terminology, an inactive body is called &#8220;sleeping body&#8221;.</p>
<p>Now the world is ready, but you must face the problem of representing meters with pixels. Box2D works with meters, and there is a rule saying 1 meter = 30pixels. That&#8217;s why at <strong>line 10</strong> I am declaring <code>world_scale</code> at <code>30</code>. You can find more information about pixels and meters at <a href="http://www.emanueleferonato.com/2008/11/16/understanding-pixels-and-meters-with-box2d-and-how-to-select-an-object-with-mouse-part-1/">this post</a>.</p>
<p><strong>Debug draw</strong></p>
<p>As you should know, Box2D does not render the scene, it&#8217;s up to you attaching real objects to Box2D bodies. Anyway, there is a debug environment you can see at <strong>lines 44-52</strong>. This is the simplest debug draw you can make: basically you create a sprite and assign it to a b2DebugDraw class. More theory about debug draw theory at <a href="http://www.emanueleferonato.com/2008/12/10/understanding-box2d-debug-draw/">this post</a>.</p>
<p>There are more options to configure the debug draw, but at the moment we are keeping things as simpler as we can. Notice at <strong>line 49</strong> how I am setting the debug scale at the same value of the world scale.</p>
<p><strong>Creation of a box</strong></p>
<p>The most basic box is defined by an origin (the centre of the box), a width and an height. We are doing it at <strong>lines 31-43</strong>.</p>
<p>Let&#8217;s see the anatomy of a simple Box2D box.</p>
<p>A box is a body that must be static or dynamic. Only dynamic boxes are affected by gravity, collisions and physics forces. Static ones remain immovable.</p>
<p>Once we create a body (<strong>line 32</strong>) place it into the world (<strong>line 33</strong>) and set it as dynamic or not (<strong>lines 34-36</strong>), we just created a generic body.</p>
<p>It&#8217;s time to create a polygon shape (<strong>line 37</strong>) and define it as a box (<strong>line 38</strong>). Notice when I define the shape as a box, both width and height are divided by the world scale and by 2. The second division is necessary because the <code>SetAsBox</code> function wants the half width and the half height.</p>
<p>Now we have a body and a polygon. It&#8217;s time to create a fixture. The fixture will hold various attributes such as density and restitution, but at the moment to keep things easy I just want to create a fixture (<strong>line 39</strong>) and assigning it the polygon shape previously created (<strong>line 40</strong>)</p>
<p>Then it&#8217;s time to place the generic body in the world (<strong>line 41</strong>) and to assign it the fixture previously created with the attached shape (<strong>line 42</strong>).</p>
<p>And finally the box is created.</p>
<p><strong>Creation of a circle</strong></p>
<p>The creation of a circle is very similar to the box creation, the only difference is the shape this time is a circle shape (<strong>line 25</strong>) the radius as parameter.</p>
<p><strong>The simulation</strong></p>
<p>We simulate the world in three steps: first we update the world (<strong>line 54</strong>) passing the amount of time to simulate (normally 1/30), the velocity constraint solver and the position constraint solver. </p>
<p>Constraint solvers determine how many times the engine sweeps over all the contacts and joints in the world to find the final position and velocity of each body. More iterations always yields a better simulation.</p>
<p>Finally we need to clear forces (<strong>line 55</strong>) to give more stability and it&#8217;s time to draw the debug data (<strong>line 56</strong>) if you are using the debug draw.</p>
<p><strong>In the end&#8230;</strong></p>
<p>In the end I created a large static box (<strong>line 13</strong>), a small dynamic one (<strong>line 14</strong>), a static (<strong>line 15</strong>) and a dynamic (<strong>line 16</strong>) sphere and I am updating the world at every frame (<strong>line 17</strong>).</p>
<p><a href="/wp-content/uploads/2010/02/absolute.zip">Download the source code</a>, library included.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Box2DFlash 2.1a released &#8211; what changed</title>
		<link>http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/</link>
		<comments>http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 15:21:25 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2281</guid>
		<description><![CDATA[Some days ago Boris the Brave released the new version of the popular physics library.
There are some critical changes that won&#8217;t make old projects run in the new environment.
The most important ones are:

It is now not necessary to specify a size for your world, it&#8217;ll always be large enough.
Improved collisions system
You must specify if a [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago <a href="http://personal.boristhebrave.com/" target = "_blank">Boris the Brave</a> released the new version of the popular physics library.</p>
<p>There are some critical changes that won&#8217;t make old projects run in the new environment.</p>
<p>The most important ones are:</p>
<ul>
<li>It is now not necessary to specify a size for your world, it&#8217;ll always be large enough.</li>
<li>Improved collisions system</li>
<li>You must specify if a body is dynamic, no matter if its mass is greater than zero</li>
<li>Fixtures now determine material properties of shapes, such as density, friction and so on</li>
</ul>
<p>But I think an example will be more explicative than a thousand words, so I created a simple vehicle you can control with left and right arrow keys. It uses <a href="http://www.emanueleferonato.com/2009/01/13/box2d-joints-revolute-joint/">revolute joints</a> and <a href="http://www.emanueleferonato.com/2009/01/19/box2d-joints-revolute-joint-building-motors/">motors</a>. </p>
<p><embed src="/wp-content/uploads/2010/01/car.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="200" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></p>
<p>This is the code you would need in the old Box2D version<span id="more-2281"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> car extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> car_body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_wheel<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_wheel<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_motor<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_motor<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_motor_added<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJoint
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_motor_added<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJoint
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// world setup</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> worldAABB<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			worldAABB.lowerBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>;
			worldAABB.upperBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>;
			world=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span>worldAABB,<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// variables used for bodies and motors</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
			<span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> boxDef<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> circleDef<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> revoluteJointDef<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the ground</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">600</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			boxDef.density=<span style="color: #000000; font-weight:bold;">0</span>;
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>boxDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			boxDef.density=<span style="color: #000000; font-weight:bold;">0</span>;
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>boxDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			boxDef.density=<span style="color: #000000; font-weight:bold;">0</span>;
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>boxDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding car body</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			car_body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			boxDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			boxDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;
			boxDef.restitution=<span style="color: #000000; font-weight:bold;">0.1</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			car_body.CreateShape<span style="color: #000000;">&#40;</span>boxDef<span style="color: #000000;">&#41;</span>;
			car_body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding wheels</span>
			circleDef.radius=<span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
			circleDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			circleDef.friction=<span style="color: #000000; font-weight:bold;">5</span>;
			circleDef.restitution=<span style="color: #000000; font-weight:bold;">0.1</span>;
			<span style="color: #009900;">// front wheel</span>
			bodyDef.allowSleep = <span style="color: #0033ff; font-weight: bold;">false</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			front_wheel=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			front_wheel.CreateShape<span style="color: #000000;">&#40;</span>circleDef<span style="color: #000000;">&#41;</span>;
			front_wheel.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// rear wheel</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			rear_wheel=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			rear_wheel.CreateShape<span style="color: #000000;">&#40;</span>circleDef<span style="color: #000000;">&#41;</span>;
			rear_wheel.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// front joint</span>
			front_motor.enableMotor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			front_motor.maxMotorTorque=<span style="color: #000000; font-weight:bold;">10</span>;
			front_motor.Initialize<span style="color: #000000;">&#40;</span>car_body, front_wheel, front_wheel.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			front_motor_added=world.CreateJoint<span style="color: #000000;">&#40;</span>front_motor<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2RevoluteJoint;
			<span style="color: #009900;">// rear joint</span>
			rear_motor.enableMotor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			rear_motor.maxMotorTorque=<span style="color: #000000; font-weight:bold;">1</span>;
			rear_motor.Initialize<span style="color: #000000;">&#40;</span>car_body, rear_wheel, rear_wheel.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			rear_motor_added=world.CreateJoint<span style="color: #000000;">&#40;</span>rear_motor<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2RevoluteJoint;
			<span style="color: #009900;">//</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>, key_down<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					rear_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					front_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					rear_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					front_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> m_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>;
			m_sprite = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>m_sprite<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgDraw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgSprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_sprite.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>dbgSprite<span style="color: #000000;">&#41;</span>;
			dbgDraw.m_sprite=m_sprite;
			dbgDraw.m_drawScale=<span style="color: #000000; font-weight:bold;">30</span>;
			dbgDraw.m_alpha=<span style="color: #000000; font-weight:bold;">1</span>;
			dbgDraw.m_fillAlpha=<span style="color: #000000; font-weight:bold;">0.5</span>;
			dbgDraw.m_lineThickness=<span style="color: #000000; font-weight:bold;">1</span>;
			dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>dbgDraw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Take a look at the compiler errors&#8230;</p>
<p><img src="/wp-content/uploads/2010/01/errors.jpg" /></p>
<p>And this is the new one. All changes are commented.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> car extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> car_body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_wheel<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_wheel<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_motor<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_motor<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #009900;">// this is new!!!</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> fixtureDef<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rear_motor_added<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJoint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> front_motor_added<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJoint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// world setup</span>
			<span style="color: #3f5fbf;">/*var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-100, -100);
			worldAABB.upperBound.Set(100, 100);*/</span>
			world=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was world=new b2World(worldAABB,new b2Vec2(0,10.0),true);</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// variables used for bodies and motors</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
			<span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> boxDef<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was var boxDef:b2PolygonDef = new b2PolygonDef();</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> circleDef<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was var circleDef:b2CircleDef = new b2CircleDef();</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> revoluteJointDef<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the ground</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">600</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// this is new!!</span>
			fixtureDef.shape=boxDef;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.friction=1;</span>
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.density=0;</span>
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was body.CreateShape(boxDef);</span>
			<span style="color: #009900;">// remove: body.SetMassFromShapes();</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			fixtureDef.shape=boxDef;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.friction=1;</span>
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.density=0;</span>
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was body.CreateShape(boxDef);</span>
			<span style="color: #009900;">// remove: body.SetMassFromShapes();</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// this is new!!</span>
			fixtureDef.shape=boxDef;
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.friction=1;</span>
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.density=0;</span>
			body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was body.CreateShape(boxDef);</span>
			<span style="color: #009900;">// remove: body.SetMassFromShapes();</span>
			<span style="color: #009900;">// adding car body</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// this is new!!</span>
			bodyDef.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// this is new!!</span>
			fixtureDef.shape=boxDef;
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.density=1;</span>
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was boxDef.friction=1;</span>
			fixtureDef.restitution=<span style="color: #000000; font-weight:bold;">0.1</span>;<span style="color: #009900;">// was boxDef.restitution=0.1;</span>
			car_body=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			car_body.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was body.CreateShape(boxDef);</span>
			<span style="color: #009900;">// remove: car_body.SetMassFromShapes();</span>
			<span style="color: #009900;">// adding wheels</span>
			<span style="color: #009900;">// remove: circleDef.radius=20/30;</span>
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #009900;">// was circleDef.density=1;</span>
			fixtureDef.friction=<span style="color: #000000; font-weight:bold;">5</span>;<span style="color: #009900;">// was circleDef.friction=5;</span>
			fixtureDef.density=<span style="color: #000000; font-weight:bold;">0.1</span>;<span style="color: #009900;">// was circleDef.restitution=0.1;</span>
			<span style="color: #009900;">// this is new!!</span>
			fixtureDef.shape=circleDef;
			<span style="color: #009900;">// front wheel</span>
			bodyDef.allowSleep=<span style="color: #0033ff; font-weight: bold;">false</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			front_wheel=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			front_wheel.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was front_wheel.CreateShape(circleDef);</span>
			<span style="color: #009900;">// remove: front_wheel.SetMassFromShapes();</span>
			<span style="color: #009900;">// rear wheel</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>, car_body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			rear_wheel=world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			rear_wheel.CreateFixture<span style="color: #000000;">&#40;</span>fixtureDef<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was rear_wheel.CreateShape(circleDef);</span>
			<span style="color: #009900;">// remove: rear_wheel.SetMassFromShapes();</span>
			<span style="color: #009900;">// front joint</span>
			front_motor.enableMotor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			front_motor.maxMotorTorque=<span style="color: #000000; font-weight:bold;">10</span>;
			front_motor.Initialize<span style="color: #000000;">&#40;</span>car_body, front_wheel, front_wheel.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			front_motor_added=world.CreateJoint<span style="color: #000000;">&#40;</span>front_motor<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2RevoluteJoint;
			<span style="color: #009900;">// rear joint</span>
			rear_motor.enableMotor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			rear_motor.maxMotorTorque=<span style="color: #000000; font-weight:bold;">1</span>;
			rear_motor.Initialize<span style="color: #000000;">&#40;</span>car_body, rear_wheel, rear_wheel.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			rear_motor_added=world.CreateJoint<span style="color: #000000;">&#40;</span>rear_motor<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2RevoluteJoint;
			<span style="color: #009900;">//</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>, key_down<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					rear_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					front_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					rear_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					front_motor_added.SetMotorSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> m_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>;
			m_sprite = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>m_sprite<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgDraw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgSprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_sprite.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>dbgSprite<span style="color: #000000;">&#41;</span>;
			dbgDraw.SetSprite<span style="color: #000000;">&#40;</span>m_sprite<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// was dbgDraw.m_sprite=m_sprite;</span>
			dbgDraw.SetDrawScale<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// was dbgDraw.m_drawScale=30;</span>
			<span style="color: #009900;">//dbgDraw.SetAlpha(1); // was dbgDraw.m_alpha=1;</span>
			dbgDraw.SetFillAlpha<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// was dbgDraw.m_fillAlpha=0.5;</span>
			dbgDraw.SetLineThickness<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// was dbgDraw.m_lineThickness=1;</span>
			dbgDraw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// was dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;</span>
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>dbgDraw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">// was world.Step(1/30, 10);</span>
			<span style="color: #009900;">// this is new!</span>
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
			<span style="color: #009900;">// this is new!!</span>
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href= "/wp-content/uploads/2010/01/car.zip">Download the source code</a>, new library included.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Way of an Idea Box2D prototype – Step 3</title>
		<link>http://www.emanueleferonato.com/2010/01/19/way-of-an-idea-box2d-prototype-%e2%80%93-step-3/</link>
		<comments>http://www.emanueleferonato.com/2010/01/19/way-of-an-idea-box2d-prototype-%e2%80%93-step-3/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 16:56:26 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2248</guid>
		<description><![CDATA[Welcome to the 3rd part. In part 2 we allowed the player to draw the chalk track in a &#8220;paused&#8221; Box2D environment and then run the simulation.
Now it&#8217;s time to delete our chalk track. 
A bit of theory: although the simulation is paused, the chalk bodies are already placed in the Box2D world. So we [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the 3rd part. In <a href="http://www.emanueleferonato.com/2010/01/07/way-of-an-idea-box2d-prototype-step-2/">part 2</a> we allowed the player to draw the chalk track in a &#8220;paused&#8221; Box2D environment and then run the simulation.</p>
<p>Now it&#8217;s time to delete our chalk track. </p>
<p>A bit of theory: although the simulation is paused, the chalk bodies are already placed in the Box2D world. So we can easily select them with our old friend <code>GetBodyAtMouse</code> function.</p>
<p>Then we need to know whether the selected body is a chalk or not&#8230; we don&#8217;t want to delete other level assets or even the ball!!</p>
<p>So we must check if the <code>userData</code> of the selected body (that is the attached sprite) is a chalk. If true, we can remove the sprite from the stage and destroy the body.</p>
<p>This is the code:<span id="more-2248"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> <span style="color: #004993;">draw</span> extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> drawing<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> canvas<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> points_array<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> pixel_dist<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">20</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> saved_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> saved_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_world<span style="color: #000000; font-weight: bold;">:</span>b2World;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> circleDef<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mousePVec<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #009900;">// radians to degrees conversion</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rad_to_deg<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">57.2957795</span>;
		<span style="color: #009900;">// simulation timestep. We start at zero.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> timestep<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// world creation</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> worldAABB<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			worldAABB.lowerBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100.0</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100.0</span><span style="color: #000000;">&#41;</span>;
			worldAABB.upperBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100.0</span>, <span style="color: #000000; font-weight:bold;">100.0</span><span style="color: #000000;">&#41;</span>;
			m_world=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span>worldAABB,<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//</span>
			<span style="color: #009900;">// *** IMPORTANT: NOTICE THERE ISN'T ANY DEBUG DRAW ROUTINE ***</span>
			<span style="color: #009900;">// </span>
			<span style="color: #009900;">// placing the ball</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			circleDef.radius=<span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
			circleDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			circleDef.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
			circleDef.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
			<span style="color: #009900;">// ball's movieclip attachment</span>
			bodyDef.userData = <span style="color: #0033ff; font-weight: bold;">new</span> sphere<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.userData.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">20</span>;
			bodyDef.userData.<span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">20</span>;
			bodyDef.userData.<span style="color: #004993;">width</span>=<span style="color: #000000; font-weight:bold;">30</span>;
			bodyDef.userData.<span style="color: #004993;">height</span>=<span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>bodyDef.userData<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// end ball's movieclip attachment</span>
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>circleDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>canvas<span style="color: #000000;">&#41;</span>;
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>,mouse_pressed<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_MOVE</span>,mouse_moved<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>,mouse_released<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, Update, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>, key_down<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_pressed<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			drawing=<span style="color: #0033ff; font-weight: bold;">true</span>;
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span>,<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span>;
			points_array=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			saved_x=<span style="color: #004993;">mouseX</span>;
			saved_y=<span style="color: #004993;">mouseY</span>;
			points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_x<span style="color: #000000;">&#41;</span>;
			points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_y<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_moved<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>drawing<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> dist_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=mouseX<span style="color: #000000; font-weight: bold;">-</span>saved_x;
				<span style="color: #6699cc; font-weight: bold;">var</span> dist_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=mouseY<span style="color: #000000; font-weight: bold;">-</span>saved_y;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>dist_x<span style="color: #000000; font-weight: bold;">*</span>dist_x<span style="color: #000000; font-weight: bold;">+</span>dist_y<span style="color: #000000; font-weight: bold;">*</span>dist_y<span style="color: #000000; font-weight: bold;">&gt;</span>pixel_dist<span style="color: #000000; font-weight: bold;">*</span>pixel_dist<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span>,<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span>;
					saved_x=<span style="color: #004993;">mouseX</span>;
					saved_y=<span style="color: #004993;">mouseY</span>;
					points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_x<span style="color: #000000;">&#41;</span>;
					points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_y<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_released<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			drawing=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> sx<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ex<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> sy<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ey<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dist_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dist_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dist<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> segments<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=points_array.<span style="color: #004993;">length</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>segments; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				sx=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>;
				sy=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>;
				ex=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>;
				ey=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span>;
				dist_x=sx<span style="color: #000000; font-weight: bold;">-</span>ex;
				dist_y=sy<span style="color: #000000; font-weight: bold;">-</span>ey;
				dist=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sqrt</span><span style="color: #000000;">&#40;</span>dist_x<span style="color: #000000; font-weight: bold;">*</span>dist_x<span style="color: #000000; font-weight: bold;">+</span>dist_y<span style="color: #000000; font-weight: bold;">*</span>dist_y<span style="color: #000000;">&#41;</span>;
				<span style="color: #004993;">angle</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>dist_y,dist_x<span style="color: #000000;">&#41;</span>;
				create_box<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>sx<span style="color: #000000; font-weight: bold;">+</span>ex<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span>,<span style="color: #000000;">&#40;</span>sy<span style="color: #000000; font-weight: bold;">+</span>ey<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>dist<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to be called if the player presses space</span>
		<span style="color: #009900;">// switching timestep</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// pressing SPACE</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>timestep<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					timestep=<span style="color: #000000; font-weight:bold;">0</span>;
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
					timestep=<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// pressing &quot;0&quot; (zero)</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">48</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// finding the body under the mouse</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body=GetBodyAtMouse<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #009900;">// if there is an object under the mouse...</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">// if the body's userdata is a chalk</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body.m_userData <span style="color: #0033ff; font-weight: bold;">is</span> chalk<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						<span style="color: #009900;">// removing the Sprite</span>
						<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>body.m_userData<span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// destroying the object</span>
						m_world.DestroyBody<span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// this is our old friend GetBodyAtMouse</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> GetBodyAtMouse<span style="color: #000000;">&#40;</span>includeStatic<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>b2Body <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> mouseXWorldPhys = <span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> mouseYWorldPhys = <span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
			mousePVec.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> aabb<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			aabb.lowerBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
			aabb.upperBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> k_maxCount<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">10</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> shapes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=m_world.Query<span style="color: #000000;">&#40;</span>aabb,shapes,k_maxCount<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body=<span style="color: #0033ff; font-weight: bold;">null</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> count; <span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>shapes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.m_body.IsStatic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000; font-weight: bold;">||</span>includeStatic<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> tShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape=shapes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2Shape;
					<span style="color: #6699cc; font-weight: bold;">var</span> inside<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=tShape.TestPoint<span style="color: #000000;">&#40;</span>tShape.m_body.GetXForm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,mousePVec<span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>inside<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						body=tShape.m_body;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> body;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> create_box<span style="color: #000000;">&#40;</span>px,py,w,h,a<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
			<span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef;
			<span style="color: #6699cc; font-weight: bold;">var</span> boxDef<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef;
			bodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">angle</span>=a;
			boxDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span>w,h<span style="color: #000000;">&#41;</span>;
			boxDef.friction=<span style="color: #000000; font-weight:bold;">0.3</span>;
			boxDef.density=<span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #009900;">// box's movieclip attachment</span>
			bodyDef.userData = <span style="color: #0033ff; font-weight: bold;">new</span> chalk<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.userData.<span style="color: #004993;">x</span>=px<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
			bodyDef.userData.<span style="color: #004993;">y</span>=py<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
			bodyDef.userData.<span style="color: #004993;">width</span>=w<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">60</span>;
			bodyDef.userData.<span style="color: #004993;">height</span>=h<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">60</span>;
			bodyDef.userData.<span style="color: #004993;">rotation</span>=a<span style="color: #000000; font-weight: bold;">*</span>rad_to_deg;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>bodyDef.userData<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// end box's movieclip attachment</span>
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>boxDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// executing Step function and updating movieclips positions only </span>
			<span style="color: #009900;">// if timestep != 0</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>timestep<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				m_world.Step<span style="color: #000000;">&#40;</span>timestep, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> bb<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_world.m_bodyList; bb; bb = bb.m_next<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>bb.m_userData <span style="color: #0033ff; font-weight: bold;">is</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						bb.m_userData.<span style="color: #004993;">x</span>=bb.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						bb.m_userData.<span style="color: #004993;">y</span>=bb.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						bb.m_userData.<span style="color: #004993;">rotation</span> = bb.GetAngle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And this is the result:</p>
<p><embed src="/wp-content/uploads/2010/01/draw31.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Draw the track with your mouse, press SPACE to begin the simulation and delete the chalk pressing 0 (zero) and moving the mouse over the chalk.</p>
<p>No need to download anything since you can easily cut-paste this code on the <a href="http://www.emanueleferonato.com/2010/01/07/way-of-an-idea-box2d-prototype-step-2/">Step 2</a> example</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/01/19/way-of-an-idea-box2d-prototype-%e2%80%93-step-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Shrink it Box2D prototype &#8211; Step 2</title>
		<link>http://www.emanueleferonato.com/2010/01/09/shrink-it-box2d-prototype-step-2/</link>
		<comments>http://www.emanueleferonato.com/2010/01/09/shrink-it-box2d-prototype-step-2/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 23:16:30 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2187</guid>
		<description><![CDATA[In the previous step I showed you how to shrink/expand any kind of polygon.
Anyway in the original game, you can&#8217;t expand objects as much as you want, because you need mass to do it, and this adds strategy to the gameplay, because you have to shrink objects to gather the necessary mass to expand other [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.emanueleferonato.com/2010/01/05/shrink-it-box2d-prototype/">previous step</a> I showed you how to shrink/expand any kind of polygon.</p>
<p>Anyway in the <a href="http://www.triqui.com/2009/12/03/shrink-it/" target = "_blank">original game</a>, you can&#8217;t expand objects as much as you want, because you need mass to do it, and this adds strategy to the gameplay, because you have to shrink objects to gather the necessary mass to expand other ones.</p>
<p>I suppose the required amount of mass is the difference between the final and the initial mass.</p>
<p>At the same time, when we shrink an object, we&#8217;ll gather mass&#8230; probably determined by the difference between the initial and the final mass.</p>
<p>When you want to play with masses, the first thing you must setup carefully is the expand/shrink ratio. In the <a href="http://www.emanueleferonato.com/2010/01/05/shrink-it-box2d-prototype/">previous step</a> I used a 10% for both shrinking and expanding.</p>
<p>This leads to a glitchy gameplay because if I have a sphere with a radius = 100 and I shrink it by 10% I get a sphere with a radius = 90. But if I expand 90 by 10% I get a sphere with a radius = 99. I don&#8217;t get the original sphere. So I cannot use these values, because I need to get the initial object if I expand it and then shrink it or if I shrink it and then expand it.</p>
<p>So in this example I am using 20% shrinking and 25% expanding. This way, our object with radius = 100 shrinked by 20% will have a radius of 80&#8230; and a radius = 80 expanded by 25% returns 100 again. </p>
<p>It&#8217;s up to you to find a couple of compatible numbers.</p>
<p>About masses, this is the concept: when the player shrinks an object, there isn&#8217;t any problem, I just have to compare the old mass with the new one and add the difference to the available mass. To get a body&#8217;s mass, use <code>GetMass()</code>.</p>
<p>When the player tries to expand an objects, things become a little harder because we can&#8217;t know the future mass of the body, unless we want to heavily play with geometry. </p>
<p>So we have to make a little trick: first we remove the original shape, then we create and attach the new, expanded shape to the body, so we can get the mass of the final body. If we have enough mass, then we render the body, otherwise we remove the new shape and restore the old one. Since we do everything before rendering the frame, it will work nicely.</p>
<p>This is the script:<span id="more-2187"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextField</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> shrink extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_world<span style="color: #000000; font-weight: bold;">:</span>b2World;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_iterations<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">10</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_timeStep<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">1.0</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30.0</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mousePVec<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #009900;">// variable to store if the player is pressing SPACE</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> space_pressed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #009900;">// initial available mass</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> avail_mass<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">20</span>;
		<span style="color: #009900;">// text field to display available mass</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> text_field<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TextField</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">TextField</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> shrink<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> worldAABB<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> polygon<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> circleDef<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			worldAABB.lowerBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100.0</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100.0</span><span style="color: #000000;">&#41;</span>;
			worldAABB.upperBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100.0</span>, <span style="color: #000000; font-weight:bold;">100.0</span><span style="color: #000000;">&#41;</span>;
			m_world=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span>worldAABB,<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// debug draw start</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> m_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>;
			m_sprite = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>m_sprite<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgDraw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgSprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_sprite.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>dbgSprite<span style="color: #000000;">&#41;</span>;
			dbgDraw.m_sprite=m_sprite;
			dbgDraw.m_drawScale=<span style="color: #000000; font-weight:bold;">30</span>;
			dbgDraw.m_alpha=<span style="color: #000000; font-weight:bold;">1</span>;
			dbgDraw.m_fillAlpha=<span style="color: #000000; font-weight:bold;">0.5</span>;
			dbgDraw.m_lineThickness=<span style="color: #000000; font-weight:bold;">1</span>;
			dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
			m_world.SetDebugDraw<span style="color: #000000;">&#40;</span>dbgDraw<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// debug draw end</span>
			<span style="color: #009900;">// ground</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span>;
			polygon.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span>;
			polygon.density=<span style="color: #000000; font-weight:bold;">0</span>;
			polygon.friction=<span style="color: #000000; font-weight:bold;">0.3</span>;
			polygon.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>polygon<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// circle</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
			circleDef.radius=<span style="color: #000000; font-weight:bold;">2</span>;
			circleDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			circleDef.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
			circleDef.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>circleDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// box</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">13</span>, <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
			polygon.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			polygon.density=<span style="color: #000000; font-weight:bold;">1</span>;
			polygon.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
			polygon.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>polygon<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// triangle</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">13</span>,<span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span>;
			polygon.vertexCount=<span style="color: #000000; font-weight:bold;">3</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			polygon.density=<span style="color: #000000; font-weight:bold;">1</span>;
			polygon.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
			polygon.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>polygon<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// custom shape</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">8</span>,<span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span>;
			polygon.vertexCount=<span style="color: #000000; font-weight:bold;">5</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			polygon.vertices<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			polygon.density=<span style="color: #000000; font-weight:bold;">1</span>;
			polygon.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
			polygon.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>polygon<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>text_field<span style="color: #000000;">&#41;</span>;
			text_field.<span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Available mass: &quot;</span><span style="color: #000000; font-weight: bold;">+</span>avail_mass.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			text_field.<span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">330</span>;
			text_field.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">20</span>;
			text_field.<span style="color: #004993;">width</span>=<span style="color: #000000; font-weight:bold;">300</span>;
			<span style="color: #009900;">//</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, Update, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>, key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>, key_up<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, GetBodyAtMouse<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// detecting if the player pressed SPACE</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				space_pressed=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// detecting if the player released SPACE</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_up<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				space_pressed=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">//</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> GetBodyAtMouse<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>b2Body <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// scale multiplyers. Remember to choose compatible multipliers</span>
			<span style="color: #009900;">// in this case, 0.8*1.25=1 =&gt; compatible</span>
			<span style="color: #009900;">// 0.9*1.1=0.99 =&gt; not compabile. Must be 1</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> mult<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.8</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>space_pressed<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				mult=<span style="color: #000000; font-weight:bold;">1.25</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> mouseXWorldPhys = <span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> mouseYWorldPhys = <span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
			mousePVec.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> aabb<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			aabb.lowerBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
			aabb.upperBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> k_maxCount<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">10</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> shapes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=m_world.Query<span style="color: #000000;">&#40;</span>aabb,shapes,k_maxCount<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body=<span style="color: #0033ff; font-weight: bold;">null</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> count; <span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> tShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape=shapes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2Shape;
				<span style="color: #6699cc; font-weight: bold;">var</span> inside<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=tShape.TestPoint<span style="color: #000000;">&#40;</span>tShape.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetXForm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,mousePVec<span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>inside<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					body=tShape.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// if I selected a STATIC body...</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000; font-weight: bold;">&amp;&amp;!</span> body.IsStatic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// gettinc current mass</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> cur_mass<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=body.GetMass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #009900;">// variable to store new shape's mass</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> new_mass<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> s<span style="color: #000000; font-weight: bold;">:</span>b2Shape=body.GetShapeList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">type</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=s.GetType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000; font-weight: bold;">:</span>
						<span style="color: #009900;">// I know it's a circle, so I am creating a b2CircleShape variable</span>
						<span style="color: #6699cc; font-weight: bold;">var</span> circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=body.GetShapeList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2CircleShape;
						<span style="color: #009900;">// getting the radius..</span>
						<span style="color: #6699cc; font-weight: bold;">var</span> r=circle.GetRadius<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// removing the circle shape from the body</span>
						body.DestroyShape<span style="color: #000000;">&#40;</span>circle<span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// creating a new circle shape</span>
						<span style="color: #6699cc; font-weight: bold;">var</span> circleDef<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef;
						circleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// calculating new radius</span>
						circleDef.radius=r<span style="color: #000000; font-weight: bold;">*</span>mult;
						circleDef.density=<span style="color: #000000; font-weight:bold;">1.0</span>;
						circleDef.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
						circleDef.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
						<span style="color: #009900;">// attach the shape to the body</span>
						body.CreateShape<span style="color: #000000;">&#40;</span>circleDef<span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// determine new body mass</span>
						body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// determining new mass</span>
						new_mass=body.GetMass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// calculating available mass after scaling</span>
						avail_mass <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000;">&#40;</span>cur_mass<span style="color: #000000; font-weight: bold;">-</span>new_mass<span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// if there isn't enough mass...</span>
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>avail_mass<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							<span style="color: #009900;">// remove new circle shape and restore last used circle shape</span>
							avail_mass<span style="color: #000000; font-weight: bold;">+</span>=new_mass<span style="color: #000000; font-weight: bold;">-</span>cur_mass;
							circle=body.GetShapeList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2CircleShape;
							body.DestroyShape<span style="color: #000000;">&#40;</span>circle<span style="color: #000000;">&#41;</span>;
							circleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
							circleDef.radius=r;
							circleDef.density=<span style="color: #000000; font-weight:bold;">1.0</span>;
							circleDef.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
							circleDef.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
							body.CreateShape<span style="color: #000000;">&#40;</span>circleDef<span style="color: #000000;">&#41;</span>;
							body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #000000;">&#125;</span>
						<span style="color: #009900;">// displaying mass</span>
						text_field.<span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Available mass: &quot;</span><span style="color: #000000; font-weight: bold;">+</span>avail_mass.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000; font-weight: bold;">:</span>
						<span style="color: #009900;">// now I know it's a polygon</span>
						<span style="color: #6699cc; font-weight: bold;">var</span> poly<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape=body.GetShapeList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2PolygonShape;
						<span style="color: #009900;">// UNIVERSAL POLYGON SCALING ROUTINE THANX TO ILYA</span>
						<span style="color: #6699cc; font-weight: bold;">var</span> vertex_num<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=poly.GetVertexCount<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #6699cc; font-weight: bold;">var</span> vertex_array<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>=poly.GetVertices<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> vert<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 <span style="color: #0033ff; font-weight: bold;">in</span> vertex_array<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							vert.Multiply<span style="color: #000000;">&#40;</span>mult<span style="color: #000000;">&#41;</span>;
						<span style="color: #000000;">&#125;</span>
						body.DestroyShape<span style="color: #000000;">&#40;</span>poly<span style="color: #000000;">&#41;</span>;
						<span style="color: #6699cc; font-weight: bold;">var</span> new_shape<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						new_shape.vertexCount=vertex_num;
						new_shape.vertices=vertex_array;
						new_shape.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
						new_shape.density=<span style="color: #000000; font-weight:bold;">1</span>;
						new_shape.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
						body.CreateShape<span style="color: #000000;">&#40;</span>new_shape<span style="color: #000000;">&#41;</span>;
						body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// determining new mass</span>
						new_mass=body.GetMass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #009900;">// calculating available mass after scaling</span>
						avail_mass<span style="color: #000000; font-weight: bold;">+</span>=cur_mass<span style="color: #000000; font-weight: bold;">-</span>new_mass;
						<span style="color: #009900;">// if there isn't enough mass...</span>
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>avail_mass<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							<span style="color: #009900;">// remove new polygon shape and restore last used polygon shape</span>
							avail_mass<span style="color: #000000; font-weight: bold;">+</span>=new_mass<span style="color: #000000; font-weight: bold;">-</span>cur_mass;
							poly=body.GetShapeList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2PolygonShape;
							body.DestroyShape<span style="color: #000000;">&#40;</span>poly<span style="color: #000000;">&#41;</span>;
							<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span>vert <span style="color: #0033ff; font-weight: bold;">in</span> vertex_array<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
								vert.Multiply<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span>mult<span style="color: #000000;">&#41;</span>;
							<span style="color: #000000;">&#125;</span>
							new_shape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
							new_shape.vertexCount=vertex_num;
							new_shape.vertices=vertex_array;
							new_shape.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
							new_shape.density=<span style="color: #000000; font-weight:bold;">1</span>;
							new_shape.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
							body.CreateShape<span style="color: #000000;">&#40;</span>new_shape<span style="color: #000000;">&#41;</span>;
							body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #000000;">&#125;</span>
						<span style="color: #009900;">// displaying mass</span>
						text_field.<span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Available mass: &quot;</span><span style="color: #000000; font-weight: bold;">+</span>avail_mass.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> body;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			m_world.Step<span style="color: #000000;">&#40;</span>m_timeStep, m_iterations<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And this is the result:</p>
<p><embed src="/wp-content/uploads/2010/01/shrink2.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Click on a body to shrink it, click + SPACE to enlarge it, and look at your mass meter in the lower left corner.</p>
<p>No need to download, simply copy/paste this code in the file you can find in the <a href="http://www.emanueleferonato.com/2010/01/05/shrink-it-box2d-prototype/">previous step</a>.</p>
<p>Next time, we&#8217;ll add the final gameplay.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/01/09/shrink-it-box2d-prototype-step-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Way of an Idea Box2D prototype &#8211; Step 2</title>
		<link>http://www.emanueleferonato.com/2010/01/07/way-of-an-idea-box2d-prototype-step-2/</link>
		<comments>http://www.emanueleferonato.com/2010/01/07/way-of-an-idea-box2d-prototype-step-2/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 15:17:11 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2179</guid>
		<description><![CDATA[It&#8217;s time to apply the concepts seen at Pausing a Box2D simulation with Way of an Idea Box2D prototype to create a paused Box2D simulation that will start only when you&#8217;ll press SPACE, and meanwhile you can draw paths, just like in the original game.
The only problem with this technique is you can&#8217;t use the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time to apply the concepts seen at <a href="http://www.emanueleferonato.com/2010/01/06/pausing-a-box2d-simulation/">Pausing a Box2D simulation</a> with <a href="http://www.emanueleferonato.com/2009/12/29/way-of-an-idea-box2d-prototype/">Way of an Idea Box2D prototype</a> to create a paused Box2D simulation that will start only when you&#8217;ll press SPACE, and meanwhile you can draw paths, just like in the <a href="http://www.triqui.com/2009/12/22/way-of-an-idea/" target = "_blank">original game</a>.</p>
<p>The only problem with this technique is you can&#8217;t use the debug draw to test your projects, because debug draw draws only after <code>Step</code> function has been executed, and since the game starts with the simulation paused, you can&#8217;t see the Box2D boxes you are drawing (and you can&#8217;t even see other assets too) until you start the simulation.</p>
<p>So the trick is: placing the Box2D boxes, the ones debug draw won&#8217;t draw, and attaching the movieclips to them. The movieclips will be rendered because I am adding them to stage, and they will remain in position until the simulation starts.</p>
<p>So the script becomes:<span id="more-2179"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> <span style="color: #004993;">draw</span> extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> drawing<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> canvas<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> points_array<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> pixel_dist<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">20</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> saved_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> saved_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_world<span style="color: #000000; font-weight: bold;">:</span>b2World;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> circleDef<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #009900;">// radians to degrees conversion</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rad_to_deg<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">57.2957795</span>;
		<span style="color: #009900;">// simulation timestep. We start at zero.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> timestep<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// world creation</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> worldAABB<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			worldAABB.lowerBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100.0</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">100.0</span><span style="color: #000000;">&#41;</span>;
			worldAABB.upperBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100.0</span>, <span style="color: #000000; font-weight:bold;">100.0</span><span style="color: #000000;">&#41;</span>;
			m_world=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span>worldAABB,<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//</span>
			<span style="color: #009900;">// *** IMPORTANT: NOTICE THERE ISN'T ANY DEBUG DRAW ROUTINE ***</span>
			<span style="color: #009900;">// </span>
			<span style="color: #009900;">// placing the ball</span>
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
			circleDef.radius=<span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
			circleDef.density=<span style="color: #000000; font-weight:bold;">1</span>;
			circleDef.friction=<span style="color: #000000; font-weight:bold;">0.5</span>;
			circleDef.restitution=<span style="color: #000000; font-weight:bold;">0.2</span>;
			<span style="color: #009900;">// ball's movieclip attachment</span>
			bodyDef.userData = <span style="color: #0033ff; font-weight: bold;">new</span> sphere<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.userData.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">20</span>;
			bodyDef.userData.<span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">20</span>;
			bodyDef.userData.<span style="color: #004993;">width</span>=<span style="color: #000000; font-weight:bold;">30</span>;
			bodyDef.userData.<span style="color: #004993;">height</span>=<span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>bodyDef.userData<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// end ball's movieclip attachment</span>
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>circleDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>canvas<span style="color: #000000;">&#41;</span>;
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>,mouse_pressed<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_MOVE</span>,mouse_moved<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>,mouse_released<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, Update, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>, key_down<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_pressed<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			drawing=<span style="color: #0033ff; font-weight: bold;">true</span>;
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span>,<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span>;
			points_array=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			saved_x=<span style="color: #004993;">mouseX</span>;
			saved_y=<span style="color: #004993;">mouseY</span>;
			points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_x<span style="color: #000000;">&#41;</span>;
			points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_y<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_moved<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>drawing<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> dist_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=mouseX<span style="color: #000000; font-weight: bold;">-</span>saved_x;
				<span style="color: #6699cc; font-weight: bold;">var</span> dist_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=mouseY<span style="color: #000000; font-weight: bold;">-</span>saved_y;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>dist_x<span style="color: #000000; font-weight: bold;">*</span>dist_x<span style="color: #000000; font-weight: bold;">+</span>dist_y<span style="color: #000000; font-weight: bold;">*</span>dist_y<span style="color: #000000; font-weight: bold;">&gt;</span>pixel_dist<span style="color: #000000; font-weight: bold;">*</span>pixel_dist<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span>,<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span>;
					saved_x=<span style="color: #004993;">mouseX</span>;
					saved_y=<span style="color: #004993;">mouseY</span>;
					points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_x<span style="color: #000000;">&#41;</span>;
					points_array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>saved_y<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_released<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			drawing=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> sx<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ex<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> sy<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ey<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dist_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dist_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dist<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> segments<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=points_array.<span style="color: #004993;">length</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>segments; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				sx=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>;
				sy=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>;
				ex=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>;
				ey=points_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span>;
				dist_x=sx<span style="color: #000000; font-weight: bold;">-</span>ex;
				dist_y=sy<span style="color: #000000; font-weight: bold;">-</span>ey;
				dist=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sqrt</span><span style="color: #000000;">&#40;</span>dist_x<span style="color: #000000; font-weight: bold;">*</span>dist_x<span style="color: #000000; font-weight: bold;">+</span>dist_y<span style="color: #000000; font-weight: bold;">*</span>dist_y<span style="color: #000000;">&#41;</span>;
				<span style="color: #004993;">angle</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>dist_y,dist_x<span style="color: #000000;">&#41;</span>;
				create_box<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>sx<span style="color: #000000; font-weight: bold;">+</span>ex<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span>,<span style="color: #000000;">&#40;</span>sy<span style="color: #000000; font-weight: bold;">+</span>ey<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>dist<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			canvas.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to be called if the player presses space</span>
		<span style="color: #009900;">// switching timestep</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>timestep<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					timestep=<span style="color: #000000; font-weight:bold;">0</span>;
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
					timestep=<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> create_box<span style="color: #000000;">&#40;</span>px,py,w,h,a<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
			<span style="color: #6699cc; font-weight: bold;">var</span> bodyDef<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef;
			<span style="color: #6699cc; font-weight: bold;">var</span> boxDef<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef;
			bodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span>;
			bodyDef.<span style="color: #004993;">angle</span>=a;
			boxDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			boxDef.SetAsBox<span style="color: #000000;">&#40;</span>w,h<span style="color: #000000;">&#41;</span>;
			boxDef.friction=<span style="color: #000000; font-weight:bold;">0.3</span>;
			boxDef.density=<span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #009900;">// box's movieclip attachment</span>
			bodyDef.userData = <span style="color: #0033ff; font-weight: bold;">new</span> chalk<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bodyDef.userData.<span style="color: #004993;">x</span>=px<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
			bodyDef.userData.<span style="color: #004993;">y</span>=py<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
			bodyDef.userData.<span style="color: #004993;">width</span>=w<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">60</span>;
			bodyDef.userData.<span style="color: #004993;">height</span>=h<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">60</span>;
			bodyDef.userData.<span style="color: #004993;">rotation</span>=a<span style="color: #000000; font-weight: bold;">*</span>rad_to_deg;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>bodyDef.userData<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// end box's movieclip attachment</span>
			body=m_world.CreateBody<span style="color: #000000;">&#40;</span>bodyDef<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>boxDef<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// executing Step function and updating movieclips positions only </span>
			<span style="color: #009900;">// if timestep != 0</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>timestep<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				m_world.Step<span style="color: #000000;">&#40;</span>timestep, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> bb<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_world.m_bodyList; bb; bb = bb.m_next<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>bb.m_userData <span style="color: #0033ff; font-weight: bold;">is</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						bb.m_userData.<span style="color: #004993;">x</span>=bb.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						bb.m_userData.<span style="color: #004993;">y</span>=bb.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						bb.m_userData.<span style="color: #004993;">rotation</span> = bb.GetAngle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>and this is the result:</p>
<p><embed src="/wp-content/uploads/2010/01/draw4.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>draw with the mouse and press SPACE to play/pause the simulation.</p>
<p><a href="/wp-content/uploads/2010/01/draw4.zip">Download the source code</a>.</p>
<p>Next step, chalk management and goal.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/01/07/way-of-an-idea-box2d-prototype-step-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
