collision detection [Solved]

Ok, so what’s happening is I have a few different objects setup in my app, some of them are set to collide with each other and preform certain things when this happens, all of those detections are working fine without problems.

The problem I’m having is that I have one Static and one Dynamic object that are set to preform functions when they collide with different objects but not each other. The problem is the Static object can be moved around the screen and when it collides with this one dynamic object, instead of doing nothing and just continuing like it should, its pushing the dynamic object out of its way. I can’t have it doing this because the object its pushing needs to stay where it is and not be able to be moved.

I have no idea how to fix this because no where in my code does it have anything to do with these colliding so I don’t know what I need to change so they can’t collide with each other and move the other one. They need to stay as Static and Dynamic objects for their other uses in the code so that can’t change.

Here is the part of coding that has to do with these two objects, my Block and Star…

function newStar( ) -- Create new stars  
 if ( gameState == "Play" ) then  
 local Star = display.newImage ("Star.png")  
 Star.x = math.random (140 , 470)  
 Star.y = 305  
 Star.itemName = "Star"  
 physics.addBody( Star, "dynamic")  
 Star:addEventListener( "collision", Star )  
 groupOne:insert( Star )   
  
function Star:collision (event)  
 if event.other.itemName == "sprite" then  
 groupOne:remove( Star )  
 Star = nil  
 local StarSpawner = timer.performWithDelay( StarTime, newStar, 1 )  
 updatePoints("add")  
 end  
end   
 end   
end  
  
 local makeWallTime = 8000  
  
local function onBlockCollision( self, event ) -- Delete block  
 if event.other.itemName == "rock" then  
 groupOne:remove( self )  
 self = nil  
 end  
end   
  
local function createWall( event )  
 if ( gameState == "Play" ) then  
 local block = display.newRect( math.random (40 , 75), math.random (130 , 220), 30, 4.5 )  
 block:setFillColor( math.random (20 , 255), math.random (20 , 255), math.random (20 , 255))  
 physics.addBody( block, "static" )  
 block.collision = onBlockCollision  
 block.itemName = "block"  
 block:addEventListener( "collision", block )   
 groupOne:insert( block )  
  
local function onTouch( event )  
 local blockmoved = event.target  
 local phase = event.phase  
if ( gameState == "Play" ) then  
 if "began" == phase then  
 -- Make target the top-most object  
local parent = blockmoved.parent  
 parent:insert( blockmoved )  
 display.getCurrentStage():setFocus( blockmoved )  
 blockmoved.isFocus = true  
 -- Store initial position  
 blockmoved.x0 = event.x - blockmoved.x  
 blockmoved.y0 = event.y - blockmoved.y  
 elseif blockmoved.isFocus then  
 if "moved" == phase then  
 blockmoved.x = event.x - blockmoved.x0  
 blockmoved.y = event.y - blockmoved.y0  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( nil )  
 blockmoved.isFocus = false  
 end  
 end  
 return true  
 else  
block.isFocus = false  
 end  
end  
 block:addEventListener( "touch", onTouch ) -- Let block be moved by touch  
 end  
end  
  
if ( gameState == "Play" ) then   
 local WallTime = timer.performWithDelay( makeWallTime, createWall, 100 )  
end  

(note, this is not the full code, only the that involves the two objects) [import]uid: 69700 topic_id: 11876 reply_id: 311876[/import]

So you want the block to just stop when it collides with the star? Or perhaps pass through it?

This might be a job for pre-collision detection.

http://developer.anscamobile.com/reference/index/eventother [import]uid: 58455 topic_id: 11876 reply_id: 43274[/import]

I need the block to just pass through the star and not react to it at all. Using preCollision could work but again, there is nothing in the code telling it to move the star or anything so I have no idea what I would put in the preCollision function to get it pass through the star. [import]uid: 69700 topic_id: 11876 reply_id: 43279[/import]

And I’m assuming if the pre-collision check is true, you can’t just set these to kinematic because they might be also colliding with something else :-/

Hmmm…need a Corona expert here. How can we make two physics objects “ignore” each other?

Paging Peach Pellen… [import]uid: 58455 topic_id: 11876 reply_id: 43311[/import]

Ya, setting them both to kinematic makes it so the wall still works when it collides with rocks like it should) and also makes it so it just passes through the star (like it needs to) but then with the star being kinematic it doesn’t collide with the sprite so I can’t use that way because the sprite and star need to collide.

Changing the Sprite from static to dynamic makes it so it does collide with the star like it needs to, but then the problem with that is we get back to the first problem of the block moving the object it collides with, this time that object is now the Sprite instead of the Star, so that wont work either.

[edit]

Thanks to chinmay on the corona irc channel this is now fully working. I changed the physic bodies back to how they originally where and am now using collision filters so that the blocks, star and sprite only collide with what they are suppose to. The block no longer pushes the star around and everything collides how it should. [import]uid: 69700 topic_id: 11876 reply_id: 43338[/import]