Game Development Community

dev|Pro Game Development Curriculum

2D Tuesdays: Box2D Wrapup

by Michael Perry · 06/19/2012 (10:25 am) · 30 comments

2D Tuesdays: Box2D Developer Updates


static.garagegames.com/static/pg/blogs/michael-perry/TorqueLogoSmall.png

Kickoff

Greetings everyone. It's 2D Tuesday! Each week someone on the 2D team will post a blog or resource related to Torque 2D and/or iTorque 2D. The majority of the posts will come from myself, but don't be shocked if someone else takes over for me from time to time.

Last week, I continued the discussion of Box2D integration in an R&D branch of our repository. Theory turned into reality when actual code and a video were posted. This week, I'm going to wrap up Box2D and move onto other topics. First, here's the "let's cover our bacon section":


static.garagegames.com/static/pg/blogs/michael-perry/TorqueLogoSmall.png

FULL DISLOSURE

Do not take the content from these blogs as law. I'm never going to say "this will be in x.x version" or "this is ready to be used right now". There's going to be a lot of R&D discussion. I might even post a discussion I had with Melv that ended up being scrapped. It might be useful for you to understand how we communicate internally, or amusing to see the mad scientists we really are. I will not post timelines. I will not post release dates. I will not commit the team to something we cannot deliver on.


static.torquepowered.com/static/pg/blogs/michael-perry/separatorz.jpg

static.garagegames.com/static/pg/blogs/michael-perry/TorqueLogoSmall.png

Will ____ work?

There is one guaranteed question type multiple people will ask when you talk about changing major systems. It almost always relates to an existing project or a project planned in the near future. That question is "Will ____ still work?" The blank part is usually a feature, function call, or something a user has written. With the discussion of Box2D, the most common targets are moveTo, rotateTo, and coordinate conversions. As I said in my past blogs, I come prepared with a backlog of e-mails, chats, and SVN commit messages. Why spend time thinking of scenarios when I can just copy/paste something like this:

Quote:Everyone,

I have added both a new "MoveTo" and "RotateTo" to the t2dSceneObject. These new methods have different arguments than the previous ones and are far less invasive. The previous ones caused interpenetration issues and essentially went against the physics/collision system. The new ones work with it so let me explain how they work.

moveTo

%sceneObject.MoveTo( worldPoint, [time = 1000], [autoStop = true] )

Quote:
All "moveTo" really does is calculate the required linear velocity from the current position to the specified "worldPoint". It does this by calculating the relative distance and dividing it by the specified time (which is specified in milliseconds and defaults to 1000 i.e. 1 second). It then immediately sets the linear velocity. The third and final (optional by defaults to true) parameter is "autoStop" which will, if set to true, reset the linear velocity to zero after the specified period of time. It will not reset any angular velocity, only linear.

As is described in the method documentation, there are a number of things that can stop the object reaching its target. The first is if the object as linear damping active. The second is if the object collides. I didn't want the "moveTo" touching anything other than the linear velocity so it is entirely up to you to ensure the object is able to reach the destination.

When the time has elapsed, the method will perform a callback to the scripts as so:

function t2dSceneObject::onMoveToComplete( %obj, %targetPosition )
{
   echo( "Object" SPC %obj SPC "moved to" SPC %targetPosition );
}

Quote:As you can see, the function returns the object that has completed the move-to as well as the target position you originally specified. You are therefore free to ensure the object really did reach the specified target by using something like:

%obj.setPosition( %targetPosition );

... within the callback.

rotateTo

%sceneObject.rotateTo( angle, [time = 1000], [autoStop = true]) )

Quote:
All "rotateTo" really does is calculate the required angular velocity from the current angle to the specified "angle". It does thi by calculating the relative, smallest arc to reach the target angle, dividing it by the specified time (again in milliseconds and defaults to 1000). It then immediately sets the angular velocity. The third and final parameter is identical to the one used in the "moveTo" call expect this time, if set to true, it resets the angular velocity to zero after the specified period of time. It will not reset any linear velocity, only angular.

