"Gates" or Doors

I want to have doors in my game which slide on a timer, user tap or whatever. Either way, they need to be physics objects and they need to open and close via sliding. Obviously, I can't just translate their X and Y values (because that would be fighting the physics engine) but I'm not sure how to get them to move otherwise.

My initial investigation (proof) uses the touch joint and piston joint method to control the axis and range of motion of the door, with a touch joint being used not by the user but by an internal mechanism.

I'd really like to get other developer's input and ideas on this, please?....

In my proof code, below, I'm using either a tap or touch on the device to set the touch joint position. Before that happens the "door" (blue square) falls with gravity (initially set towards the left) but after that the door is held in place by the touch joint...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
local physics = require("physics")
 
-- turn status bar off
display.setStatusBar( display.HiddenStatusBar )
 
math.randomseed( os.time() )
 
-- start physics
physics.start()
physics.setGravity( 0, 0 )
physics.setDrawMode( "normal" ) -- normal, hybrid, debug
 
local defaultrectbody = { friction=0, bounce=0, density=0 }
local defaultroundbody = { friction=0, bounce=0, density=0, radius=5 }
 
local anchor = display.newCircle( 20, 20, 5 )
physics.addBody( anchor, "static", defaultroundbody )
 
local white = display.newRect( 100, 100, 300, 100 )
physics.addBody( white, "dynamic", defaultbody )
local weld = physics.newJoint( "weld", anchor, white, anchor.x, anchor.y )
 
local blue = display.newRect( 100, 100, 100, 100 )
blue:setFillColor( 0, 0, 255 )
physics.addBody( blue, "dynamic", defaultbody )
local piston = physics.newJoint( "piston", white, blue, white.x,white.y, 10, 0 )
piston.isLimitEnabled = true
piston:setLimits( 0, 200 )
 
local touch = display.newCircle( 150, 150, 20 )
touch:setFillColor( 255, 0, 0 )
 
function createTouchJoint()
        if (not touch.joint) then
                touch.joint = physics.newJoint( "touch", blue, blue.x, blue.y )
        end
end
 
function tap( event )
        createTouchJoint()
        touch.x, touch.y = event.x, event.y
        touch.joint:setTarget( touch.x, touch.y )
        return true
end
Runtime:addEventListener( "tap", tap )
 
function drag( event )
        createTouchJoint()
        touch.x, touch.y = event.x, event.y
        touch.joint:setTarget( touch.x, touch.y )
        return true
end
Runtime:addEventListener( "touch", drag )

just make them static and move with transition.to
or .isBodyAwake = false and then move it like you want it to

I've tried transition.to with static objects before, but ended up with errors. Are you saying that would not happen with isAwake=false ? The problem I see here is that I still need other objects to interact with the door while it is moving.

What errors are you running into with using "static"? I just ran a simple test and I was able to use transition.to on it error free, and it still collided with the dynamic object dropped on top of it.

I found that over time it generated errors - test code was fine, but not something to be relied on in production. Errors ranged from basic exceptions to some objects simply not being there any more suddenly. It was all very surreal.

personally i do not have any problems with static bodies
there's still plenty of ways to do what you need with dynamic bodies, like runtime function or applying constant force or whatever, try it, test it, experiment )

I will try it with some property changes, however I do know that using explicit changes to object values is fighting against the physics engine and this can cause spurious errors.

views:1468 update:2012/1/3 13:02:13
corona forums © 2003-2011