Solutions to Common Physics Challenges
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, such as Bullet, 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.
Nice tutorial.
I have however a point about “sticking to walls” / also other objects.
That is due to the elastic collisions velocity threshold from Box2D, which is not exposed to the Corona API, and all the tricks above will not work since increasing bounce, decreasing friction has as result objects moving at very high speeds, higher than the normal “simulation”.
https://developer.anscamobile.com/forum/2010/11/10/expose-box2d-velocitythreshold-parameter-help-sticking-objects is the thread where i’ve been asking for quite some while now about exposing at least this flag to the API.
So if you really wanna help solve those physics problems, expose the flags from the Box2D config which affect the world and the collisions to the Corona API. Then, and only then, all the things you mentioned above will work correctly.
Alex
Thanks for this post! Just to add, another potential for the “Objects are sticking to walls” is to set isBullet to true for the object. It worked for me in a situation where a ball was “gliding” down a box even though it should clearly have bounced off. Whenever isBullet was set to true, the bouncing then worked.
Hi,
I just wrote a blog post that can help with the sticky object problem, at least in some cases:
http://www.ludicroussoftware.com/blog/2011/09/01/corona-physics-forced-bouncing/
Another post will be forthcoming to deal with objects that aren’t horizontal/vertical.
Thanks,
Darren
Nothing really works for me, but I have some things that REALLY improve it.
First the obvious stuff that people say to try, but I find to be lies!
physics.setPositionIterations(128)
physics.setVelocityIterations( 128)
physics.setDrawMode( “normal”)
physics.start(noSleep)
physics.setGravity( 0,10)
then adding box.isBullet etc.
I’ve even set up the iterations to crazy numbers like 512, 1024 and higher with little improvements.
What works better for me (in addition to the iterations):
The only thing that seems to help is to set the gravity really high like 80, then set density to something really light at .01.
Also, I set a newRect at 500×20, then place a smaller one inside that one thats 500×10 (so it’s a new shape within a shape). That creates a “buffer” so the physics object hits and tries to go through the first shape, but then catches by the second one.
The problem you could imagine is if you already have 20,000 vertices like I do, the thought of having double that is I N S A N E!
I’ve really had to think about this and tear apart Box2d and how it ticks.
Pain in my ass for sure, but it’s really hindering my release where I am considering making other games in Corona SDK, but not this type of game which I may have to go to something like Unity or something (I haven’t crossed that bridge though!).
-Nick
@cellphonegaming
Ok, I’m starting to get some improvement
http://www.gamedev.net/blog/1350/entry-2253939-intelligent-2d-collision-and-pixel-perfect-precision/
This link just popped up things to iNSERTCODE.
In addition to what I posted previously, I also used
body.angularDamping = 15
body.linearDamping = 25
body.angularVelocity = 10
body.isBullet = true
The problem isn’t that an object is hitting another object, slowing down and penetrating it. It’s that it’s almost like Box2d engine just forgets for a second, or something and the object just goes clean through the wall. Sometimes this happens at slow speeds! It’s a big case of WTF and it pisses me off to no end.
So now, thanks to craptastic box2d math rounding, we as devs have to invent crazy and weird ways to offset it. I’ve had to introduce a ton of overhead, doing pre collision checks, frame by frame checks, math stepping to offset rounding etc. It has resulted in degradation in performance.
I wish Box2d had an option for precise math AND rounding that way a user could choose and pick the best scenario instead of forcing us to use this! GRRR!
ng
how to start creating a grappling hook ?
thanks