As is described in the method documentation, there are a number of things that can stop the object reaching its target angle. The first is if the object as angular damping active. The second is if the object collides. I didn't want the "rotateTo" touching anything other than the angular velocity so it is entirely up to you to ensure the object is able to reach the destination.

When the time has elapsed, the method will perform a callback to the scripts as so:

function t2dSceneObject::onRotateToComplete( %obj, %targetAngle )
{
   echo( "Object" SPC %obj SPC "rotated to" SPC %targetAngle );
}

Quote:
As you can see, the function returns the object that has completed the rotate-to as well as the target angle you original specified. You are therefore free to ensure the object really did reach the specified target by using something like:

%obj.setAngle( %targetAngle );

... within the callback.

Quote:As a quick note: if you issue another move-to or rotate-to while a previous one is running, the previous one will immediately stop and will not result in a callback or stop the velocity however your new call will set the velocity and result in a callback. If you need to know whether a previous "moveTo" or "rotateTo" has completed on a scene object then you can use:

echo( %obj.isMoveToComplete() );
echo( %obj.isRotateToComplete() );

Quote:The above set-up requires the minimum of processing as it only requires modifying an initial velocity and performing a single schedule over the specified time at which point it takes the final action (auto-stop) and then performs a callback. For situations where these methods are used, it is unlikely that the objects being moved/rotated have either damping or collision activated however if they do then you need to understand the implications.

Hopefully, this will provide everything everyone needs.


Coordinate System Changes
As I mentioned in last week's blog, integrating Box2D resulted in sweeping changes throughout the engine. One of the bigger changes is severing the connection between the scene and screen resolution. Previously, if your object was 64x64, that was a measurement of both world units and pixels. They were tied together, which is an old way of doing 2D. It works, but there are better alternatives.

Now, the term better is subjective. I talked about this with Conor O'Kane pretty early this morning in IRC. He expressed dismay over the change to the coordinate system. Make no mistake, we wouldn't apply these changes without going through the pains ourselves. Eat your own dog food, right? In the very same e-mail that contained the information on moveTo and rotateTo, the team started to report major issues with an existing game we were working on. We were porting an older T2D project, which still contained values related to pixels. Here is that discussion log (please forgive any typos, we weren't writing an English paper):

BEGIN TRANSCRIPT

Richard Ranft: Indeed, thanks! The script versions were ... inadequate.


Doug Poston: So it looks like we're still having issues. Is there some sort of limiter on velocity? Can you think of any reason why we can't travel 1km in 1.2 seconds
(other than Nevada speed limits)?


Melv May: I'd be surprised if there's a limit using linear velocity even if you're pushing the limits on the forces involved. I'll check out the speeds you're referring to.


Doug Poston: I'd be surprised too, but it looks an awful lot like that's the case. Since we're using 1 pixel = 1 meter (which is crazy), we're moving 105m tall cards almost 1km in distance in 1.2 seconds (breaking all natural laws of physics).


Kyle Miller: Below are the relevant areas in the documentation:

Box2D FAQ - "How do I convert pixels to meters?" in the API section
Box2D Manual - Section 1.7 "Units"


Doug Poston: The problem does appear to go away when you reduce the playing field by two orders of magnitude (e.g. divide everything by 100).


Michael Perry: So we have isolated the problem. The next step is to try and decouple the resolution and pixels from the world units. We have this hierarchy:

GuiCanvas - Set to window resolution 1024x768
-> t2dSceneWindow - Set to window resolution 1024x768, also contains the camera which uses the window resolution values
-> t2dSceneGraph - Set to window resolution 1024x768, contains all the scene objects which are too large right now

To me, it makes sense that we keep GuiCanvas at 1024x768. This is a sub-view that needs to fit the window. t2dSceneWindow is also a GuiControl, so I think it makes sense for that to be

1024x768. However, the internal camera should start working with Box2D world units. t2dSceneGraph is a SimSet that contains a property called cameraSize, which is pulled by t2dSceneWindow.

It does not actually get used for anything in t2dSceneGraph.

So my opinion is we break the pixel/world unit coupling in t2dSceneWindow's camera. We obviously need the camera to zoom in dramatically, kind of like what Doug just mentioned in his reply.

I might be muddling things up because of how hectic things are for me right now.

@Melv - Am I making sense? Anything you can add?


Melv May: Maybe I need to read this when I'm more awake but the system is decoupled from pixels which is what I did in my testbed and the truck demo.

You create your scene using world units and set your camera area to scale it. If you are running different resolutions it makes no difference apart from when the aspect ration changes. In the end, the camera renders to the target resolution but all code within it is unaware of that.


Kyle Miller: Unless I'm misinterpreting it, Box2D FAQ - "How do I convert pixels to meters?" in the API section seems to suggest a different approach - that we set a scaling standard and convert at the engine/physics level? This is perhaps something we should consider. What's best for usability? Is it better for a user to have a direct correlation between the art they're creating in Photoshop and that in the tools? We need to carefully consider our approach here as this is a major decision that will greatly affect current and all future templates.


Melv May: This is not a physics issue, it's an issue that affects all game engines so there are no changes required from the physics standpoint. Let's not bring box2d into it. :) You

