Posted on by

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 . Thanks for reading...

15 Responses to “Solutions to Common Physics Challenges”

  1. nexendk

    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

    Reply
  2. Philipp Lenssen

    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.

    Reply
  3. Nick

    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

    Reply
  4. nick

    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

    Reply
    • Jerry Palmroos

      Yes – this situation is very bad. One other side effect: the

      body.getLinearVelocity function goes haywire.

      In my situation (landing a rocket inside a mountain crater), the ship sticks, and the velocity goes UP (to double digits).

      Have tried every recommended method on this board:

      Lower the Friction
      Raise Bounce
      Raise Bounce
      Force Bounce on Collision
      body.isBullet = true
      Set Gravity High, Density Low
      Build a Second Object inside the Wall Structure

      NONE of these worked. WTF?!!!

      Seriously bummed here.
      Jerry Palmroos
      Charlotte

      Reply
  5. nick g

    I fixed most of my issues. I started using Gideros Studio. I’m still using corona and I prefer it’s ease of use but I can’t explain how the hell it works so oddball in corona and fine in Gideros.

    If I run a sample by itself it works well. If I run it with Director it runs pretty good, and in storyboard it runs OK. It’s so all over the place, I can’t trust it to release to production. It varies too, sometimes I get blatant passthrough of objects on build 840, but If I go back to an older stable build it works fine. Sometimes the daily builds work…….meh.

    I’m a ways off from releasing, I’ll keep at it on both fronts :)

    Crazy times.

    Reply
  6. Nick G

    Ok, so I responded a couple of days ago about the issues I was having. Seems some things are working better now that the latest release 894 is using Box2d 2.2.1. I don’t know exactly why that is, but I’m getting issues rarely now vs the last stable build of 840.

    Crazy.

    Reply
  7. Nick G

    I still say build 894 is the game changer and what made me stick with Corona. I almost abandoned it due to the box2d being a version behind. The collision issues I was having were just stupid and I wasted months trying to fix it, when it could have been just updated. I understand there is a process to test it and all that, but it was frustrating to say the least.

    At least now, it’s happy times :)

    ng

    Reply
  8. Underbeliever

    So, does this now work for you guys?

    I’m using build 947 and objects still stick to the walls like crazy. And i’ve no idea what to do about it.
    I have a box of four walls and a dozen balls inside. Randomly tossing them about results in all but one ball sticking to a wall..

    Have any of you fixed this issue at all, or is it simply here to stay?
    Thank you for any help you might offer!

    Reply
    • Brent Sorrentino

      Hello @Underbeliever,
      Is your project simple enough that you could zip it into a file and send it to me? Or perhaps you could record it to a video using a screen-recording utility like QuickTime Player or a Windows equivalent, so I could see what’s going on? I can’t promise that I’ll have some amazing insight on it, but if I see the project I might be able to pinpoint something.

      Reply
  9. Tim

    I seem to be having the opposite problem as everyone else. My objects at repelling each other like crazy and I don’t know why. Is there some basic setting I’m missing that would cause objects to never even get within 100+ pixels of each other without bouncing away??
    I’ve tried adding the body with all setting at default or changing them wildly and the same effect remains no matter what. global physics settings are super-basic also.

    Globals are set to:
    physics.start()
    physics.setGravity( 0, 10 )
    physics.setScale( 30 )
    physics.setDrawMode( “normal” )

    And the add body is set to:
    physics.addBody( target, “dynamic”, { density=100, bounce=0.0, friction=0.3 } )

    and I’ve tried it as simple as:
    physics.addBody( target, { } )

    all with same results. crazy repelling of each other.

    Reply
    • Brent Sorrentino

      Hi Tim,
      Can you post a bit more of your specific code, i.e. what “target” looks like, the shape of your physics bodies (or auto, if you’re just doing that). Everything in your code above looks basically correct, but there must be some little thing causing this strange behavior…

      Thanks,
      Brent

      Reply
  10. Will

    Hell all. I am pretty new to Corona but I have been doing a lot of tutorials and I think I am getting the hang of this.

    My question deals with transition.to I am using this to move a character around on screen by touching an area and then the character walks to that location. I have the gravity set to ( 0, 0.2 )

    My issue is that my character will walk through static objects? If I let the character sit, and thus be affected by gravity, it then will be blocked by static objects.

    Does anyone know how to make an object that is being moved via the transition.to
    respect the physical space of static objects?

    Thank you kindly!

    Reply

Leave a Reply

  • (Will Not Be Published)