Jump to content

Box2D integration


Recommended Posts

Hello,

 

for everyone who wants to develop games or physic simulations with SmartMS, I can already offer a preview of my Box2D wrapper.

 

The example, which I prepared for SmartMS is an adaptation of the example that comes with Box2dWeb.

 

The Box2D wrapper unit (Box2DWrapper.pas) has been mostly auto-translated and thus still contains dozens of imprecise decelerations. By default, all procedures are functions having only variant inputs and outputs. Also nearly all internal variables are yet missing or not accessible (except those needed for the example).

 

To avoid conflicts with keywords of the pascal language, I also needed to rename some method names. So in case one wants to use a specific method, that can be found in the original manual, please check whether it might have been renamed.

 

Now here's finally the link to an archive containing the .opp file and an precompiled html file:

http://www.savioursofsoul.de/Christian/SMS/Box2D.7z

 

Kind regards,

 

Christian

 

PS: The example can also be tested online here: http://www.savioursofsoul.de/Christian/SMS/Box2D.html

Link to post
Share on other sites

That looks excellent. I have only tried the original version and your demo page. At the moment your implementation doesn't have the mouse dragging ability, correct?

 

I really need to get my work done so I can start using SMS again. Too busy with other not-so-much-fun stuff.

 

Link to post
Share on other sites

Cool!

 

First thought it was slower than the box2dweb, then realized you were doing it full-screen (when reducing at the same size, it is similarly fast).

 

Having looked at that, I wonder if http://code.google.com/p/box2d-delphi/ couldn't be adapted. From a first look, the code uses pointers to records, which aren't supported in SmartMS, but those records could probably be mass-replaced by classes (they're dynamically allocated AFAICT). That or adding support for pointers to records support in the compiler...

Link to post
Share on other sites

Making a direct port would be the next step, either from the Delphi port or the original C++ code itself. Doing so would allow smart linking for Box2D, which would be an awesome addition.

 

However, for now, I just like to get it working at all to have a reference. With the reference it's possible to replace only parts with a native translation, to keep the port scalable.

 

One can now write test code so that a native port can be tested right from the beginning, when ported (some stuff, such as the HTML5 canvas backend must be rewritten entirely, so there's a greater chance of errors).

 

Furthermore, my initial task was to write an automatic header translation tool for external JavaScript libraries and Box2D was only my 'library under test' subject.

 

And last but not least it helped me to learn JavaScript better. Prior to this I did not really know how inheritance worked in JavaScript. Unfortunately I still know only a few ways to achieve this, but this is a different story.

Link to post
Share on other sites

By the way, about graphics speed. I've experimented with a neat trick to make faster canvas drawing. When I tested the idea i found it to be slower at first, but having adapted to the style - it now runs a lot faster than ordinary canvas drawing.

 

It goes like this:

 

Webkit has support for "live" backgrounds. So instead of assigning a static image to a DIV (control), or render the graphics to an offscreen picture and then blitting it onto the canvas handle -- you would assign a CSS style that has a "canvas" - and render directly onto the device context without any need for copying pixels.

 

This is where the class TW3BackgroundGraphicContext comes in (see w3graphics.pas). If I am able to automate it, and create a fallback mechanism for browsers that dont support the -webkit feature -- then we should be able to squeze the last drops of performance from the browser. This should benefit box2d quite a bit!

 

Here is the SO case:

http://stackoverflow.com/questions/8263549/performance-of-background-canvas-vs-regular-canvas/10965389#10965389

 

 

Link to post
Share on other sites

Regarding speed in the Box2D demo: It currently uses the slow and unoptimized Tb2DebugDraw class directly from the Box2D library. Typically this bottleneck is removed as soon as the library is used with any other (faster) frontend.

 

Also, I could make the demo run faster (in terms of physics), by changing some constants. The FPS will remain the same, but the demo might probably appear faster. The constants I used have not been tweaked in any way. They have been chosen from the examples or other Box2D snippets found in the net.

 

But beside, the new way drawing sounds interesting. Even if it's only 10% faster, this can already make a difference!

Link to post
Share on other sites

Christian, just when i think we got it all under wraps, you come along and take it to the next level. I have to be honest and say that your demos rock. I've met some guys that take the RTL and smart to the next level, but you really raise the bar. I just had to get that out there.. qdos dude, awsome work on all accounts.

Link to post
Share on other sites

Having looked at that, I wonder if http://code.google.com/p/box2d-delphi/ couldn’t be adapted.

I think I should give it a try. With the existing wrapper there are several issues due to conversion from C++ -> ActionScript -> JavaScript

A lot of things can be done better if only one translation would have been performed. Also some existing RTL code could be reused for this.

 

So far I have prepared everything for a possible port and I'd like to get started as soon as possible. I just need to work on some of my other projects in the meantime.

 

The next step for the Box2D integration would be a native/custom DebugDraw. Once this is done a native translation from C++ -> DWS script code is possible.

Link to post
Share on other sites

It seems as if the 'cite' tag is not working correctly. The first sentence in the post above should be a cite from Eric's post.

 

Also I want to mention, that a direct port of the original C++ library can be improved if not ported blindly. The original is well optimized to be compiled natively, but with JavaScript as backend in mind, there are other bottlenecks.

 

If such a port could be established the SmartMS Box2D would be king of the hill. But there is still so much work ahead...

Link to post
Share on other sites

I'll have a look at adding support for record pointers, so far, it seems it should be possible to have them added to the compiler without too much trouble.

 

If you get started on the port, let me know if there are other features that could help (though they would have to be balanced between "getting it to compiler" and "having it run efficiently", f.i. a pointer to a float could theoretically be supported in several cases, but its performance would likely be atrocious, thus negating the point of supporting it in the first place)

Link to post
Share on other sites

So far I've seen record pointers are not crucial to get a rather native port to work. While records (and pointers) are used to reduce the overhead of classes, this might be irrelevant for JavaScript, since records and its pointers would probably be translated to classes anyway, or am I wrong?

 

On the other hand (with that fact in mind) other ways can likely be found to improve usability. Hence the port would not be 1:1 in the end.

Link to post
Share on other sites

Yes, they would be objects too. I was thinking about to reduce the distance from the box2d-delphi, but if you don't from it, it's not a factor.

Though even if records are object in JS, the compiler handles the copying when assigning a record to another or pass by value, while if you do that with classes, you have to make some form of Assign() method and then use it instead of ":=", as well as a form of Clone() to pass by value.

 

That said I don't know if box2d copies records by values a lot, I guess they strive to avoid it for the larger records, but the smaller ones (vectors f.i.) may well be copied around.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...