Categories
Custom Design Grumm Urban papercraft

NoissGrumm – part 2

Since my post about NoissGrumm I’ve modified the legs and arms a little bit 🙂

This is a short explanation about this model:

I’m working for a company named NOISE (http://www.makesomenoise.nl) and I wanted to make a Grumm to “explain” what I’m doing in my spare time.

Now I worked on the legs, feet, arms and hands.
I’m not happy with the hand yet, but that will improve.

NoissGrumm front wip2
NoissGrumm front wip2

NoissGrumm back wip2
NoissGrumm back wip2
Categories
Design Urban papercraft

Shin Tanaka x Giant Robot x Scion

When I started to write about this Griant Robot there was not a site for it; now there is: Gaint Robot Shin.
This website is for a contest: “Customize the template with your own designs”.
But before you start designing: you need to read the rules (.pdf)…. But to make a long story short: basically you need to live in the USA, which I don’t do)

But the best stuff is yet to come: the template that are in the magazine can be downloaded form there:
part 1, part 2, part 3 and the blank template.

Read more about this contest at NPT

I’ve been asked to talk about this subject…… yeah this title is ohhh soooo original.

I though about it for a while (do I really want to make free advertising for a car company or a magazine?), but I couldn’t resist: Shin Tanaka is involved.
And that dude is cool!!

Shin is featured in an Asian/Amercan popculture magazine Giant Robot with a 3 piece robot: this image is the second one that can be found in Issue 57 of Giant Robot magazine.
Second robot by Shin Tanaka

Buy magazine 56, 57, 58 and you will have this:
Three robots combined by Shin Tanaka
How cool is that!

I’ve been looking around in the stores in Amsterdam, but I haven’t found this magazine (yet). So I can’t build it 🙁 and those who can’t build, write about it…. 😀

The same thing about Scion, this brand isn’t sold in the Netherlands (perhaps it is, but I don’t know about it, and I have never seen one).

But if Scion is happy with this post, and want to reward me, just give me a xB (no, really!!)

Categories
AS3 AS3 migration Flash

From AS2 to AS3 – Where did it go – random

This is one that I figured out pretty quick.

What has the ActionScript 2.0 Migration to say about this subject:

ActionScript 2.0  ActionScript 3.0  Comments
random() Math.random() Removed. Use Math.random() instead.

I could have seen that coming:

Deprecated since Flash Player 5. This function was deprecated in favor of Math.random().
Returns a random integer between 0 and one less than the integer specified in the value parameter.

Source: as2 help documentation.

I was still using it a in Flash 6 and higher, but now I have to change that habit.

And now we come to an annoying point: if you go to the as3 help documentation you only get an explanation but not a nice piece of example code. I don’t know why they changed that, but I don’t like it.

So this is the explanation from the as3 help for Math.random()

Returns a pseudo-random number n, where 0 <= n < 1. The number returned is calculated in an undisclosed manner, and pseudo-random because the calculation inevitably contains some element of non-randomness.

with some example code from as2 (functionality isn’t changed)

[as]
// The following example outputs 100 random integers between 4 and 11 (inclusively):
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max – min + 1)) + min;
return randomNum;
}
for (var i = 0; i < 100; i++) { var n:Number = randRange(4, 11) trace(n); } [/as] the way I using it was somewhat simpler then the example

AS 2

There are a couple of ways to do that in ActionScript 2:

Example #1
[as]random(10)+1; // to get a number between 1 and 10 (1 and 10 included)[/as]

AS 3

Lets try this in ActionScript 3:

Example #1
[as]Math.floor(Math.random()*10)+1; // to get a number between 1 and 10 (1 and 10 included)[/as]

Just make a snippet out of it, and you have never to search for this answer…

Categories
AS3 Flash

Hello box2D – part 4

In my previous post I update the “Hello world” for box2D so that you can see something visual interesting.
You can find the original here: AS3: Hello Box2D.

Today I will post the same code but now how it should be.
Keeping the coding conventions in mind and making it easier to reuse code.

Still keeping in mind the original Hello world

[as]
package {

import flash.display.*;
import flash.events.*;
// Box2D Classes used in this “Hello world”
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;

//[SWF(width=”640″, height=”480″, backgroundColor=”#000000″, frameRate=”60″)]
public class HelloWorld extends MovieClip {

public var world:b2World;
public var iterations:int = 10;
public var timeStep:Number = 1.0 / 60.0;
public var worldScale:Number = 30.0;

// to see what you have created
private var debugDraw:b2DebugDraw;
private var isDebugDrawing:Boolean = true; // change to false and there is no debugDraw

// constructor
public function HelloWorld() {
//trace( “HelloWorld.HelloWorld” );
init();
}

/**
* lets start building our “Hello world” box2D program
*/
private function init():void {
trace( “HelloWorld.init” );

createWorld();
creatingGroundBox();
creatingDynamicBody();

setupDebugDraw();

// Add event for main loop
addEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);
}

/**
* http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_World
*/
private function createWorld():void {
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);

// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2 (0.0, -10.0);

// Allow bodies to sleep
var doSleep:Boolean = true;

// Construct a world object
world = new b2World(worldAABB, gravity, doSleep);
}

/**
* http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Ground_Box
*/
private function creatingGroundBox():void {
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0.0, -9.0);

var groundBody:b2Body = world.CreateBody(groundBodyDef);

var groundShapeDef:b2PolygonDef = new b2PolygonDef();
groundShapeDef.SetAsBox(50.0, 10.0);

groundBody.CreateShape(groundShapeDef);
}

/**
* http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Dynamic_Body
*/
private function creatingDynamicBody ():void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(10.0, 10.0);
var body:b2Body = world.CreateBody(bodyDef);

var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
shapeDef.restitution = 0.8;
body.CreateShape(shapeDef);
body.SetMassFromShapes();

}

// extra to visualize
private function setupDebugDraw():void {
if(isDebugDrawing) {
debugDraw = new b2DebugDraw();
debugDraw.m_sprite = new Sprite();

addChild(debugDraw.m_sprite);

debugDraw.m_drawScale = 30;
debugDraw.m_fillAlpha = .25;
debugDraw.m_lineThickness = 1;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;
world.SetDebugDraw (debugDraw);
}
}

/**
* http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
*/
public function onUpdateHandler(e:Event):void{
world.Step(timeStep, iterations);
}

} // end class

} // end package
[/as]

So all you need to do is CTRL+ENTER.

And you will have something like this:

[swf]http://www.matthijskamstra.nl/blog/wp-content/uploads/helloworldbox2d_nice.swf,520, 390[/swf]
Categories
Custom Design Drukk

Drukks: the Pusher Series 1 – Dr. Bear

I’m one step closer filling the 6 slots for Pusher Series 1 (Designers on Drukks).

Scott Schaller is filling the 5th slot with his Dr. Bear
Dr. Bear by Scott Schaller

An awesome design. And I love the extras Scott designed for Dr. Bear, especial the brain case.. Always wanted a evil Doctor in my papertoys collection.

Of course is Scott and Nice paper toys member, you almost have to be 😉 …

Some other nice-toknow information: Scott is a Graphic Design Teacher, which isn’t that special: a lot of papertoy designers have a creative job or education. No, besides teaching Graphic Design, Scott is also in charge of an after school program: F studio.

As a design teacher, I often throw in a lot of hand skills oriented components to the projects I assign. The computer plays a big part in all of our projects, but the finishing of a project, whether it is binding a box or constructing a packaging comp, is the part that makes the project complete. Paper toy crafting just seems to be a natural next step, combining design and illustration, hand skills, and toy making.
….
we are making our own vinyl toys, including the design, sculpting, and casting of resign models

How cool it that!!! I’m trying to figure this stuff out in my spare time and here is a teacher who can teach you everything you ever wanted to know. Man I’m jealous.
Visit Cabrillo Design Lab for more info.

Check this out: Custom self portraits by students of Scott (and himself) based upon boxy from Shin Tanaka.
Boxy student self portraits
Scott himself
Scott Schaller

