jfpilch

[GLOBAL: userInfoPane.html]

jfpilch

Observer

1 posts

Corona SDK

09 February 2019 - 10:33 AM

I am new to Corona, and struggling to figure out the following: I have a dynamic body that I would like to be able to move along the Y axis, but I would like to fix its X position, effectively preventing it from moving along the X axis.

Is there a smart way for me to achieve this? (I assumed resetting X frequently sounds like a terrible idea...)

horacebury

[GLOBAL: userInfoPane.html]

horacebury

Corona Geek

3,059 posts

Corona SDK

11 February 2019 - 06:43 AM

If you try to set the .x, .y or .rotation values of physics objects you will be fighting the physics engine. This is a bad idea because it will win. That will take the form of perhaps removing objects from the visible scene but without their variables disappearing. Any way it happens, it's bad.

The only physics object you can directly affect without definitely negatively affecting the objects are static objects, because the engine essentially doesn't care.

If you want to fix a dynamic object in place you have a number of options, but my preference is to create an offscreen, static sensor. Any dynamic object is then fixed to that static object with a weld joint. This works surprisingly well and has the unexpected benefit of making effects like two objects bouncing off each other more realistic. This is because you have two dynamic bodies rebounding from each other than one dynamic rebounding from a static object - which is the equivalent of hitting an immovable object (the bounce tends to be exaggerated.)