Box2D is a lot of fun: you can build cool physics-based games with very little coding. Popular games like Angry Birds were built with it. If you haven’t yet seen the awesome Seb Lee-Delisle’s live coding example of building an Angry Bird clone in 45 minutes with the Corona SDK for iOS and Android, do it now! You’ll be inspired to build your own Angry Birds clone, like I was.
So I set about copying Seb’s clone game, “Ghosts and Monsters”, except I wanted to do in Javascript, using the Box2dWeb JS port of Box2D. However, there are not a lot of options in Javascript for integrating rendering with Box2D. I did find two:
- ImpactJS, a proprietary, closed-source Javascript game framework for building HTML5 games. While ImpactJS has become quite popular in gaming, the closed-source nature and game license turned me off
- boxbox, which I haven’t tried yet. It is an open-source JS project with a simplified API that wraps both Box2D and rendering. It’s not clear whether it exposes you to the full methods and attributes of the Box2DWeb library.
Neither of these appealed to me. I ended up rolling my own integration with EaselJS. I chose EaselJS over Processing.js (which I’ve used in the past), because of its built-in functionality for mapping events (like clicking and dragging) to the shapes. So, I rolled my own EaselJS-Box2dWeb wrapper, stole the “Ghosts and Monsters” artwork (thanks, Seb!), and got something quick and dirty working in relatively short time.
There are a couple of gotchas with integrating a graphics library and Box2Dweb. For example:
- Box2D co-ordinates are all expressed in meters, while EaselJS are expressed in pixels. Have fun with all the conversions!
- Box2D object co-ordinates represent the object’s center of mass, while EaselJS co-ordinates represent the upper-left most position of the object. This is a bit of a nuisance, and don’t forget to set a EaselJS’s Bitmap’s regX and regY properties so the object rotates around its center.
Check out a working demo here, and grab the source code here on Github.
Oh, and yay for CoffeeScript!
This is really great, Jeff. I may steal some of this to work on a game with Trey.
Please do! Also, you may be interested in this meetup tomorrow: HTML5 games + Box2d: http://www.meetup.com/Boston-HTML5-Game-Development/events/44904852/
hi!
this is so amazing..
I’m working on something like that using box2dweb and easeljs.
I was wondering could u plz help me, how can i move my canvas background so it will follow where ball is hitting like angrybird.
Yes, it’s definitely possible. I’ll see if I can knock together a quick demo to show you how.
So here is that change: https://gist.github.com/2155060
Essentially, you can pan the whole scene left or right by modifying @world.easelStage.x in the tick() method. All you need to do is track how much the ball is moving, and pan the scene the corresponding amount.
Boo-yahh.. You are awesome..
It worked. I managed to integrate this in my project. I will post my project link when I will finish it.
thnks a bunch. 🙂
Boo-yahh.. You are awesome..
It worked. I managed to integrate this in my project. I will post my project link when I will finish it.
thnks a bunch. 🙂
Hi! this is me again.
im again stuck. could you plz help me how can i remove an easelObj from box2d world.
I’ve used destroyBody() method to destroy a box2d obj, so box2d obj removed but easelobj still on the stage, no physics with it though. Thanks in advance.
Yes, you were close. You also need to call the “removeChild” method to remove it from the easel stage:
@box2dWorld.DestroyBody(object.body) #for Box2d
@easelStage.removeChild(object.easelObj) #for EaselJS
I updated the EaselBox code so that EaselBoxWorld now has a “removeEntity” method that does this. You can use that, or else call the two lines above manually.
Here’s a Gist with an example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gistfile1.coffee
hosted with ❤ by GitHub
you rock! as always 🙂
hi!
could u plz tell me is it possible to remove an event frm easel. like i ve a button
btn.onPress = showSome();
now how can i remvoe onPress event from btn so it wont be clickable nomore. thanks in advance :
I think that would just be a Javascript question. You could try setting the function to null?, e.g.:
btn.onPress = null;
hi, im again.
My qus might not related to easeljs but more related to angrybird.
when we release birds it collide with dummies then when it becomes stable(which im guessing when obj turns red to grey in debug mode ) camera automatic pan for next shot.
Is there any way to do this perfectly? after releasing the bird I used timer for another throw which is not good/ nice.
So, in one word how can i know obj becomes stable after collision occurs
I haven’t tried this, but I think with Box2d, you want the b2Body.IsSleeping() method:
http://www.box2dflash.org/docs/2.0.2/reference/Box2D/Dynamics/b2Body.html#IsSleeping()
i tried that. but its giving me “obj has no method”. as im using easelbox i used
obj.body.IsSleeping()
what did i wrong this time 😦
** although i tried obj.body.IsAwake() which is working.
awesome demo !!!
but can you make the scene follow the head when flying ?
Just FYI the links to the github source are broken on this and other pages. They should point to https://github.com/jcole/easelBox no?
Hi Gruff, thanks very much. The links in this post and on the project site should be fixed now. Cheers!
Why do you use server side JS (i.e. express in app.js) BTW in the github examples? Surely that’s not necessary for these at all?
Hi Gruff, it was because the original source was all CoffeeScript, so I was using the Node module for parsing the CS. I just looked, and it appears I accidentally committed the compiled JS, which I hadn’t intended.
Ah! Makes sense. I’d just assumed it would need compiling beforehand. Thanks!
Hi Jeff, thanks a lot for your awesome job! I took some time to dig into the code and I’ve made a Angry Birds clone using your demo: https://dl.dropboxusercontent.com/u/5728198/DaazCavernas/DaazCavernas.html
Hi Marcelo, I love it. Great job!