And way down here on the end of the post… Scott told me that one of his students is working on a Drukk and she wants to have the last slot in series 1. Please do, you can have the last slot. And when you have filled that last spot I will start on the Selfish Series 2. 🙂
Categories
AS3 Flash

Hello box2D – part 3

In my previous post I update the “Hello world” for box2D so that you can see something visual happening.
You can find the original here: AS3: Hello Box2D.

Today I will make it a little bit more attractive (remove trace).

First we change some settings to make it more fun to watch at!

The groundBodeDef (strange name for something that is the ceiling) is moved more down so you can see where the bodyDef is bouncing off.
[as]
//groundBodyDef.position.Set(0.0, -10.0); // [mck]: old code
groundBodyDef.position.Set(0.0, -9.0);
[/as]

The bodyDef is moved to the center and dropped a little higher
[as]
//bodyDef.position.Set(0.0, 4.0); // [mck]: old code
bodyDef.position.Set(10.0, 10.0);
[/as]

And the shapeDef is bouncy so that you can see what box2D can do:
[as]
shapeDef.restitution = 0.8; // [mck]: extra new code
[/as]

And delete the trace:
[as]
var position:b2Vec2 = body.GetPosition();
var angle:Number = body.GetAngle();
trace(position.x +’,’+ position.y +’,’+ angle);
[/as]

End result

The document class (HelloWorld.as) will look like this:

[as]
package {

import flash.display.*;
import flash.events.*;
// Box2D Classes used in this “Hello world”
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;

// [SWF(width=”640″, height=”480″, backgroundColor=”#000000″, frameRate=”60″)]
public class HelloWorld extends Sprite {

private var body:b2Body;
private var world:b2World;

// constructor
public function HelloWorld() {
trace( “HelloWorld.HelloWorld” );

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_World
////////////////////////////////////////////////////////////////////////////////////////////////////
// Create world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);

// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2 (0.0, -10.0);

// Allow bodies to sleep
var doSleep:Boolean = true;

// Construct a world object
// var world:b2World = new b2World(worldAABB, gravity, doSleep); // [mck]: old code
world = new b2World(worldAABB, gravity, doSleep);

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Ground_Box
////////////////////////////////////////////////////////////////////////////////////////////////////
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0.0, -10.0);

var groundBody:b2Body = world.CreateBody(groundBodyDef);

var groundShapeDef:b2PolygonDef = new b2PolygonDef();
groundShapeDef.SetAsBox(50.0, 10.0);

groundBody.CreateShape(groundShapeDef);

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Dynamic_Body
////////////////////////////////////////////////////////////////////////////////////////////////////
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(0.0, 4.0);
// var body:b2Body = world.CreateBody(bodyDef); // [mck]: old code
body = world.CreateBody(bodyDef);

var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
body.CreateShape(shapeDef);
body.SetMassFromShapes();

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Debug_Drawing
////////////////////////////////////////////////////////////////////////////////////////////////////
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = new Sprite();

addChild(debugDraw.m_sprite);

debugDraw.m_drawScale = 30;
debugDraw.m_fillAlpha = .25;
debugDraw.m_lineThickness = 1;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;
world.SetDebugDraw (debugDraw);

//
addEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
////////////////////////////////////////////////////////////////////////////////////////////////////
public function onUpdateHandler(e:Event):void{

var timeStep:Number = 1.0 / 60.0;
var iterations:Number = 10;

world.Step(timeStep, iterations);

}

} // end class

} // end package

[/as]

So all you need to do is CTRL+ENTER.

And you will have something like this:

[swf]http://www.matthijskamstra.nl/blog/wp-content/uploads/helloworldbox2d_nice.swf,520, 390[/swf]

Is this it? No, lets make it better.
Tomorrow more

Categories
AS3 Flash

Hello box2D – part 2

In my previous post I started with a “Hello world” for box2DFlash.
You can find the original here: AS3: Hello Box2D.

When you get it working you will not be satisfied: a black swf, with only a trace in the output panel.

Today I will make the “Hello world” more visible.

Debug_Drawing

