Disable horizontal movement of a certain body

I have a physics game with gravity and most bodies should freely move in all directions. For one specific body, however, I would like to disable any horizontal. So it should fall, but never be kicked to the side by another body or slide down on a diagonal slide.

How can I achieve this? I can get only so far with setting linearDampings (which also has the side unfortunate effect of really slowing down vertical movement too), setting high density and friction, or constantly inverting speedX in self:setLinearVelocity(-speedX, speedY)…

Thanks!! [import]uid: 10284 topic_id: 5997 reply_id: 305997[/import]

> I would like to disable any horizontal

Make that: " I would like to disable any horizontal movement". (any way to edit a post once submitted?) [import]uid: 10284 topic_id: 5997 reply_id: 20544[/import]

In Box2D, you would use a prismatic joint. In Corona terms, this is a piston joint (http://developer.anscamobile.com/content/game-edition-physics-joints#Piston_joint) however, there is an issue concerning certain types of joints you may wish to keep track of: https://developer.anscamobile.com/forum/2010/10/30/pivot-joints-and-apparent-elasticity

The core issue here is that certain joints are not performing as they, very reliably, do in other Box2D-based systems (eg: Flash, Cocos2D)

So, for the time being (that is, until a bug fix is found, mostly likely) your solution will have to be to have a function called by the enterFrame event which adjusts the x value of your object.

It’s a little brute force (ok, extremely) but the only hard and fast solution.

Correction… (not an edit)

Ok, I still stand by what I just wrote, but the solution I found in my game (due to piston joints and the isFixedRotation property cancelling each other out) is to create walls either side of the object being restricted in movement.

In my case I want to stop blocks from shifting sideways, so I’ve got a 1px invisible wall either side of each block, static and with no friction, bounce, etc.

A modification of this could be to have a circle body fixed (welded or pivot’ed) to the overall body being restricted and place the restricting bodies around the smaller piece. Like a runner on a drawer.

Unfortunately, excessive forces on the larger body will pull it away from the ‘runner’ object - no matter the strength of the joint between the two (see the issue in the link above) so I would go with my ‘restricting walls either side’ solution, if I were you.

Hope this helps.

Matt. [import]uid: 8271 topic_id: 5997 reply_id: 20594[/import]

if the object has a “enterframe” event just do this:

object.x = 100 – replace 100 by your coordinate.

And that’s it.

Your object will be follow the gravity but only in the Y direction. [import]uid: 9975 topic_id: 5997 reply_id: 20665[/import]

Thanks so much for the answers!! [import]uid: 10284 topic_id: 5997 reply_id: 20727[/import]