Draggable dynamic physics bodies

So for my game I am forced to use dynamic bodies for my draggable objects. So to do this I am attaching a static sensor body to that dynamic body, so that gravity does not affect it. Reason for doing this is because I want those objects to register with the collision listener so the user cannot overlap physics objects, a kinematic to kinematic collision doesnt register.
The issue is that group object shows its being move on screen, but when I actually test it(in this case throw a ball at it) the physics bodies are still in their initial position. Even though debug draw mode shows it being moved around screen.

I hope this is understandable, I believe it has something to do with sending a group to the touch listener.

Thanks

Could you post up some code to show exactly what you are doing please?

nevermind, i fixed this issue by creating the anchor and joint in the touch event. I've run into some other problems now. If only kinematic bodies registered a collision event, it would save me a lot of headaches.

Ok here is some test code ive been playing around with, just to take the distraction of the game out of the equation and just focus on this issue. All I am trying to do is move the boxes around, but if they overlap I want them to snap back to the last valid position after the user ends touch.
Of course my game will have much more complex bodies than squares, this is just an example.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local physics = require "physics"
physics.start()
physics.setGravity(0,10)
physics.setDrawMode("hybrid")
 
local arguments =
{
        { x=50, y=10, w=100, h=100, r=10, red=255, green=0, blue=128 },
        { x=50, y=150, w=100, h=100, r=10, red=0, green=128, blue=255 },
        { x=50, y=300, w=100, h=100, r=10, red=255, green=255, blue=0 }
}
 
local function onLocalCollision( self, event )
        if ( event.phase == "began" ) then
                        self.isPositionLegal = false
                        print("began")
        elseif ( event.phase == "ended" ) then
                        self.isPositionLegal = true
                        print("ended")
        end
    return true
end
 
local function onTouch( event )
        local t = event.target
        
 
        local phase = event.phase
        if "began" == phase then
                -- Make target the top-most object
                local parent = t.parent
                parent:insert( t )
                display.getCurrentStage():setFocus( t )
 
                -- Spurious events can be sent to the target, e.g. the user presses 
                -- elsewhere on the screen and then moves the finger over the target.
                -- To prevent this, we add this flag. Only when it's true will "move"
                -- events be sent to the target.
                t.isFocus = true
 
                -- Store initial position
                t.x0 = event.x - t.x
                t.y0 = event.y - t.y
        elseif t.isFocus then
                if "moved" == phase then
                        -- Make object move (we subtract t.x0,t.y0 so that moves are
                        -- relative to initial grab point, rather than object "snapping").
                        t.x = event.x - t.x0
                        t.y = event.y - t.y0
                        
                        --if(t.isPositionLegal) then
                        --t.validX = t.x
                        --t.validY = t.y
                        --end
                elseif "ended" == phase or "cancelled" == phase then
                        --t.x = t.validX
                        --t.y = t.validY
                        display.getCurrentStage():setFocus( nil )
                        t.isFocus = false
                end
        end
 
        -- Important to return true. This tells the system that the event
        -- should not be propagated to listeners of any objects underneath.
        return true
end
 
-- Iterate through arguments array and create rounded rects (vector objects) for each item
for _,item in ipairs( arguments ) do
        local button = display.newRoundedRect( item.x, item.y, item.w, item.h, item.r )
        button.isPositionLegal = true
        button.validX = item.x
        button.validY = item.y
        button:setFillColor( item.red, item.green, item.blue )
        button.strokeWidth = 6
        button:setStrokeColor( 200,200,200,255 )
        physics.addBody(button, "dynamic")
 
        
        local anchor = display.newRect(item.x,item.y,item.w,item.h)
        anchor.isVisible = true
        physics.addBody(anchor, "static", {isSensor = true})
        physics.newJoint( "weld", button,anchor, anchor.x,anchor.y )    
        local group = display.newGroup()
        group:insert(button)
        group:insert(anchor)
        group:addEventListener( "touch", onTouch )
        group.collision = onLocalCollision
        group:addEventListener("collision", group)
        
end
views:1538 update:2011/11/27 10:14:33
corona forums © 2003-2011