Collision check between different bodies

Ok so I am wondering.

Say you have a ball. This ball has 2 objects it can collide with, for example a spring and a static platform.
How do you check for collisions and execute different actions depending on which object the ball collided with? I believe I asked this before and in theory it is simple to do, however I still see no clear indication about comparing multiple physical bodies and executing actions custom to each one.

Like when the ball hits the spring it should bounce off it, when it hits the platform it should bounce a little then rest.

Having one defined collision type just isn’t going to work for the idea i have. [import]uid: 6981 topic_id: 4085 reply_id: 304085[/import]

that behaviour your mention is automatically handled by the physics engine depending on the “bounce”, “friction”, “density” properties of your objects in conjunction with the gravity setting

however if you give your objects custom properties like

obj1.isPlatform=true

then you can check this property during collisions and act accordingly

i suggest you work through the included samples which cover some of these issues [import]uid: 6645 topic_id: 4085 reply_id: 12601[/import]

Yeah that part is fine. However what if you wanted one of the objects to act as a wind turbine? Those three properties simply wont cut it and you need the custom actions, ala been able to execute a function on collision with a particular object [import]uid: 6981 topic_id: 4085 reply_id: 12603[/import]

like i said give your object a custom property that you can check in your collision function
http://developer.anscamobile.com/reference/index/eventobject1 [import]uid: 6645 topic_id: 4085 reply_id: 12605[/import]

I know what your saying, but care to elaborate a little further (code example). The link you provided doesn’t really show how to contrast between the two collision types.

I appreciate your help and am not trying to be rude, by any means. It’s only that I have asked this question several times (over the last few months) and have only ever got vague information on what I am trying to achieve.

Thank you [import]uid: 6981 topic_id: 4085 reply_id: 12606[/import]

[lua]local score=0

local crate = display.newImage( “crate.png” )
crate.x = 90; crate.y = 90
physics.addBody( crate, { density=3.0, friction=0.4, bounce=0.2 } )
crate.isCrate = true – custom flag

local platform2 = display.newImage( “platform2.png” )
platform2.x = 240; platform2.y = 150
physics.addBody( platform2, “kinematic”, { friction=0.7 } )
platform2.isPlatform = true – custom flag again

local function doCrateVsPlatform(theCrate, thePlatform)

score=score+10
theCrate:removeSelf()
thePlatform.alpha=0.1

end function

local function onGlobalCollision(event)

local obj1 = event.object1
local obj2 = event.object2

if(event.phase==“began”) then
if(obj1.isCrate and obj2.isPlatform) then
doCrateVsPlatform(obj1,obj2)
end

end

return(true)

end

Runtime:addEventListener( “collision”, onGlobalCollision )[/lua] [import]uid: 6645 topic_id: 4085 reply_id: 12608[/import]

Thank you, that explained it perfectly.

I really appreciate it. Cheers [import]uid: 6981 topic_id: 4085 reply_id: 12609[/import]

Hi,

I’m working on something similar, I tried applying the code in this post but still it didn’t work.

I have a ball rolling down a platform. At the end of the platform there is a wall. When the ball collides with the wall it is destroyed and a similar new ball is created at a deferent location on the screen. I managed to distroy it on collison , but when i try to creat a new ball the simulater crashes. would appreciate help in correcting the following code

thx

[code]

local wall= display.newRect( 10, 100, 20, 100 )
physics.addBody( wall, “static”, { friction=0.5 } )
wall:setFillColor( 97, 226, 253 )
wall.alpha = 1
wall.myName = “wall”

local platform= display.newRect( 40, 140, 100, 20 )
physics.addBody( platform, “static”, { friction=0.5 } )
platform:setFillColor( 88, 48, 32 )
platform.alpha = 1
platform.rotation = -5

local ball= display.newCircle( 80, 120, 20 )
physics.addBody( ball, { density=0.6, friction=0.6, bounce=0.3, radius=20} )
ball:setFillColor( 255, 110, 0 )
ball.alpha = 1
ball.myName = “ball”
local function onLocalCollision( self, event )

if ( event.phase == “began” ) then

if self.myName == “ball” and event.other.myName == “wall” then
ball:removeSelf()

local newBall= display.newCircle( 200, 10, 20 )
physics.addBody( newBall, { density=0.6, friction=0.6, bounce=0.3, radius=20} )
newBall:setFillColor( 255, 110, 0 )
newBall.alpha = 1
newBall.myName = “newBall”

end
end
end
ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )

[/code] [import]uid: 10542 topic_id: 4085 reply_id: 12614[/import]

you dont want to add bodies in the collision event itself i dont think… the world shouldnt change inside a physics step. that is why removeSelf isn’t instant. internally Box2D has a step function and changes to the world need to happen outside this (ie in your enterframe event)

set a flag in your collision like

createNewBall = createNewBall + 1

and in your enterframe check the value of createNewBall and add that many balls and set it back to 0.
[import]uid: 6645 topic_id: 4085 reply_id: 12624[/import]

@ jmp909

I’m not sure how to do that. Can you give a code example please.

thx [import]uid: 10542 topic_id: 4085 reply_id: 12647[/import]

ah you’re only creating 1 ball at a time. sorry i didn’t read. why even bother destroying it at all, cant you just move it somewhere else? [import]uid: 6645 topic_id: 4085 reply_id: 12650[/import]

Well… It’s meant to disappear, and re-appear at a deferent location.

Some code would be very appreciated.

thanks [import]uid: 10542 topic_id: 4085 reply_id: 12651[/import]

ok i cant find a way to move it manually…so

[lua]local physics=require(“physics”)
physics.start()

local wall= display.newRect( 10, 100, 20, 100 )
physics.addBody( wall, “static”, { friction=0.5 } )
wall:setFillColor( 97, 226, 253 )
wall.alpha = 1
wall.myName = “wall”

local platform= display.newRect( 40, 140, 100, 20 )
physics.addBody( platform, “static”, { friction=0.5 } )
platform:setFillColor( 88, 48, 32 )
platform.alpha = 1
platform.rotation = -5


local mainball – our main character
local _needToCreateNewBall = false

local function onBallCollision( event )

local obj = event.target

if ( event.phase == “began” ) then

if(event.other.myName==“wall”) then

mainball:removeSelf()
_needToCreateNewBall=true

end
end

return true
end
local function createBall(xpos,ypos)

local ball = display.newCircle(xpos, ypos, 20 )
physics.addBody( ball, { density=0.6, friction=0.6, bounce=0.3, radius=20} )
ball:setFillColor( 255, 110, 0 )
ball.alpha = 1
ball.myName = “ball”

ball:addEventListener( “collision”, onBallCollision )

return ball

end
function update(event)

if(_needToCreateNewBall==true) then
_needToCreateNewBall = false
mainball=createBall(100,10) – recreate our main character
end

end

mainball=createBall(80,120)
mainball.collision = onLocalCollision
Runtime:addEventListener(“enterFrame”, update)[/lua] [import]uid: 6645 topic_id: 4085 reply_id: 12652[/import]

Wow, that is brilliant.

I would have never figured it out

thanks jmp909, very appreciated ^.^ [import]uid: 10542 topic_id: 4085 reply_id: 12654[/import]