simply cannot create a single resolution art the works 1:1 on different resolution devices without showing different scene areas i.e. scaling. This is the same situation you had prior to the box2d work.

If you want the art to be 1:1 with pixels on the screen then you've got a hard task. By far the easiest way is to have the artwork created for the highest res and it be scaled down during texturing on lower resolutions which is what happens now. If that doesn't suit then you need the system to choose alternate artwork for different ranges of resolutions. Again, nothing to do with box2d.


Kyle Miller: Sorry for the confusion, Melv - I don't think I was very clear in my email. I did not intend to imply there was an issue with Box2D. I think we're on different pages a bit here, and there are additional factors Mich and I have gone over at this point, so let's just continue this in our discussion tomorrow. Thanks for taking the time to respond!


Melv May: Let me illustrate....Truck demo creates objects at reasonable sizes suited for the physics system. I chose an area of the camera to show around 100 x 75 meters wide (4:3).

This is resolution independence. Now, at 1024 x 768 (4:3) you can see there's a fixed scaling i.e. 1 pixel = (100/1024) meters in X and (75/768) in Y. If you change the resolution then you change the pixel:meter ratio and there's little you can do about it.

You can either live with the fact that the art is scaled or you can select alternate art for that resolution or a range of resolutions. Nothing to do with box2d. In you previous code, you used 1:1 of world units to pixels however that breaks down if the resolution changes. So I ask, what has changed? The need to have multi-resolution artwork?

(To Kyle)Hey, no problem. I'm simply confused on what has changed here. Te same problem existedin the previous codebase.

END TRANSCRIPT

I admit, it's a fairly dry discussion with the exception of Doug's Nevada reference and some smileys. As you can see, miscommunication can happen and sometimes a single thread can't close the loop. We ended up having a lengthy Skype call to hash out the details and debate the merits of retaining the pixel sizing of objects vs more realistic world units.

In the end, using standard world units won. For anyone who might be concerned that their project might be getting gimped, even if they do not create a physics-driven game, do not be alarmed. We have ported two games that do not need a full physics simulator. One is a card game, the other uses A* pathing. That requires full control over placement and motion. The games turned out just fine. Then, there are the games that will make clever use of physics:





static.garagegames.com/static/pg/blogs/michael-perry/TorqueLogoSmall.png

Next Time

That's it for this week folks. I'm sure you have plenty of questions related to Box2D and I could probably keep posting for another month on the topic, but I'm going to end it in this post. Feel free to ask more questions here, because next week I'm leaving the realm of physics and talking about other exciting R&D that has been going on. I've seen a lot of naysayers who demote the usefulness of behaviors, so I'm also going to try to convert a few of you. Behaviors rock! Next week I'm going to prove it to you.
Page «Previous 1 2
#1
06/19/2012 (11:06 am)
Is it me, or the post is missing something? ;)
#2
06/19/2012 (12:19 pm)
@Novack - There, fixed =)
#3
06/19/2012 (2:24 pm)
Okay, one question which I never got answered:
Will this update be free? :)
Sorry, but I don't know anything about the update policy of you guys.

Really looking forward to it.
#4
06/19/2012 (3:17 pm)
We haven't nailed down update pricing or whether there will be a cost for an upgrade to the next series of engine releases.

