Tuesday, June 10, 2014

Fighting The Physics Engine

I've seen way too many posts on the forums where a developer asks about the best way to control the absolute position of a dynamic physics object when using Box2D. This lead me to thinking that there should be a catch-all place for someone to read the right answer and as I've answered these questions on the forums many times, I'll start logging them here. Should have done this a long time ago.

  • How do I control the position of an object in Box2D?



    Dynamic objects in the Box2D physics engine are under the constant control (at least, while not asleep) of the physics engine. If you attempt to change their .x or .y values and move them with your own code you are trying to force the engine to accept a different world position for it's own, managed objects. Because of this change, sometimes it's own internal values will effectively break during computation and the result is that, in Corona SDK, the display objects will be lost from their physics object.
    In short, forcing the physics engine to accept different positions for it's bodies is fighting the engine for control and it will break.


    The way to control the position of physics bodies is to use the engine to position the them itself. This can be done by attaching a touch joint to the body and controlling the position of the joint and thus the position of the body.
    It has been mentioned that static bodies can be used to absolutely position dynamic bodies, via a weld joint, but this is similarly inadvisable due to the the reasons above.

Friday, April 11, 2014

Improved Slicing

The previous post showed cutting an object with lines and a curve. I've worked the code and can now slice an image into many pieces.

As before, if you would like this for your own projects, please get in touch on the CoronaLabs forums - user Horacebury.

Friday, March 21, 2014

Polygon Slicing With Corona SDK

As we all know by now, Corona SDK includes the fantastic 2D physics engine Box2D. Play with this for long enough and knowing how to manipulate polygons becomes very useful indeed. I've wanted to implement everything from Angry Birds to iSlash, so it was awesome to see Corona Labs include the new display.newOutline() function in a recent build.

This cool function basically takes a PNG with web transparency and returns a table of x,y coordinates for the circumference polygon of the visible pixels in the PNG. That polygon can then be plugged into a new constructor parameter of the physics.addBody() function to actually create a physics body against an image for that image's outline.

I wanted to take that further, ala iSlash, and decided to produce a library which would take the outline polygon and the end points of a line and separate the polygon into sliced parts. I won't go into the painful details of trying to dig up the code from the internet but suffice to say that code I found was either unintelligable enough to use or simply a mass of maths that I couldn't fathom.

Fast forward a few days and having tried at least 3 different methods I finally hit upon a winner. Here's the proof, running on Mac Corona simulator...

[View full size video]

As you can see, the intersection code works both for a single line cutting straight through the object at any any angle and even re-entering the polygon multiple times. It also handles a hand-drawn curve which, likewise, can start anywhere and enter/exit the shape as often as required. This allows quite a powerful set of options ranging from effects seen in iSlash to cutting shapes into unordered pieces.

I won't be posting the code this time as I fully intend to use this in my own productions. If you would like to implement this in your own production please get in touch via the Corona forums or on twitter: @horacebury

Friday, March 14, 2014

Corona Sample Code And Libraries

Here you can find my most recent Corona SDK samples, demos and libraries. I decided to create a new post because the date on the last one is really quite old and many things have changed in Corona SDK since I started it. You can consider everything here as compatible with Corona since the great Graphics 2.0 update of late 2013.

You can find my Code Exchange posts here:
They are all submitted to github Gists:

Thursday, June 27, 2013

Angry Birds in 30 Mins

It's a bit old now, but the proof still stands and this is a very entertaining video: Seb Lee-Delisle teaching an audience how to code up a simple Angry Birds in 30 mins with CoronaLabs' CoronaSDK.

You can follow Seb on Twitter: https://twitter.com/seb_ly and browse his internet wanderings: http://seb.ly/

Tuesday, January 01, 2013

Sample Code

Bunch of sample codes I've written for use in lua on the Corona SDK, utility and math libraries.
Some of these may appear in both lists due to being a library within a demo.

I have now added a third list of other people's libraries as they are proving quite useful.



Sample/Demo code:


Friday, November 30, 2012