This the preferred method of drawing these physics entities for debugging, rather than accessing the data directly. The reason is that much of the necessary data is internal and subject to change.

source: Debug_Drawing

Box2D has a debugDraw class that lets you see what you have built, they call it DebugDraw.
Take the code from yesterday and place this code before Simulating_the_World code.
[as]
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = new Sprite();

addChild(debugDraw.m_sprite);

debugDraw.m_drawScale = 30;
debugDraw.m_fillAlpha = .25;
debugDraw.m_lineThickness = 1;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;
world.SetDebugDraw (debugDraw);
[/as]

Thx Boy Wonder for pointing me to a little mistake in the part of the code: I forgot the last piece of code world.SetDebugDraw (debugDraw);. The class at the bottom of the page was correct!

So all you need to do is CTRL+ENTER.

Nice effect isn’t it? Almost? Well at least you see something although it’s not impressive…
Yeah, it’s isn’t much. Lets see how we can improve this.

Improve Debug_Drawing

The problem is that the debug window is created and executed 60 times, but because it is done in a for loop, you only see the end result (after 60 updates).
To see animation (if you follow the trace you see that something is moving) we need to update (visible) the data in dbgDraw more and we will use the frame rate to update it (yesterday set to 60 frames).
But for that there needs to be changed more than just a couple of lines

First we need to add some variables:
[as]
private var world:b2World;
private var body:b2Body;
[/as]
and change some related code:
[as]
// Construct a world object
// var world:b2World = new b2World(worldAABB, gravity, doSleep); // [mck]: old code
world = new b2World(worldAABB, gravity, doSleep);
[/as]
and
[as]
// var body:b2Body = world.CreateBody(bodyDef); // [mck]: old code
body = world.CreateBody(bodyDef);
[/as]

and remove the code add at Simulating_the_World with
[as]
addEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);
[/as]

and add
[as]
////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
////////////////////////////////////////////////////////////////////////////////////////////////////
public function onUpdateHandler(e:Event):void{

var timeStep:Number = 1.0 / 60.0;
var iterations:Number = 10;

world.Step(timeStep, iterations);

var position:b2Vec2 = body.GetPosition();
var angle:Number = body.GetAngle();
trace(position.x +’,’+ position.y +’,’+ angle);
}
[/as]

End result

The document class (HelloWorld.as) will look like this:

[as]
package {

import flash.display.*;
import flash.events.*;
// Box2D Classes used in this "Hello world"
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;

// [SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
public class HelloWorld3 extends Sprite {

private var body:b2Body;
private var world:b2World;

// constructor
public function HelloWorld() {
trace( "HelloWorld.HelloWorld" );

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_World
////////////////////////////////////////////////////////////////////////////////////////////////////
// Create world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);

// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2 (0.0, -10.0);

// Allow bodies to sleep
var doSleep:Boolean = true;

// Construct a world object
// var world:b2World = new b2World(worldAABB, gravity, doSleep); // [mck]: old code
world = new b2World(worldAABB, gravity, doSleep);

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Ground_Box
////////////////////////////////////////////////////////////////////////////////////////////////////
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0.0, -10.0);

var groundBody:b2Body = world.CreateBody(groundBodyDef);

var groundShapeDef:b2PolygonDef = new b2PolygonDef();
groundShapeDef.SetAsBox(50.0, 10.0);

groundBody.CreateShape(groundShapeDef);

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Dynamic_Body
////////////////////////////////////////////////////////////////////////////////////////////////////
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(0.0, 4.0);
// var body:b2Body = world.CreateBody(bodyDef); // [mck]: old code
body = world.CreateBody(bodyDef);

var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
body.CreateShape(shapeDef);
body.SetMassFromShapes();

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Debug_Drawing
////////////////////////////////////////////////////////////////////////////////////////////////////
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = new Sprite();

addChild(debugDraw.m_sprite);

debugDraw.m_drawScale = 30;
debugDraw.m_fillAlpha = .25;
debugDraw.m_lineThickness = 1;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;
world.SetDebugDraw (debugDraw);

//
addEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
////////////////////////////////////////////////////////////////////////////////////////////////////
public function onUpdateHandler(e:Event):void{

var timeStep:Number = 1.0 / 60.0;
var iterations:Number = 10;

world.Step(timeStep, iterations);

var position:b2Vec2 = body.GetPosition();
var angle:Number = body.GetAngle();
trace(position.x +’,’+ position.y +’,’+ angle);
}

} // end class

} // end package
[/as]

