Pistons

Is it possible to have a body moving up and down like a piston, but only within a certain area?

Explanation: I’d like a piston-like object to move up and down within a column, but not need to be tied to anything.

I’m playing with having invisible walls on either side of the piston object, but this seems a bit brute-force for something which should only move up and down.

Thanks,

Matt. [import]uid: 8271 topic_id: 2026 reply_id: 302026[/import]

Sorry guys, but I’ve been playing with pistons etc a lot recently, and all I can see is that the object moving along the piston axis is restricted to that axis unless something collides with it. Am I correct here and if I am, is there any way to force an object to stay on the axis?

My test of this is to have an object slide vertically down a piston axis until it hits a tilting platform. The object should stay in position on the platform while the platform tilts down, but it doesn’t - the object seems to drop off the end of the piston. [import]uid: 8271 topic_id: 2026 reply_id: 5987[/import]

I hate to keep posting about this, but I can’t seem to get one piston working in any kind of useful manner. Could someone post a small code sample showing how to use pistons/prismatics, please? All I get is a crashed simulator… [import]uid: 8271 topic_id: 2026 reply_id: 6036[/import]

Right, ok, I think I’m finally getting this. Must be the early mornings and excessive coffee.

It seems as though the axis x and y values can only range from -10 to 10. Anything outside that range, while it would initially look to represent a more specific angle, is actually invalid. So, to get an object to slide on the piston at an angle of 5 degrees below the horizontal, one would do:
function rotatePoint( point, degrees )
local x = point[1]
local y = point[2]

local theta = math.rad( degrees )

local xx = x * math.cos(theta) - y * math.sin(theta)
local yy = x * math.sin(theta) + y * math.cos(theta)

return { xx, yy }
end

local point = rotatePoint( { 10,0 }, 5 )

physics.newJoint( “piston”, one, two, one.x,one.y , point[1],point[2] )
[import]uid: 8271 topic_id: 2026 reply_id: 6037[/import]

Right. Here’s what I’ve found and I’d really, really like to know if I’m doing something wrong, not read the docs properly or if this is a bug in Corona…

It seems as though pistons only work if the anchor is an image.

Ok, let me caveat that: I have a piece of code which tries to create a piston using a display.newRect and a display.newImage. That fails by crashing the simulator. When I replace the display.newRect with a display.newImage it works.

Here’s my code (assume require physics, etc. above)…
local physprop = { density=0.0, friction=0.0, bounce=0.0 }
local img = display.newImage( “crate.png” )
img.x = 100

local anchor = display.newRect( 0,0 , 10,10 )
anchor.x = 100
anchor.y = display.contentHeight - 100
anchor:setFillColor( 50,50,50 )

local crate = physics.addBody( img, physprop )
img.linearDamping = 2

local block = {}
block[“img”] = img
block[“crate”] = crate
block[“physprop”] = physprop

local crate = display.newImage( “crate.png” )
crate.x = 100
crate.y = display.contentHeight - 200
physics.addBody( crate, “static”, physprop )

– local piston = physics.newJoint( “piston”, anchor, img, anchor.x,anchor.y , 0,5 ) – breaks!
local piston = physics.newJoint( “piston”, crate, img, crate.x,crate.y , 0,5 )
[import]uid: 8271 topic_id: 2026 reply_id: 6042[/import]

Just to close out this thread for the record: the problem in the above code was that the vector object “anchor” was never turned into a physics body. Thus, the attempt to make a joint with it failed.

The crash can be avoided by adding this line before the joint constructor:

physics.addBody( anchor, "static", physprop )  

In addition, the local variable “crate” was used twice, but that wasn’t the source of the crash. [import]uid: 3007 topic_id: 2026 reply_id: 13138[/import]

Thanks for the help guys :slight_smile:

matt [import]uid: 8271 topic_id: 2026 reply_id: 13173[/import]