These discussions are part of our research and development mode--which is the reason Mich is careful to include the full disclosure statement. They are great discussions and development insights, but they are not a guarantee of future features for these engines. Providing these discussions allows you guys to weigh in on the discussion and provide feedback as well.

And it also provides us with a barometer of how interested the community is on different features.
#5
06/19/2012 (3:22 pm)
Thanks for your answer David.

I know its not nailed yet if this is gonna come out or not, but I am just excited about new features and on top of that, I basically just got the engine. Thats all hehe. So of course, I would be a bit sad if I had to buy a new version a few months down or pay again etc. Think you get my concern.

But overall I feel this would be a great addition, regardless pricing etc. I had some experiments with Box2D and I think it is a really sophisticated piece of open source software.
#6
06/19/2012 (3:26 pm)
@MrSpaceGame - Welcome to the T2D community then =). I'm not the policy maker or the biz dev guy. I am, however, annoyingly vocal and a big agenda driver. If you have suggestions on anything, please post them. I read every post in these blogs and keep them in mind when I'm called into discussions about updates and pricing.
#7
06/19/2012 (4:11 pm)
Very good stuff here.

I would like to say that I'm fine with the new engine not backward compatible with the old engine.
#8
06/19/2012 (4:18 pm)
1. I really like the decoupling of screen resolution from world units. I see myself as creating a sandbox for people to play in, not a pixel perfect painting.

Though I can understand how others see themselves differently, so I'm not going to say they're wrong or anything like that.

I just use my camera quite liberally ;-)

2. Still reading each of these tutorials with one eye on spriter. I'm beginning to think more and more I should not overcomplicate my current project and use standard sprite animation with iT2D 1.5 That will make this all the more interesting and relaxing. After reading the Box2D manual and the spec for Spriter, I'm either imagining an elaborate set of joints to replicate the tweening animation.

I understand that we're moving away from talking about box 2D, but I would still love somewhere, somehow some content about how to hook up an animation system like that.

These are awesome and I'm a diehard GG fan because of content like this.
#9
06/20/2012 (12:07 pm)
I think I'm missing something fundamental, but I've never noticed the pixels and world units as coupled!

In my game, I have a 800x600 pixel window, and a camera view of 64x45 world units.

I *could* have set a camera view of 800x600 world units I guess, and this would have "coupled" them, but nothing would be forced by the engine. When I made the new scene, the default wasn't 800x600 camera units either, so I didn't get into that habit.

It might be *nice* to know my game characters have one texel for one pixel, but it felt like a conscientious choice by the Torque engine writers and I agree.

And now I've made a copy of the demo game at 1064x768. This would have screwed up the 1:1 even if I had set it.
#10
06/20/2012 (3:01 pm)
Will it be possible to modify the b2Settings.h of Box2D or changing the value from Torque2D without rebuild the whole engine from a pro version?

I know I used a global scaling system of 12 in my game engine and change a few settings so the simulation feel natural and not sluggish by default.
So being able to change these values is a key feature.


I seriously hope it will be a free update...
#11
06/20/2012 (3:09 pm)
RE: Free Updates

iTorque 2D is a solid product in its current state with an extremely reasonable price. Hoping/requesting for an update that they put considerable resources into to be free is asking them to value this new work less. Obviously it has some value if it sells more existing licenses, but not much if the existing license owners don't pay for the benefit of the update.

I would be more than happy to pay for this update if it means that it'll be as solid as their recent releases. If it means that Michael can get more test resources or more time to update the documentation, I will happily continue to be a GarageGames customer.

I don't mean to discount your hopes/wishes, just show that some of us believe in paying for this work.
#12
06/20/2012 (4:00 pm)
Hi David,

well I own Torque2D (aka TGB) and I am not so satisfied with the quality of it to be very honest. There ain't much documenation coming with it and most things I want to achieve is trial and error. Some parts of the documentation are even wrong and misleading, some parts incomplete, like UI Elements. Most Open Source Products have better documentation. I know about the TDN, but many stuff there is either outdated or for iTorque, Torque3D or does simply not work.

