Tuesday, October 14, 2014

Thursday, August 28, 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?

    Example:
    http://forums.coronalabs.com/topic/48488-disable-the-y-axis/

    Description:

    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.

    Solution:

    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.
  • How can I group the physics bodies like I do with display objects?

    Example: http://forums.coronalabs.com/topic/50626-pin-one-object-to-another/

    Description:
    Many developers want to group many physics objects into a single display group so that they can move them all together. The physics engine uses the top level display group as the starting 0,0 point for all physics bodies, so trying to move a display object's parent group will not work, because 0,0 never moves.

    Solution:
    Grouping the display objects which have physics bodies attached into display groups is not a problem. You just can't move the parent groups at all. This is usually fine because moving a physics body should be done using a touch joint, which will cause any bodies attached by physics joints to move with it.

Wednesday, August 13, 2014

AutoMapper And LoaderExceptions Problems

When setting up a website in IIS6 with a bunch of NuGet packages installed and having only just pulled down a fresh copy of the code base from SVN I discovered a number of baffling problems. They all boiled down to the same basic issue.

Firstly, there was the "Retrieve the LoaderExceptions" message on the yellow screen of death. This was not helpful but the most I could discern was that the objects that AutoMapper was trying to instantiate were not accessible for some reason. Initially this looked like a database connectivity failure, but it turned out to be the wrong version of AutoMapper. Although the base project had the correct version of the DLL, one of the dependency projects was referencing the wrong DLL - all this through auto-update with NuGet. Here's what the exception looked like:


The second problem was the "Value cannot be null" which equates to the same problem - that one of the objects cannot be instantiated because AutoMapper is the wrong version and not compatible.


The last option is to try changing the App Pool in IIS if you see this:


Here are some references from StackOverflow:

  • http://stackoverflow.com/questions/8042493/could-not-load-file-or-assembly-automapper-or-one-of-its-dependencies
  • http://stackoverflow.com/questions/4667078/how-to-retrieve-the-loaderexception-property

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: