As you already know, Corona includes the powerful Box2D physics engine to give your games the ultimate sense of depth by providing a high-quality, realistic physics simulation — all without you having to know much more than just the “common sense” aspects of physics.
Generally, if you know that gravity makes things fall, and understand that heavier objects travel slower than lighter objects when an equal amount of force is applied, then your knowledge of physics is sufficient enough to make a great physics-based game using Corona. Piece of cake right?
However, because there are several properties that affect how things behave—things we never really have to think about in the real world—sometimes your in-game physics don’t always behave exactly how you expect them to, or want them to.
This blog post will introduce some common “problems” that are brought up in relation to physics in Corona, and will attempt to help you narrow down the right properties/settings to change in hopes of allowing you to finally get things to behave as you want them to.
Objects are moving too slowly, or too fast
There are a number of factors that can affect how quickly objects react to gravity (as well as other physical forces).
I recommend playing with the density parameter when you create your physics body. Density affects how “heavy” things are, so if you are applying force, this property needs to be considered.
Another thing you’ll want to take a look at is physics.setScale(). This will not change anything visually, but will greatly affect the behavior of your physical objects—in particular, make things move slower/faster depending on how low/high the setting is. The default value is 30.
Objects are going through walls
A relatively common problem I’m seeing people have is that at times, their dynamic objects are passing through static walls, particularly when the object is traveling at a high speed.
There are several factors that play into this, so I’ll list all of the different things that you may want to play with, also in conjunction with the other items, to overcome your problem:
- body.isBullet – This property will cause a body to be subject to continuous collisions, so it reduces the chances of missing a collision when traveling at a high speed.
- Play around with the density property, which affects how “heavy” an object is. While changing this probably wouldn’t fix your problem outright, it could be a factor that plays a big part.
- I’ve ran into a case where a static body was just too thin, and making it a little larger fixed the problem of dynamic objects passing through it. Of course, this meant I also had to make my image resource larger, which may not be an option for your project.
- The physics.setScale() property greatly affects how physical bodies react to the environment, and with eachother. Try playing around with this, as well as in conjunction with the other properties, to see if it fixes your problem.
- physics.setVelocityIterations() and physics.setPositionIterations() determine how often position and velocity calculations are computed. The higher the value, the more computational overhead it takes, however, setting this to just the right threshold could potentially give you the solution you’ve been looking for.
Objects are sticking to walls
Another challenge people are running into is that sometimes, slow-moving objects stick to walls or other objects that they should be bouncing off of. While it’s impossible to tell you exactly how to fix your problem in particular, here are some things you can look into:
- Try adjusting the friction and bounce properties of your physics bodies. Since friction affects how “sticky” a surface is, and bounce affects how “bouncy” an object is, you might try playing with the bounce property of the moving object, and the friction property of the static object to see if that helps solve your problem.
- Mentioned as a proposed solution to the previous problem, the physics.setVelocityIterations() and physics.setPositionIterations() functions might also help prevent objects from “sticking” to others unintentionally.
- Finally, as a last resort, if tweaking properties and changing global physics settings doesn’t work for you, you could always add a collision listener to one of the objects, and apply a small amount of force (in the opposite direction of travel, to simulate a bounce) whenever a collision occurs. Of course, this is a workaround, so we highly recommend tweaking properties and settings first.
Don’t be reluctant to experiment!
Of course, the above was only a small handful of the most common issues people are having when it comes to getting things to behave how they want, but there are more. The best thing you could do to overcome these challenges is to strive to get a deeper understanding of how Box2D physics in Corona works.
To gain this understanding, I recommend opening one of the “Physics” sample code projects and play around with physics.setScale() to see how it affects the simulation. Additionally, play with the objects’ density, bounce, and other properties/functions (such as friction and physics.setVelocityIterations()) to get an even better understanding of how the various properties can affect your physical game world.
Posted by Jonathan Beebe. Thanks for reading...