[Resolved] Can collisions occur with image and rectangles?

I am fairly new to corona (and lua), and I am having trouble making an event happen when a image collies with a rectangle (made in the coding). I follow the samples, the tutorials, reference youtube etc and I cant seem to make this work

Question is if that’s possible?

Thanks,
Hansel L. [import]uid: 123133 topic_id: 22616 reply_id: 322616[/import]

Any display object should be able to collide with any other display object. I’m assuming you’re using physics and if that’s the case, there are a few things you need to be aware of.

  1. All display objects need to also be physics bodies (physics.addBody())
  2. If you’re putting them into groups, objects can only collide with other objects in the same group.
  3. Only dynamic objects can collide with other objects. In other words, two static objects won’t collide with each other. So at least one of the two objects have to be dynamic.

Perhaps if you posted your code, that might help us pin-point your problem.
[import]uid: 19626 topic_id: 22616 reply_id: 90197[/import]

well, there not technically dynamic, they are static but draggable. Im using similar code as the tutorial posted from Peach.
frame.myName = “framec”
pic1.myName = “pic1”

pic1:addEventListener(“collision”, pic1)

function pic1:collision (event)
if event.other.myName == “frame” then
pic1.x = frame.x
pic1.y = frame.y
end
end
The usage is: When the player drags “pic1” over to “frame”, pic1 automatcly snaps in place with the frame. [import]uid: 123133 topic_id: 22616 reply_id: 90241[/import]

help? [import]uid: 123133 topic_id: 22616 reply_id: 90543[/import]

You did not provide enough code. Where are the objects being defined?
And from looking at what you’re doing, Collisions with Physics is probably not your best answer.

See:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

for some sample code that doesn’t use physics. In your case, instead of using an event listener on the enterFrame event, you would include the test to see if the objects collided in your drag handler’s move phase.

  
pic1 = display.newImage(...)  
  
local function movePicture(event)  
 if event.phase == "moved" then  
 if hasCollided(event.target, frame) then  
 event.target.x = frame.x  
 event.target.y = frame.y  
 return true  
 else  
 -- the regular move/drag code you are using  
 end  
 end  
 return true  
end  
  
pic1:addEventListener("touch",movePicture)  

Of course you should handle the began and ended phases during the drag operation. [import]uid: 19626 topic_id: 22616 reply_id: 90547[/import]

ok i try and implement the code on the link you provided + yours and i am getting an error when it calls hascollided:

[C]: in function ‘hasCollided’
…nsel rios\documents\corona projects\ejemplo\game.lua:187: in function
<…nruntime error> …nsel rios\documents\corona projects\ejemplo\game.lua:187: attempt to
call global ‘hasCollided’ (a nil value)
stack traceback:
[C]: in function ‘hasCollided’
…nsel rios\documents\corona projects\ejemplo\game.lua:187: in function
<…nruntime error> …nsel rios\documents\corona projects\ejemplo\game.lua:187: attempt to
call global ‘hasCollided’ (a nil value)
stack traceback:
[C]: in function ‘hasCollided’
…nsel rios\documents\corona projects\ejemplo\game.lua:187: in function
<…n>
here is the code for my drag event:

[lua]local function drag ( event )

local t = event.target
local phase = event.phase

if “began” == phase then
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t, event.id )

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

if click < 2 then
click = click + 1

elseif click == 2 then
click = 1
end

if click == 1 then
oracion:insert(t)
elseif click == 2 then
if Fase == 1 then
Parte1:insert(t)
elseif Fase == 2 then
Parte2:insert(t)
elseif Fase == 3 then
Parte3:insert(t)
end

end


local function hasCollided(t, boton1)

if t == nil then

return false

end

if boton1 == nil then

return false

end

local left = t.contentBounds.xMin <= boton1.contentBounds.xMin and t.contentBounds.xMax >= boton1.contentBounds.xMin

local right = t.contentBounds.xMin >= boton1.contentBounds.xMin and t.contentBounds.xMin <= boton1.contentBounds.xMax

local up = t.contentBounds.yMin <= boton1.contentBounds.yMin and t.contentBounds.yMax >= boton1.contentBounds.yMin

local down = t.contentBounds.yMin >= boton1.contentBounds.yMin and t.contentBounds.yMin <= boton1.contentBounds.yMax

return (left or right) and (up or down)

end




elseif t.isFocus then

if “moved” == phase then

if hasCollided(t, boton1) then

t.x = boton1.x
t.y = boton1.y

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

end

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

end



return true
end

end
YoQuiero:addEventListener(“touch”, drag )
NoQuiero:addEventListener(“touch”, drag )
comer:addEventListener(“touch”, drag )
tomar:addEventListener(“touch”, drag )
egg:addEventListener(“touch”, drag )
pancake:addEventListener(“touch”, drag )
leche:addEventListener(“touch”, drag )
cereal:addEventListener(“touch”, drag )
pan:addEventListener(“touch”, drag )
egg:addEventListener(“touch”, drag )[/lua]

since i am unfamiliar in the used of the tables, i am assigning one by one the event listener. I assigned the event to the pics that i want to move, and if the collide with a static image below, they snap in with the static image.

any help on why this is failing??
[import]uid: 123133 topic_id: 22616 reply_id: 91263[/import] </…n></…nruntime></…nruntime>

You have a scope problem. Your function 'hasCollided" is only visible inside the block:

if “began” == event.phase then

else
since it’s scoped to be local to that block only. Move that function above your drag function to solve the problem. [import]uid: 19626 topic_id: 22616 reply_id: 91266[/import]

i tried that and since i am using the (t) (event target) (witch it is created in the begining of the drag function ) i wont reconize the (t) variable. and i try to move the local t (event.target) outside of the drag function but it doesnt let me create it. That is why i inserted the function inside the bolck.

So basicly i can use a varibale holding the event target to use it in hascollided? [import]uid: 123133 topic_id: 22616 reply_id: 91270[/import]

Sure it will. “t” is just a parameter that gets passed to the function. You’re passing it properly when you call hasCollided below.

If you’re getting errors post those and we can move forward.
[import]uid: 19626 topic_id: 22616 reply_id: 91274[/import]

Great got it working! thanks. The problem was that i was declaring t outside of the drag function as well thinking that i had to. But got that problem fixed and got it working. Thanks alot rob :wink: [import]uid: 123133 topic_id: 22616 reply_id: 91551[/import]