Sprite collision without using box2d

So I am currently trying to get collision between 2 sprites working without using the corona physics system. I cannot use the corona physics because my sprites are not square and I do need to rotate them on occasion, which this physics system doesn’t understand.

My two sprites are made from sprite sheets, and the logic I am trying to follow is that if an edge of a sprites overlaps an edge of the other sprite then we have collision, sounds simple but doesn’t seem to work.

if(spriteObject.x - spriteObject.contentWidth / 2 \>= richInstance1.x + richInstance1.contentWidth / 2 and spriteObject.x - spriteObject.contentWidth / 2 \< richInstance1.x - richInstance1.contentWidth / 2) then print("pre collision hit") if(spriteObject.y + spriteObject.contentHeight / 2 \>= richInstance1.y - richInstance1.contentHeight / 2 and spriteObject.y + spriteObject.contentHeight / 2 \< richInstance1.y + richInstance1.contentHeight / 2) then print("collision with ball") elseif(spriteObject.y - spriteObject.contentHeight / 2 \<= richInstance1.y + richInstance1.contentHeight / 2 and spriteObject.y - spriteObject.contentHeight / 2 \> richInstance1.y - richInstance1.contentHeight / 2) then print("collision with ball") end end [import]uid: 105311 topic_id: 21677 reply_id: 321677[/import]

kc that is a wonderfully thoughtful answer. +1 [import]uid: 52491 topic_id: 21677 reply_id: 86043[/import]

You should be able to use sprites as physics objects and rotate them, even if they aren’t square. I do it in my current project.

If your objects aren’t square, you can assign shapes to them using the following:
[lua]mainCharacterShape = { 15,-22, 16,0, 14,20, 10,31, -10,32, -14,20, -19,-6, -14,-20 }[/lua]
Then create a new spritesheet:
[lua]sheet1 = sprite.newSpriteSheet( “testSprite.png”, 64, 80
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 4)
sprite.add( spriteSet1, “mainCharacterSprite”, 1, 4, 500, 0 )
mainCharacter = sprite.newSprite( spriteSet1 ) [/lua]
Whenever you want to rotate it you just use the following:
[lua]if character == “needToRotate” then mainCharacter:rotate(90) end [/lua]
The following is my exact collision code:
[lua]local function onLocalPostCollision( self, event )
– This new event type fires only after a collision has been completely resolved. You can use
– this to obtain the calculated forces from the collision. For example, you might want to
– destroy objects on collision, but only if the collision force is greater than some amount.

bodyName = event.other.bodyName
if ( event.force > 20.0 ) then
–print( "postCollision force: " … event.force … ", friction: " … event.friction )
local impactChannel
if string.find(bodyName, “lava”) ~= nil or string.find(bodyName, “grass”) ~= nil then
if playSounds then impactChannel = audio.play( boingSound, { channel=3 } ) end
elseif string.find(bodyName, “ramp”) ~= nil then
if playSounds then impactChannel = audio.play( swooshSound, { channel=3 } ) end
elseif string.find(bodyName, “trampoline”) ~= nil then
if playSounds then impactChannel = audio.play( bounceSound, { channel=3 } ) end
elseif string.find(bodyName, “keg”) ~= nil then
if playSounds then impactChannel = audio.play( owSound, { channel=3 } ) end
elseif string.find(bodyName, “spikeWall”) ~= nil then
if playSounds then impactChannel = audio.play( owSound, { channel=3 } ) end
end
end
end[/lua]
It works when your character/item collides with other objects.

The greatest part about Corona is it makes the physics possible and easy. Don’t give up on it and it will impress you, I promise. [import]uid: 44607 topic_id: 21677 reply_id: 86009[/import]

Thanks for the reply, now what I am curious about is do I need to make a shape for a rectangular object, and then how do I apply it, because I don’t see where that is done from your snippets.
[import]uid: 105311 topic_id: 21677 reply_id: 86242[/import]

What you would do, using KC’s code;

[lua]mainCharacterShape = { 15,-22, 16,0, 14,20, 10,31, -10,32, -14,20, -19,-6, -14,-20 }
physics.addBody(obj, “dynamic”, {shape=mainCharacterShape})[/lua]

It would then have the shape defined above.

Peach :slight_smile: [import]uid: 52491 topic_id: 21677 reply_id: 86267[/import]