can i restrict physics bodies to only y motion?

so i have a column of blocks and i only want them to fall vertically (no horizontal motion, no rotation, no horizontal interaction with other bodies). even with mask bits and filters, i haven’t found any way that i can do this (aside from constantly monitoring velocity and setting it to 0.

i’ve gone with setting up border rectangles with mask bits around every column of blocks, but this seems unreasonable. is there a better way? thanks! [import]uid: 8791 topic_id: 2570 reply_id: 302570[/import]

i’ve also tried making the bodies kinematic and only applying vertical forces, but they still bounce horizontally [import]uid: 8791 topic_id: 2570 reply_id: 7350[/import]

I’ve been trying to solve this problem for some time. The route I took was to use piston joints. These (which are confusing at first, if this area is new to you) simply restrict the movement of a physics object to an axis. The axis itself is simply defined by having an anchor object and an x,y value in relation to the anchor. The trick is to use values between -10 and +10 when defining the axis, anything else and the math on the piston seems to explode.

While this will do what you want, unfortunately there are problems with pistons. I’ve posted about all of these in the game forum, but got very few answers, I’m afraid, so there may be no way around it. The problems involve, like you said, movement against the axis. Pistons allow this when an object interacts with another object, like bouncing against something or being pushed by an object under motion.

My solution (which also involves blocks dropping vertically) was simply to check if the blocks being made to collide with the vertically dropping blocks are “about to move to an area where the collision will happen.” What I mean by that is, essentially, that I check on touch events which blocks are likely to collide. I have allowed horizontal motion by use of touch events, but the vertical only blocks should never cause collision events because of my pre-checking.

I recently posted (another unanswered post) about using isFixedRotation=true in conjunction with a piston joint: http://developer.anscamobile.com/forum/2010/10/09/pistons-and-isfixedrotation-are-not-compatible

The brief summary of which is this: When using isFixedRotation=true (not the default false) on an object which is attached to a piston (but not as the anchor) the piston has no effect in restricting the object’s motion. Turn the isFixedRotation=false and suddenly the piston works again.

If you have any success, please please post here - I will be very, very, very happy to learn what you’ve discovered!

Best of luck,

Matt. [import]uid: 8271 topic_id: 2570 reply_id: 7359[/import]

Hey horacebury, have you had any luck with this?

I’ve posted and emailed ansca about this because with flash and cocos2d using box2d it actually stays restricted to the axis.

I just have to know why this doesnt work the same way with corona. If I’m doing something wrong, then thats great, I just need it to work. [import]uid: 10243 topic_id: 2570 reply_id: 19513[/import]

Well, in a word, no. Pistons seem to have a problem in Corona. The body property isFixedRotation cannot be set to true while a body is attached to a piston, or the piston will simply not have any affect at all. Also, pistons don’t force an object to stay on the axis. I had assumed this was natural to mathematical approximation, but if that is not true for flash, then I don’t know.

I have emailed and posted bug entries with Ansca, but it seems there is no luck. I’m sure they’ve seen my requests, but either they’ve got higher priority issues (being a dev man I can totally sympathise) or the implementation simply doesn’t allow for it. I would question the latter, however.

I don’t believe you are doing anything wrong as I don’t believe I was doing anything wrong, but there is no evidence to the contrary.

My solution, for my game, was to place 1px wide rectangular, static bodies with a collision filter set so that they only affect the blocks I needed keeping straight, instead of using pistons. I was then free to set isFixedRotation to true, as there was no joint fighting it.

Let me know what you find. For reference:

https://developer.anscamobile.com/issues/2634 [import]uid: 8271 topic_id: 2570 reply_id: 19515[/import]

It is definitely supposed to restrict it to one axis. It even says so in Corona’s documentation. Here are some examples.

http://www.actionsnippet.com/swfs/prismatic.html

With this example you can click on the circle and drag it only left and right because it is restricted to the x axis using a piston/prismatic joint.
http://blog.allanbishop.com/wp-content/uploads/2010/09/Box2DJointTutorial.swf

With this example click the right arrow until you get to prismatic joints and you will once again see that the block is restricted to one axis, the y axis in this case.

http://www.emanueleferonato.com/downloads/prismatic_joint.swf

Here is another example. Click and drag the box. It will only drag on the x axis because it is using a prismatic joint.

I understand about the mathematical approximation, especially while using the slower cpu of an Idevice, but I still can’t get it to work at all in corona, even in the simulator running on a core 2 duo 2.6ghz with 4 GB of ram.

I know prismatic joints fix the rotation automatically so that may be why the isFixedRotation = true was not working. But to me it is clear that the piston/prismatic joint is supposed to restrict movement to a single axis. [import]uid: 10243 topic_id: 2570 reply_id: 19524[/import]

When you say they restrict the rotation, do you mean isFixedRotation would be implicitly true in, say, flash?

[import]uid: 8271 topic_id: 2570 reply_id: 19528[/import]

Yes, from what I’ve seen using box2d with flash, cocos2d or anything else rotation is automatically fixed when using prismatic joints. But I may be missing exactly what your talking about horacebury, honestly I’m more interested in figuring out why I can’t get the piston joint to restrict to one axis like it is supposed too.

Here is some code to illustrate what I’m talking about with the axis. You can play with the numbers and add motors, but from what I’ve found there is no way to restrict the movement to one axis using piston/prismatic joints in corona. Hopefully I’m missing something because it works perfect using box2d with anything other than corona.

You can drag the boxes.

[code]
local physics = require(“physics”)
physics.start()
physics.setDrawMode(“debug”)

display.setStatusBar( display.HiddenStatusBar )

local function startDrag( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y

event.target.bodyType = “kinematic”

event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
event.target.bodyType = “dynamic”

end
end

return true
end

local bigBlock = display.newRect( 0, 0, 128, 128 )
bigBlock.x = 160; bigBlock.y = 0
physics.addBody( bigBlock, { density=5.0, friction=0.4, bounce=0.2 } )

local anchor = display.newRect( 0, 0, 32, 32 )
anchor.x = 160; anchor.y = 400
physics.addBody( anchor, “static” )

local block = display.newRect( 0, 0, 64, 64 )
block.x = 160; block.y = 300
physics.addBody( block, { density=3.0, friction=0.4, bounce=0.2 } )

bigBlock:addEventListener( “touch”, startDrag )
block:addEventListener( “touch”, startDrag )

myJoint = physics.newJoint( “piston”, block, anchor, anchor.x, anchor.y, 0,5 )
myJoint.isLimitEnabled = true
myJoint:setLimits(0,1)
[/code] [import]uid: 10243 topic_id: 2570 reply_id: 19534[/import]

Hey guys sorry to resurrect this post, but I was wondering the same thing. Did you come with a solution to restrict objects only in y ?? or to stop them from rotating…

thanks [import]uid: 74667 topic_id: 2570 reply_id: 125747[/import]

I have tried and piston joints work fine - there was a bug which was fixed some time ago. Restricting motion to a single axis is straightforward now. [import]uid: 8271 topic_id: 2570 reply_id: 125806[/import]

Hey guys sorry to resurrect this post, but I was wondering the same thing. Did you come with a solution to restrict objects only in y ?? or to stop them from rotating…

thanks [import]uid: 74667 topic_id: 2570 reply_id: 125747[/import]

I have tried and piston joints work fine - there was a bug which was fixed some time ago. Restricting motion to a single axis is straightforward now. [import]uid: 8271 topic_id: 2570 reply_id: 125806[/import]