A More Perfect Javascript Port

Discuss issues specific the Javascript port of Box2D
curiousaaron
Posts: 18
Joined: Tue Dec 14, 2010 10:38 am

A More Perfect Javascript Port

Postby curiousaaron » Mon Mar 21, 2011 7:24 am

Thank you Erin for adding this forum!

I'm sure other web developers out there have been thinking about a physics engine for HTML5 like I have. After scouring the web, I found 3 decent ports of Box2D to Javascript for use with the canvas element (all based on the AS3 port). The most up to date port appears to be Box2DWeb:

http://code.google.com/p/box2dweb/

Box2DWeb is based on a more recent Box2D release than the better known Box2DJS, and I've had better luck with performance and collision accuracy. Performance on iPad may not be acceptable for most applications, however, I'm hopeful iPad2's speed boost will change that.

Unless there's a sudden resurgence of activity on the other ports, I think this is the best option for Javascript / HTML5 physics out there and I'd recommend it to other developers.

Additions I will be working on or looking for (tell me if you've already done this work!) are:

- rope joints
- tools for simplified 'actor' integration

dbilenkin
Posts: 3
Joined: Thu Mar 24, 2011 7:43 pm

Re: A More Perfect Javascript Port

Postby dbilenkin » Thu Mar 24, 2011 7:47 pm

Perfect timing! I'm right in the middle of working on something and I was using the box2d-js port and was wondering if there was an updated one. It looks like the API is a bit different (What I'm using has no fixtures), but I'm going to update to use this one. I'll be sure to let you know how it goes. Thanks!

curiousaaron
Posts: 18
Joined: Tue Dec 14, 2010 10:38 am

Re: A More Perfect Javascript Port

Postby curiousaaron » Fri Mar 25, 2011 7:18 am

Sounds good! I'd like to see some community build around this so people can share information, since it's sometimes hard to make Box2D do what you want, and ports of it all have their own peculiarities :)

dbilenkin
Posts: 3
Joined: Thu Mar 24, 2011 7:43 pm

Re: A More Perfect Javascript Port

Postby dbilenkin » Fri Mar 25, 2011 8:49 am

Oh man, I've been at it til' 2 am last night and picked it up again this morning. I've run into a few issues. First one took me forever to figure out. box2djs depends on prototype, and of course box2dweb doesn't. I had to make a small change in order to get it to work as I wasn't ready to get rid of prototype for some of the other stuff I was using.

I had to change this line of code in Box2dWeb (line 2498):

for (var each in __this.m_moveBuffer) {
queryProxy = __this.m_moveBuffer[each]; {

to:

for (var i = 0, len = __this.m_moveBuffer.length; i < len; i++) {
queryProxy = __this.m_moveBuffer[i]; {

I think it's because prototype is overriding "for, in" and it ends up going through the loop more than it needs to and breaking.

So, finally after getting this working, I had to change quite a lot. The whole api has pretty much changed for creating bodies, shapes, joints, ok pretty much everything. I'm getting close, but now I'm stuck with bodies that are not properly rotating around revolute joints.

I need to take a break. I can't wait to get to the point where this is working, and then I can compare between the two versions in terms of performance and stability. When I get there, I'll let you know.

When my project is a little bit more finished, I'll definitely share it here. It uses a genetic algorithm to evolve locomotion in different creatures. Kind of like http://boxcar2d.com/ but with creatures instead of cars.

curiousaaron
Posts: 18
Joined: Tue Dec 14, 2010 10:38 am

Re: A More Perfect Javascript Port

Postby curiousaaron » Fri Mar 25, 2011 8:55 am

Cool! Thanks for sharing!

I actually have a question out to the author of Box2DWeb regarding revolute joints, so I'll let you know if I learn something. If you get it figured out first let me know. Thanks!

Uli
Posts: 2
Joined: Wed Mar 30, 2011 8:26 am

Re: A More Perfect Javascript Port

Postby Uli » Wed Mar 30, 2011 8:41 am

@curiousaaron: Your question is answered.

@dbilenkin: The for/in problem is now fixed in trunk. However, I think it's a bad idea to extend Array.prototype, but most frameworks are doing it...

I'm going to make a few improvements today and tomorrow. For instance moving inherit() from Function.prototype to another place.

Feedback for non-official versions in trunk is always appreciated. I think I'll add a new official release ("Downloads"-section) next week.

Cheers,
Uli

curiousaaron
Posts: 18
Joined: Tue Dec 14, 2010 10:38 am

Re: A More Perfect Javascript Port

Postby curiousaaron » Wed Mar 30, 2011 8:51 am

Thanks again, Uli!

And for anyone who stumbles on this post, the reason my joints weren't working is that I was accidentally passing fixtures as bodyA and bodyB when creating my joints... d'oh!

dbilenkin
Posts: 3
Joined: Thu Mar 24, 2011 7:43 pm

Re: A More Perfect Javascript Port

Postby dbilenkin » Sat Apr 02, 2011 1:31 pm

Thanks, Uli. The for/in issue was a side effect of using the prototype framework because that's what box2djs uses.

That being said, have you thought about addressing performance? Unfortunately, I'm at a point where I went back to the other framework because despite being based on an older port, it performs faster. It's possible that I'm not using box2dweb completely correctly but after spending time trying to use the right scale and tinkering with some things, it was still slower.

Do you think it's just a matter of me not using it right, or do you agree there is room for performance improvent. Especially with JavaScript, performance is always a big concern, because it's slower than all the other language ports already.


Return to “Javascript”



Who is online

Users browsing this forum: No registered users and 3 guests