Touch movement and collision

Hi guys
can you help me to solve the following problem for my first puzzle game?
in the scene there are actors of various shapes and all stuck together, like tetris (square, rectangle, L shape etc ), all of them can move into the scene dragging them with the finger (touch movement).

I would like that when an actor touch another one, the movement directed to the collision is disabled, and left free in other directions.
I would like that the actors can fit together and that the movement is only allowed to the free of obstacles directions.

Thank you very much for your help

(Sorry for my english!) [import]uid: 25437 topic_id: 15218 reply_id: 315218[/import]

You would give your objects custom shapes when defining their physical properties.

Once a block is in it’s final position you’d change it to “static”, this would mean if you had another block pressing against it, it could not move that way.

Does that make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 15218 reply_id: 56251[/import]

try this
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0,0)

local circle = display.newCircle(100,100,20)
physics.addBody(circle,“static”)

local rect = display.newRect(50,50,20,20)
physics.addBody(rect,“static”)

local function moveObject( event )
local t = event.target
local phase = event.phase
if “began” == phase then
t.bodyType = “dynamic”
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store touch position on object
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase then
t.bodyType = “static”
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
circle:addEventListener(“touch”,moveObject)
rect:addEventListener(“touch”,moveObject)[/lua] [import]uid: 71210 topic_id: 15218 reply_id: 56265[/import]

Thanks to all of the answers.
As I have suggested works, but if I try to force the movement of the piece above the static block I still can get over it.
Instead I would like to be disabled in the movement direction of the collision.

PS: the movement of the blocks (red) will be only horizontal and vertical and will use a kind of grid. (see example of the game)

https://picasaweb.google.com/103072110993094844433/18Settembre2011?authuser=0&feat=directlink

Other ideas? [import]uid: 25437 topic_id: 15218 reply_id: 56426[/import]

Can anyone help me? [import]uid: 25437 topic_id: 15218 reply_id: 56669[/import]

let me try… will get back to you… :slight_smile: [import]uid: 71210 topic_id: 15218 reply_id: 56672[/import]

just a quick check ? what will be the shape of your objects ? square, rectangle and L ? [import]uid: 71210 topic_id: 15218 reply_id: 56681[/import]

I have tried to create a grid logic for your requirement. now I may need to know the shapes your objects will be of. then we need to map the grid columns which are occupied. and privent movement of object to occupied column.

this is what I reached so far…
[lua]local rect1,rect2,rect3

local function moveObject( 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 )
t.isFocus = true
elseif t.isFocus then
if “moved” == phase then
local gridx = math.floor(event.x/50)
local gridy = math.floor(event.y/50)
local gridxpos = gridx * 50
local gridypos = gridy * 50
if gridx > -1 and gridx < 5 and gridy > -1 and gridy < 9 then
t.x = gridxpos
t.y = gridypos
end
elseif “ended” == phase then
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end

local line1,line2
local x = 50
for i=1,10 do
line1 = display.newLine(0,x,320,x)
line2 = display.newLine(x,0,x,480)
line1.alpha = .2
line2.alpha = .2
x=x+50
end

rect1 = display.newRect(0,0,100,50)
rect1:setReferencePoint(display.TopLeftReferencePoint)
rect1:addEventListener(“touch”,moveObject)

rect2 = display.newRect(150,150,100,50)
rect2:setReferencePoint(display.TopLeftReferencePoint)
rect2:addEventListener(“touch”,moveObject)

rect3 = display.newRect(200,200,100,50)
rect3:setReferencePoint(display.TopLeftReferencePoint)
rect3:addEventListener(“touch”,moveObject)[/lua] [import]uid: 71210 topic_id: 15218 reply_id: 56682[/import]

yes, and other shapes. Have you seen the image posted in the previous link?
I’ve read your last post… Thank you very much for your help [import]uid: 25437 topic_id: 15218 reply_id: 56684[/import]