Collision detection

I’m trying to make it so a Star image gets created on my screen and when a player walks into it the star gets deleted and it reloads the timer to spawn a new star.

The player only moves along the X axis and does not move up or down at all. i Have the player and the star on the same Y cords but nothing happens when they collide. The following is the code I’m using for creating the star and for the star collision portion…

local Sprite = display.newImage ("Sprite.png")  
 Sprite.x = 330  
 Sprite.y = 290  
 groupOne:insert( Sprite )  
  
 local StarTime = 0   
  
local function onStarCollision( self, event ) -- Delete Star  
 groupOne:remove( self )  
self = nil  
 StarTime = 0  
end   
  
function newStar() -- Create new rocks  
 if ( gameState == "Play" ) then  
 local Star = display.newImage ("Star.png")  
 Star.x = math.random (140 , 470)  
 Star.y = 290  
 physics.addBody( Star, "static", { isSensor=true } )  
 Star.id = "Star"  
 Star.collision = onStarCollision  
 Star:addEventListener( "collision", Star )  
 groupOne:insert( Star )  
 end  
end  
  
local StarSpawn = display.newText( "..", -99, -99, native.systemFont, 1 )  
StarSpawn:setTextColor( 0, 0, 0 )  
 groupOne:insert( StarSpawn )  
  
-- Setup Timers  
function StarSpawn:timer( event )  
 if ( gameState == "Play" ) then  
 StarTime = event.count  
 if ( StarTime == 5 ) then -- Create new star  
 newStar()  
 end  
 else  
 event.count = 0;  
 end  
end  

But my player just runs through the star and nothing happens. if I make the star fall from above down onto the player then the collision works fine, but it does not work when they are both at the same height and moving from side to side into each other. [import]uid: 69700 topic_id: 11672 reply_id: 311672[/import]

Does your player have a “kinematic” or “static” physics body?

Only “dynamic” bodies can collide with “kinematic” or “static” bodies. [import]uid: 49978 topic_id: 11672 reply_id: 42434[/import]

My player is also Static so thats prob why. I can’t have my Player or Star move down though, they have to stay at the same X access instead of falling. How can I take off the static part and keep them from falling off the screen? (I can’t turn physics off, its needed for other parts of the app) [import]uid: 69700 topic_id: 11672 reply_id: 42469[/import]

If you check out http://techority.com I’ve recently posted Corona For Newbies Part 4, which has a section on collision detection - it might solve your problem :slight_smile: [import]uid: 52491 topic_id: 11672 reply_id: 42480[/import]

I tried using your code for the collision detection with my Star and player, but it still does nothing when I run through my star with the player sprite.

My code now looks like the following:

local Sprite = display.newImage ("Sprite.png")  
 Sprite.x = 330  
 Sprite.y = 290  
 Sprite.itemName = "player"  
 groupOne:insert( Sprite )  
  
gameState = "Play"  
  
 -- Setup Star and Points calling --  
-- Display score  
 GamePoints.DisplayPoints()  
 local StarTime = 0   
  
function newStar() -- Create new rocks  
 if ( gameState == "Play" ) then  
 local Star = display.newImage ("Star.png")  
 Star.x = math.random (140 , 470)  
 Star.y = 290  
 physics.addBody( Star, "static")  
 Star:addEventListener( "collision", Star )  
 groupOne:insert( Star )   
function Star:collision (event)  
 if event.other.itemName == "player" then  
 groupOne:remove( Star )  
 Star = nil  
 StarTime = 0  
 end  
end   
 end   
end  
  
local StarSpawn = display.newText( "..", -99, -99, native.systemFont, 1 )  
StarSpawn:setTextColor( 0, 0, 0 )  
 groupOne:insert( StarSpawn )  
  
-- Setup Timers  
function StarSpawn:timer( event )  
 if ( gameState == "Play" ) then  
 StarTime = event.count  
 if ( StarTime == 5 ) then -- Create new star  
 newStar()  
 end  
 else  
 event.count = 0;  
 end  
end  

They both are still set as Static so I don’t know if thats the only/main reason the collision functions are not working but as I said before, I need to keep them as static because I need them to stay on the same Y axis.

Any help is very appreciated if you can figure out a way to fix this. [import]uid: 69700 topic_id: 11672 reply_id: 42488[/import]

I don’t see where the groupOne() display group is defined. Did you leave any code out or is what posted all of it? In your updated code I don’t see a function being called when the star is collided with. [import]uid: 27965 topic_id: 11672 reply_id: 42509[/import]

I left a whole lot of code out, I only posted the part that had to do with the Star/Sprite and their collision.

Also the collision function is under function Star:collision (event) in the above code [import]uid: 69700 topic_id: 11672 reply_id: 42530[/import]

They both are still set as Static so I don’t know if thats the only/main reason the collision functions are not working

yes that’s why they are not colliding.
My player is also Static so thats prob why. I can’t have my Player or Star move down though, they have to stay at the same X access instead of falling. How can I take off the static part and keep them from falling off the screen? (I can’t turn physics off, its needed for other parts of the app)

You can try to create invisible horizontal floor and roof which only collide with Player and Star using collision filters

[import]uid: 48521 topic_id: 11672 reply_id: 42543[/import]

Ahh ok, adding a invisible line to be used as a second floor that only supports the Star worked so that it can now be dynamic without falling.

I also used Peach’s physics examples and added names to all of my objects so their collider functions only work with the other objects I want them to instead of with everything. This way my player can gain points when they collide with the star instead of dieing when they collide with anything.

thanks a lot for the help, its all working now :smiley: [import]uid: 69700 topic_id: 11672 reply_id: 42549[/import]