Sorry to use such harsh words, but thats how I feel since I got the engine. Don't get me wrong, Torque is still a good engine. But it is lacking a lot in those parts, even for the good price.

I would be fine by paying a small upgrade price (20ish $) if there are enough NEW features that justify it, but not the full price again. However, I DO expect them to complete and correct the documentation, which I hope will happen in the future.
#13
06/20/2012 (9:50 pm)
Hi MrSpaceGame,

Thanks for sharing your opinion. I own both TGB and iTorque2D and I can tell you that one of my issues in the far past has been documentation.

What I do know is that Michael has turned that around for GarageGames. The recent iT2D release featured much better tutorials and comprehensive documentation. I rarely check out TDN anymore because of it. Additionally, the iT2D forum has been a great resource with multiple users being virtual encylopedias of how to handle certain things.

I'd challenge the notion that most open source products have better documentation, but without data, it's a moot point. I hope you have the time to provide a clear organized list of issues that you have with the documentation so that GG can use the feedback to deliver to your expectations.
#14
06/21/2012 (6:11 am)
@David - Much appreciate the compliments on the iT2D docs. What we have is fairly solid. It needs some updating, but thats expected with each update. For iT2D, I myself would like to see more tutorials and stronger script reference.

I will defend MrSpaceGame on some points on the T2D docs. Those are old and incomplete. We've tried to update as much as we could in the allotted times, but I can speak for Geoff when I say we can do better. For T2D, I myself want to update available tutorials in the official docs to be more accurate. It could also use the same script reference iT2D would get.

What really renders this all moot is if this R&D goes live into a release, a very large rewrite of docs for both iT2D and T2D would have to occur. Still, I would also like to hear any other doc suggestions MrSpaceGame has.
#15
06/21/2012 (6:17 am)
Good to hear some of the behind-the-scenes discussion that's going on regarding the units/pixels. I'm really looking forward to trying this out!
#16
06/21/2012 (4:26 pm)
It's not really a problem for me if the documentation is a bit outdated as long as the API/examples are working and the bug get fixed, because it's a point where I can't do much.

Like MrSpaceGame suggested a pay update would only be relevant if a few important features were added like for example shaders, lights, performance gain, new supported platforms, etc. Though I don't even need all that :)

It's not like if Box2d was an in-house technology!
I often read in the forum that if you are not happy with the current (awful) physic system you can try to support Box2d by yourself.

While a powerful physic system has always been promised and advertised more than 6 years ago, I think it's fair not having to pay only for that. Just my 2 cents.


By the way, did you try to mix Box2D physics with your networking system? I guess there is potential issues with that, no? IIRC real-time multiplayer Box2D simulation as always been a bit tricky because of many factors. Do you already know if there will be compatibly with both system?

Thanks
#17
06/21/2012 (4:38 pm)
Quote:I often read in the forum that if you are not happy with the current (awful) physic system you can try to support Box2d by yourself.
Very true. Hopefully those who have read the past three blogs can see that it is not a simple task. A proper implementation, especially when you need it to be productized for mass-use, takes time and affects a lot of systems.

Quote: I think it's fair not having to pay only for that. Just my 2 cents.
Fair enough and I agree. A paid update would include more, but that's just inviting the speculation monster which could derail the series.

Quote:By the way, did you try to mix Box2D physics with your networking system?
If there was a networking system in T2D/iT2D, we probably would have tested it. As it stands, there is only turn-based networking, which does not care about real-time physics simulation.
#18
06/21/2012 (6:44 pm)
Quote:did you try to mix Box2D physics with your networking system?

networking system? crap I must have misplaced mine, I can't find it anywhere. Do you have a spare I can borrow?

@Mich - I really enjoy these blogs, thanks for posting.
now if we can just get some for t3d...
#19
06/22/2012 (6:36 am)
Quote:If there was a networking system in T2D/iT2D, we probably would have tested it. As it stands, there is only turn-based networking, which does not care about real-time physics simulation.

Oh yeah! True
Sorry, I forgot about that... I never use it for networking before. Was just thinking about it.
#20
06/22/2012 (12:43 pm)
You rock Mich!
Page «Previous 1 2