Game Development Community

Torque 2D Development Blog - 2D Model Overview

by Melv May · 11/10/2009 (8:26 pm) · 32 comments

static.garagegames.com/static/pg/blogs/jason-hetu/Torque-2D_Development-Blog-Header.png

Welcome to another entry in the Torque 2D Development Blog series. In the last blog we announced that our new 2D engine would use Box2D as the physics core, inheriting all the physics goodness that it provides. Today we want to give you more detail on both how and why we did this.


Rigid Body Physics

Rigid-Body physics in games has become extremely popular in recent years, not simply because it adds great game dynamics but also because it can make the task for developers easier. It is far simpler to configure a vehicle to work very much like it would in the real world and draw on that knowledge than it is to try to fake every aspect of its movement. This is particularly true when the vehicle has to interact with its environment in realistic and convincing ways. Having a unified way to do that for all game entities simply makes development easier. Previously, we implemented our own physics solutions, but there are now so many great 3rd party physics solutions out there that it makes sense to work with them.

As we've already announced, we chose Box2D which provides pretty much all the features you'd like to see in any realistic 2D physics simulation. When we started looking at Box2D, however, it became pretty obvious that we couldn't just “plug” it into the existing codebase. That was one of the many reasons why we chose to develop a new Torque 2D from the ground up.

So what does the physics look like in Torque 2D? Well, the first thing to point out is that we've hidden away Box2D from the end-user. Sure, you can drill down to Box2D if you want but there's no need to do so. The reason we chose to do this is three-fold:

  1. Abstract away from some of the confusing aspects of Box2D, which is especially important for new users.
  2. Provide the ability to unify the way all aspects of Torque 2D are controlled, e.g. physics, rendering, sound-fx etc.
  3. Provide the ability to extend Torque 2D in a unified way.

2D Model

In doing this, the 2D Model was born. Now we're sure this immediately puts the thought of 3D Models into your mind. The good news is that this is exactly what it is! Torque 2D has its own 2D equivalent of a 3D Model. This means that inside the model you can have all the features provided by Torque 2D such as collision-detection, rendering, joints, pathing, sound-fx, particles, text etc. These 2D Model features are represented by things called Model Nodes. These nodes are arranged into a hierarchy meaning that some nodes are children to other nodes. To give you a sense of how powerful and flexible the 2D model is, consider that the “Apocalypse” demo shown in the last blog was done using a SINGLE model!

Now as crazy as that sounds, it should give you an idea of how flexible the 2D model is! Typically though, you can think of a model as a way to encapsulate some discreet element of your game such as:

  • Player Character
  • Non-Player Character
  • Misc Scene Item
  • Vehicles
  • Tile-Layers
  • Etc
Your scene will be composed using as many 2D Models as you like. So what does the 2D Model node hierarchy actually look like? Well we can't show you everything that's in the model, but we can show you enough to give you an idea of what's going on:

static.garagegames.com/static/pg/blogs/melv-may/2DModelDiagram.png



















Bodies

A body is a point in space that can be orientated, have velocity, have mass, etc. As well as having an unlimited quantity of bodies, each can also have unlimited children nodes of any kind. These children nodes perform various tasks and live relative to the body in space so that if the body moves or changes angle, so do its child nodes. Bodies are extremely important to the 2D Model as they are the parent for all the other node types, so if you destroy a body, all its children are destroyed. Bodies also have an extremely powerful feature called "sleeping." When a body doesn't move or isn't involved in any collisions for a small period of time, it automatically goes to sleep. This has the effect of reducing its overhead to practically zero. If a body is involved in a collision or is moved either by the user or a joint, then it automatically wakes up. This happens completely automatically and results in huge performance gains when bodies are asleep.


Collision-Shapes

Collision-Shape nodes allow you to add collision areas to your model. These collision-shapes not only add mass to the body and affect how the body moves/rotates, but are how reactions to collisions are configured. Currently there are two types of collision-shapes: circle and convex-polygon. Convex polygons can also be defined as a single edge only (line) and can even be configured to have one-way collision only, which is extremely useful for platformer games. As is the case with all nodes, you can have as many collision-shapes as you desire. This has the additional benefit that if you want to define concave polygons, you can by using multiple convex polygons.

Collision-Shapes can also be configured so that the parent body becomes immovable. This is typically used for your level geometry. You can even configure collision-shapes so that they are "sensors" in that they report collisions, but don't cause the body to react to the collision. You can mix all these types of configuration under a single body as well if you so choose. Your player may need to collide with stuff, but it may have regions where you simply want to know if something collided. You can even detect collisions starting and collisions ending!


Render-Actions

Torque 2D abstracts rendering into what it terms Render-Actions. A render-action simply renders things to your scene. There are several types of render-action nodes including several sprite types, particle-effects, text etc. As mentioned before, you can have as many render-action nodes as you wish. This offers you a powerful way to design your game in that you can mix any render-action nodes together and create the effect you're after. As with all body children, these render-actions move/orientate with the parent body.


Joints

So we've skipped many additional children nodes here (most of which will be subject of a future blog), but let's finally discuss a very important one - the Joint node. There are many types of joints and their sole purpose is to bind two bodies together in cool and interesting ways. You are not restricted to only linking bodies within the model; you are free to link any two bodies that exist in the scene, meaning you can binding multiple 2D Models together.

As an example of how you can use joints, imagine that you wanted to ensure that two bodies are always a specific distance apart. To do this you could add a “Distance” joint between those two bodies. You can also configure the joint to be soft (e.g. springy) or hard (e.g. rigid), what distance you want etc. Alternately, if you wanted to have one body revolve around another, then you could use the “Revolute” joint. You could then configure the angle limits of this joint so the bodies could only rotate through specific angles. You could also activate the motor on the joint to cause it to turn one body with respect to another. You can even set the motor speed, its maximum torque and many other cool things. There are lots of joints available, each of which has a specific purpose. It is also common to use multiple joints together to create even cooler effects.

Joints are the bread and butter of interesting physics and are way too much fun to play with.


Prefabs

As you can see from above, it's possible to create 2D Models as simple or as complex as you like. Another great advantage of being able to aggregate functionality into a Model is that you can load/save them just like you would a 3D model. We call these Prefabs and they give you the ability to distribute your models between team members and/or the community. Now that's cool!


All together now...

Now that we've introduced the 2D Model, here's another look at the Apocalypse demo video, but this time showing some of the elements we've described above using a cool feature we've not yet talked about called visualizers. If you look closely you'll see things waking up and going to sleep as indicated by transparency of the green lines:




Performance

Obviously it is important to keep performance high and memory consumption low. Here we've addressed both. The 2D Model uses a very powerful and configurable caching factory to ensure that a balance between node creation and node memory usage is kept to a minimum.

Many of the nodes are extremely lightweight and incur a tiny cost, especially when automatically retrieved from the cache. Render-Actions are particularly lightweight in that they allow you to create thousands of them without concern. Also, all rendering in Torque 2D uses a batching system to further increase performance.

The performance gains of this system, especially rendering, has allowed Torque 2D to free itself from having lots of specific types of objects such as tile-layers. Tile-layers don't exist in Torque 2D! Before you panic though, let's explain what they've been replaced with. A tile-layer can now be a single body with as many render-actions as you like. This has huge implications for people designing tile-layers and other complex game entities. You can now have any type of node in your "tile-layer" such as sprites, particle-effects, collision-shapes, sound-fx which all move together. Because the sprite render-actions and other aspects are so lightweight, they can be used as tiles. They are also not restricted by size, position or orientation - the builder will allow you to control that placement. Imagine creating a "tile-layer" that includes joints, particles, sprites etc!

As a final point to whet your apetite, Torque 2D now has a real X,Y and Z value for positioning your 2D Model nodes at depth. This Z coordinate means that you can place stuff at depth values such as 3.5 or 4.05 or any value you like. You can also place nodes at depth relative to other nodes, at the front or back of the scene! This works with Box2D in that we use metres, kilograms and seconds. We also provide comprehensive camera and construction methods to work with metres or pixels. We'll provide more on this feature and its usefulness in future blogs.


