Box2D integration in WaveEngine 2.3.0

Box2D is a 2D physics engine for games, for more related help, please visit http://www.box2D.org.

Now, in WaveEngine 2.3.0, we have integrated Box2D as 2D Physic engine. In previous versions, we use Farseer Physics (C# reimplementation of Box2D https://farseerphysics.codeplex.com). Farseer is a great tool, but we decided to step forward and include a 100% native physic engine.

Keys of the new Physic 2D engine

New 2D Physic API

In WaveEngine 2.3.0 we have improved the 2D Physic API, adding new features and enhancing existing ones.

We’ve tried to maintain the name of Box2D properties and methods to facilitate the adoption of the new physics.

RigidBody2D – Collider2D relationship

In previous Wave Engine versions, RigidBody2D component required to have a Collider2D component, and ONLY can have one collider.

Now this dependency is removed. You can have a RigidBody2D without a Collider2D, but as a counterpart, you can add to an entity all Colliders that you desire.

In Box2D terminology, a RigidBody2D internally adds a b2RigidBody instance to the world, and every Collider2D attached creates an associated b2Fixture, with a b2Shape.

In that image we can see an entity with a RigidBody2D and three Colliders.

RigidBody2D features

We have exposed the b2RigidBody class’s properties and methods through the RigidBody2D component, with new features such as IsBullet, GravityScale and much more.

Another relevant key is that we’ve moved several properties from RigidBody2D to Collider2D component, especially all related with the collision and surface properties (such as Density, Collision Group configuration, restitution,  friction…) including the Collision events (BeginCollision, EndCollision.)

If you want more information concerning Box2D rigid body, view the documentation available here: http://box2d.org/manual.pdf, Now it’s easy to extrapolate that concepts to WaveEngine.

New Collider2D features

In WaveEngine, Colliders2D are the components that specify the entity shape in a 2D environment. We’ve exposed b2Fixture and b2Shape properties and methods in Collider2D.

We’ve improved the shape configuration, now you can change the size, offset and rotation of Collider2D shape. In earlier versions, doing that was more difficult.

As we said previously, we’ve moved to Collider2D all collision and surface properties that change increase the possibilities to specify your physic body. For example, you can set two colliders to adapt better to a sprite shape, and sets different physic properties (density, friction, etc…) to each collider.

Now you can specify in WavEditor the collision group flags, with a new GTK widget:

Polygon Collider

We have removed the old PerPixelCollider2D component, and It’s been replaced by the new PolygonCollider2D.

While PerPixeCollider2D checks every pixel of a texture to detect if it collides with others, PolygonCollider2D defines a polygon (list of points) which is more optimal to detect collision, and is supported by Box2D.

Currently the list of points is obtained by a specified texture, and in future versions we will alow to edit your custom polygon.

New Joint2D features

Box2D has a number of Joints that can be used to connect two bodies together. These joints can be used to simulate interaction between objects to form hinges, pistons, ropes, wheels, pulleys, vehicles, chains, etc. Learning to use joints effectively helps to create a more engaging and interesting scene.

In previous WaveEngine versions, to specify joints with the Joints2DMap component, wich store the list of joints associated to the entity, now we’ve removed that way of work.

Now each Joint is a Component itself, and you can add several Joints to an Entity in the same way as Collider2D instances. We have reimplemented all Joint2D properties to be more usable in Wave Editor, and now is more straightforward to adjust anchors, angle limits and much more.

Here is the list of the current Joint2D supported by Wave Engine:

  • DistanceJoint2D: A point on each body will be kept ata fixed distance apart.
  • WeldJoint2D: Holds the bodies at the same orientation.
  • RevoluteJoint2D: A hinge or pin, where the bodies rotate about a common point.
  • RopeJoint2D: A point on each body will be constrained to a maximum distance apart.
  • PrismaticJoint2D: The relative rotation of the two bodies is fixed, and they can slide along an axis.
  • MouseJoint2D: Pulls a point on one body to a location in the world
  • WheelJoint2D: This joint is designed for vehicle suspensions and provides two degrees of freedom: translation along an axis fixed in body1 and rotation in the plane.

Performance boost

Box2D is a portable C++ library, so if your app makes an intense use of 2D physics will benefit of a performance boost compared with previous versions.

Because WaveEngine is a C# engine, we need to allow C++/C# interoperability. We’ve accomplished that task using SWIG (http://www.swig.org/).

C# wrapper using SWIG

The crossplatform mechanism that .Net platform allows to interop with C# is P/Invoke, that enables managed code to call native code.

In that point comes SWIG, a esplendid tool that helps the huge task of create all necessary P/Invoke calls and glue code to accomplish this.

As a result, we have in C# almost the same classes that is defined in C++ Box2D:

Leave a Reply

Your email address will not be published. Required fields are marked *