So all you need to do is CTRL+ENTER.

Now we see something moving, and we have an endless stream of trace data.

It’s better, but there is more room for improvement…
tomorrow more.

Categories
AS3 Flash

Hello box2D – part 1

In programming the Hello world is probably the first you will ever make in a new language.
In the box2D wiki you can find a “Hello world” in box2DFlash style: AS3: Hello Box2D.

Easy as it should be … or is it?
This is not the “Hello world” I expected, so I’ll post one that helps you more on your way.

As irongleet mentions in the comments, this is not really a tutorial. There is already a tutorial on the page I mention above AS3: Hello Box2D. It’s not a bad tutorial, but you need to have some knowledge of AS3 to follow it and I personally don’t like the end result. So this is the first part of a series which will help you make more out of the previously mentioned tutorial. But I’m not going to copy past the tutorial written somewhere else and present it here. That’s why you should read the links to the source for more information. And the next Hello box2d – part 2 will have some more input from me.

Hello_Box2D

source: Hello_Box2D

Lets start with a Flash document width 640px, height 480px, backgroundColor=”#000000″ and a framerate 60.
And add a document class: HelloWorld.
Save this .fla (for example HelloWorldBox2d.fla).

Now get your favorite code-editor (I suggest FlashDevelop) and create that document class, name it HelloWorld.as:
[as]
package {

import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;

public class HelloWorld extends Sprite{

// constructor
public function HelloWorld() {
trace( “HelloWorld.HelloWorld” );
}

} // end class

} // end package
[/as]
and save it next to the HelloWorldBox2d.fla.

You can see that I already add the import needed for Box2D.

The last thing you need to do is download the box2D classes: http://sourceforge.net/projects/box2dflash
Extract the zip file and copy the folder box2d next to the HelloWorldBox2d.fla and the HelloWorld.as

And now you are set…

Creating_a_World

Every Box2D program begins with the creation of a world object. This is the physics hub that manages memory, objects, and simulation.

source: Creating_a_World

The next piece of code can be placed in the constructor (public function HelloWorld() )
[as]
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);

// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2 (0.0, -10.0);

// Allow bodies to sleep
var doSleep:Boolean = true;

// Construct a world object
var world:b2World = new b2World(worldAABB, gravity, doSleep);
[/as]

Creating_a_Ground_Box

source: Creating_a_Ground_Box

[as]
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0.0, -10.0);

var groundBody:b2Body = world.CreateBody(groundBodyDef);

var groundShapeDef:b2PolygonDef = new b2PolygonDef();
groundShapeDef.SetAsBox(50.0, 10.0);

groundBody.CreateShape(groundShapeDef);
[/as]

Creating_a_Dynamic_Body

We can use the same technique to create a dynamic body. The main difference, besides dimensions, is that we must establish the dynamic body’s mass properties.

source: Creating_a_Dynamic_Body

[as]
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(0.0, 4.0);
var body:b2Body = world.CreateBody(bodyDef);

var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
body.CreateShape(shapeDef);
body.SetMassFromShapes();
[/as]

Simulating_the_World

So we have initialized the ground box and a dynamic box. Now we are ready to set Newton loose to do his thing. We just have a couple more issues to consider.

source: Simulating_the_World
[as]
var timeStep:Number = 1.0 / 60.0;
var iterations:Number = 10;

for (var i:Number = 0; i < 60; ++i) { world.Step(timeStep, iterations); var position:b2Vec2 = body.GetPosition(); var angle:Number = body.GetAngle(); trace(position.x +','+ position.y +','+ angle); } [/as]

End result

The document class (HelloWorld.as) will look like this:

I didn’t wrote the code for this tutorial, so this is collection of code copy/paste from the original AS3: Hello Box2D tutorial. But I did, as many probably did, the original tutorial and I was a little disappointed at the end result… so my addition to the original tutorial can be read in the next part of this series (Hello box2d – part 2)

[as]
package {

import flash.display.Sprite;
// Box2D Classes used in this “Hello world”
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;

public class HelloWorld2 extends Sprite {

// constructor
public function HelloWorld() {
trace( “HelloWorld.HelloWorld” );

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_World
////////////////////////////////////////////////////////////////////////////////////////////////////
// Create world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);

// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2 (0.0, -10.0);

// Allow bodies to sleep
var doSleep:Boolean = true;

// Construct a world object
var world:b2World = new b2World(worldAABB, gravity, doSleep);

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Ground_Box
////////////////////////////////////////////////////////////////////////////////////////////////////
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0.0, -10.0);

var groundBody:b2Body = world.CreateBody(groundBodyDef);

var groundShapeDef:b2PolygonDef = new b2PolygonDef();
groundShapeDef.SetAsBox(50.0, 10.0);

groundBody.CreateShape(groundShapeDef);

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Dynamic_Body
////////////////////////////////////////////////////////////////////////////////////////////////////
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(0.0, 4.0);
var body:b2Body = world.CreateBody(bodyDef);

var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
body.CreateShape(shapeDef);
body.SetMassFromShapes();

////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
////////////////////////////////////////////////////////////////////////////////////////////////////
var timeStep:Number = 1.0 / 60.0;
var iterations:Number = 10;

for (var i:Number = 0; i < 60; ++i) { world.Step(timeStep, iterations); var position:b2Vec2 = body.GetPosition(); var angle:Number = body.GetAngle(); trace(position.x +','+ position.y +','+ angle); } } } // end class } // end package [/as] So all you need to do is CTRL+ENTER. Nice effect isn't it? No? Only some numbers in the output panel? Yeah I know that was not very useful... So next post will be a better/improved version of the Hello world box2D style >> read Hello box2d – part 2

Categories
AS3 Flash

Box2DFlashAS3 Refactored

If you are a Flex/Flash developer (and not familiar with another programming language), you will agree that the syntax for Box2DFlash is not following the AS3 coding conventions and seems funny.

Not that I’m so good at it (seem to have a lot of problem shaking the suffix “_mc”) but other people have the same problem: read this, this and this.

And one of them refactored the code, and not just someone: it done by John Lindquist (box2D user-name is pv3d).
John is one of the “Committer team” (?) of papervision3D and writes tutorials for papervision3D on pv3d.org.

You can find the code at http://code.google.com/p/box2dflash/ and John started working on tutorials (http://box2dflash.org/) but stopped after completing one… 🙁

[swf]http://box2dflash.org/sites/default/files/flash/RefactoredBox2dSpeedTest.swf, 520, 390[/swf]

I’ve took a quick look in the examples made by John, and I must confess that it seems more familiar/easier.
Conventions are you friend…

But what to do? Use the box2D port to AS3 from C++ or use the refactored one.
I’m using the original Box2DFlash AS3 2.0.1, not only for this reason (shaktool is one of two programmers responsible for the C++ to AS3 conversion and maintenance) but a lot of tutorials are based upon the original.

But I can’t wait for Box2DFlash AS3 2.0.2 and see what skatehead, shaktool and borisTheBrave have decided (I’m secretly cheering for John’s version!)

Categories
AS3 Flash

Box2DFlashAS3 documentation

In a previous post I started my journey into Flash physics by using Box2D.
And I “complained” about the documentation.

So I decided to create one for the boys & girls at the box2d Flash AS3 forum using ASDoc…..

Someone beat me to the punch: BorisTheBrave created a script to convert the current comment into ASDoc style comments and the result is posted here:
The unofficial Box2DFlashAS3 Documentation.

BorisTheBrave has made an update to the documentation: read more about that. I have really no idea what he did, I downloaded the files, but just over write existing files… why?
Anyway the Box2dAS3 documentation looks very nice, and that is the most important stuff he did!!