How to permanently set body's X position?

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…)

Thanks!

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.)

So, create an ‘anchor’:

local anchor = display.newGroup() anchor.x, anchor.y = -500, -500 physics.newBody( anchor, "static", { radius=10, sensor=true } )

Then, if you have a platform you want to keep in place:

local platform = display.newImage("platform.png") physics.addBody( platform, "dynamic" )

Weld the platform to the anchor:

local platformweld = physics.newJoint( "weld", anchor, platform, platform.x, platform.y )

If you want to move the platform, move the anchor. Of course, if you’ve welded other objects to the anchor they will move, too. So create the anchors you need.

Tip: If you’re going to do that a lot you might want to put the anchor at the same location as the platform, or your “hybrid” mode will get real freaky.

use a piston joint (axis 0,1; no motor)

I’ve built some demos for you to take a look at:

https://github.com/HoraceBury/HelpDemos

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.)

So, create an ‘anchor’:

local anchor = display.newGroup() anchor.x, anchor.y = -500, -500 physics.newBody( anchor, "static", { radius=10, sensor=true } )

Then, if you have a platform you want to keep in place:

local platform = display.newImage("platform.png") physics.addBody( platform, "dynamic" )

Weld the platform to the anchor:

local platformweld = physics.newJoint( "weld", anchor, platform, platform.x, platform.y )

If you want to move the platform, move the anchor. Of course, if you’ve welded other objects to the anchor they will move, too. So create the anchors you need.

Tip: If you’re going to do that a lot you might want to put the anchor at the same location as the platform, or your “hybrid” mode will get real freaky.

use a piston joint (axis 0,1; no motor)

I’ve built some demos for you to take a look at:

https://github.com/HoraceBury/HelpDemos