Welcome!

Think Labs is an ongoing effort by Seven2 to provide research and educational opportunities in the web development and mobile field. To see what we’ve been cookin’ up, check out our blog postings.

Created by
Seven2 Login

Categories
Tags
Team Think Labs | Solving common physics problems with Box2D
757
single,single-post,postid-757,single-format-standard,ajax_fade,page_not_loaded,,,wpb-js-composer js-comp-ver-4.2.3,vc_responsive
1958- tennis for two

Solving common physics problems with Box2D

As early as 1958 when tennis for two’ was introduced to the world, physics has been instrumental in the evolution of video games. Physics simulation can be divided into two simple components: The physics engine that attempts to simulate Newtonian Physics (motion), and collision detection that resolves a collision between two bodies.

1958- tennis for two

Box2D is the world leader with 2D physics simulation, and for good reason: speed, capability and stability. It first gained popularity in Flash development, but quickly spread to mobile platforms. The Corona SDK and mobile heavyweight Angry Birds both use Box2D, and it was our natural choice when developing Flickitty for iPhone and Android. This article will explain how we solved some common problems with physics simulation, and how the hero, Flick, interacts with the world.

this is Flick

Object Specific Gravity using FORCE

Box2D ships with a gravity property. However, FORCE can also be used to simulate gravity. FORCE is a simple vector that moves an object in a constant and specific direction. By modifying the FORCE of each object in the world, each object can have gravity that is independent of the world.

Dragging and Throwing Objects  with Velocity

Motion is simulated with VELOCITY, and unlike FORCE, it is only a temporary modifier. While FORCE continues to push an object in a constant direction, VELOCITY attempts to move an object in another direction. Imagine throwing a ball- as it leaves your hand at a high velocity, that velocity gradually decays over time until the ball succumbs to gravity and falls back to the earth. In order to drag an object, FORCE is dropped to a zero value, and VELOCITY is applied in the direction of the drag vector.

dragging an object

Mass is Mass, No Matter How Small

All physics objects in the world have MASS. An object with zero mass cannot move, no matter how much FORCE or VELOCITY is applied. This is how objects like the wood plank and steel beam are able to hold their position in mid-air: their MASS is set to zero. Although Flicks tail is very small compared to his body, each tail segment contains a tiny amount of mass (0.0001) in order to keep it moving. A larger number would throw off his balance and interfere with his normal motion.

Temporary Joints and Binding

The most common usage of JOINTS fuses two or more physics objects together. Flicks tail and most of his body is constructed using JOINTS to join his body parts. However, joints can also be used to fuse Flick to another object; without this joint he would slip and fall off, no matter how high the FRICTION is set. This temporary joint allows Flick to ride an object without falling off. He can even hang upside down from an object.

Special Events

While Box2D handles things like motion and collision very well, it is the EVENTS that make Box2D truly flexible. When two objects collide, Box2D fires off an event notifying us that a collision occurred, allowing us the opportunity to do something special with that EVENT. Should we explode the object? Make a sound? Throw sparks? Pick up a coin? All of that is handled with your own functions.

shattering rocks

Ignoring Some Collisions

What happens if we want a collision to occur with one object, but not another? Box2D uses COLLIDEGROUP and COLLIDEMASK to determine if a collision can occur at all. Objects like the steel beam can be dragged through walls and repositioned without a collision, but the beam will trigger a collision with all other objects in the world. Coins ignore collision with all objects in the world, except for Flick, allowing him to pick them up. Hazards like the Gun Turret become deadly as the bullets only collide with Flick, while ignoring collisions with walls and objects.

coin collecting

Because of Box2D’s massive popularity and support on various platforms, documentation, examples and code snippets can be easily found online. Video of Flickitty and the physics in action