Conclusion

So as you can see, Torque 2D has a comprehensive 2D Model and puts practically no restrictions on the developer on how it is used. Anyone who knows how to use C++ can also extend the 2D Model as there is a standard way to do this that doesn't involve hours of coding.

In designing Torque 2D we wanted to free users from having to deal with heavy-weight, specific-use objects but instead offer a way to almost “draw” the scene the way they want it. In offering a unified 2D Model with unlimited content, we've definitely achieved that. Almost everything in Torque 2D centers on the 2D Model so no matter what you're interested in - physics, collision-detection, rendering, sound-fx, etc. - you'll soon be at home using it.

We hope this blog has given you a taste of how everything in Torque 2D is unified. Please feel free to ask about this 2D Model and how you could use it for your next big game.



This is post #6.

Torque 2D development blogs:


Page «Previous 1 2
#1
11/10/2009 (9:29 pm)
Wow Melv, That video is awesome. (now if torque 3d could do that out of the box....)
#2
11/10/2009 (9:34 pm)
Very nice. A great direction to take the 2D engine in a modern direction!
#3
11/10/2009 (10:56 pm)
This is ridiculously awesome :)
#4
11/10/2009 (11:16 pm)
Always fun to see what you're working on, Melv.
#5
11/11/2009 (2:07 am)
Wow, sounds like it's be amazing!

Hey is that a game that is being made or was that made just for the demo?
#6
11/11/2009 (2:23 am)
Where do I pay?
#7
11/11/2009 (2:32 am)
Great blog :) (does not seem nearly as hard as it seemed through the facebook comment ;) )

Definitely looking forward to it. The move away from the TGE codebase to behave "current day compliant" seems to do a lot of good to it :)
#8
11/11/2009 (4:26 am)
I'm torn... I check every hour for a new T2D blog post, and I'm always excited about the new features, and disappointed because it isn't a "Torque 2D Beta Released - Buy it now!" post.

I'll be patient though. For an hour at least...

Seriously though, T2D is looking really impressive - there's nothing that comes even close to it, for any price (I can say the same about TGB, actually). Keep up the good work and the blog posts please.
#9
11/11/2009 (4:38 am)
Congratulations!

Don't you want to unify Torque3D while you're at it? ;-)

Nicolas Buquet
www.buquet-net.com/cv/
#10
11/11/2009 (5:29 am)

Quote:This is post #Awesome.

At least, that's how it read to me :)

Amazing work!
#11
11/11/2009 (9:31 am)
"This is mutany!" "THIS IS MADNESS!"

"Madness?"

"THIS IS AWSOME!!"

*Epic Sparta Kick Into A Bunch Of 2D Boxes* Powered By Box2D ^^
#12
11/11/2009 (10:58 am)
Do want.....
#13
11/11/2009 (1:26 pm)
Give us beta now!

A lot to take in but looks great! Can't wait to use the new physics to make some games!

#14
11/11/2009 (1:31 pm)
I said I was going to be patient. I take that back.
#15
11/11/2009 (1:34 pm)
I hope this tech makes it into Torque X eventually
#16
11/11/2009 (2:43 pm)
This sound really cool.

I have a question. does this mean we can use depth instead of parallax mapping?
#17
11/11/2009 (3:01 pm)
Having objects at depth provides opportunities for parallax yes. I'll provide more information on Torque 2D cameras in a later blog.
#18
11/11/2009 (10:16 pm)
More, more, give us more... :) Very much looking forward to this.
#19
11/11/2009 (10:34 pm)
Melv, you say a body automatically goes to sleep. Is that just for collision? Can it have other processes that keep going (like "thinking") or does the whole object essential go to sleep and stop processing?
#20
11/11/2009 (11:01 pm)
Jeff, its a Box2D thing. When an object goes to sleep it doesn't process any of its own collisions until its woken up by another moving object.
Page «Previous 1 2