Object's radius made my colision stop working.

I have a colision that works, or used to, that when the bullet hits the asteroid, both disapear. The question is that I didn’t put a radius to the asteroid and the default radius was huge, and was making the colision a little buggy. But, when I put a smaller radius in the physics.addBody() the colision stop working. If I remove the radius it works but it’s buggy, but with the radius defined by me, it won’t work.

Any help that you can give me

Cheers,

If you don’t specify a radius, the physics will take up the whole image, transparent areas included. I presume this is the cause of the buggy behaviour, but’s impossible to say whithout seeing some code and a better description of “buggy”. If adding a radius is stopping collisions from working, I don’t know what’s happening unless you’re not doing it right, or the object is too small and fast and is going through things.

In any case, this might be helpful:

local physics = require "physics" physics.start() physics.setGravity(0, 0) local leftWall = display.newRect(0, 240, 10, 480) local rightWall = display.newRect(320, 240, 10, 480) local topWall = display.newRect(160, 0, 320, 10) local bottomWall = display.newRect(160, 480, 320, 10) physics.addBody(leftWall, "static", {bounce = 1}) physics.addBody(rightWall, "static", {bounce = 1}) physics.addBody(topWall, "static", {bounce = 1}) physics.addBody(bottomWall, "static", {bounce = 1}) local asteroid, bullet, newAsteroidAndBullet local function onCollision(event) if event.other ~= asteroid or event.phase ~= "began" then return true end asteroid:setLinearVelocity(0, 0) bullet:setLinearVelocity(0, 0) timer.performWithDelay(10, function() display.remove(asteroid); display.remove(bullet) end) timer.performWithDelay(2000, newAsteroidAndBullet) end function newAsteroidAndBullet() asteroid = display.newCircle(160, 240, 20) physics.addBody(asteroid, "dynamic", {radius = asteroid.width/2}) asteroid:setLinearVelocity(math.random(-100, 100), math.random(-100, 100)) bullet = display.newCircle(30, 30, 3) physics.addBody(bullet, "dynamic", {radius = bullet.width/2}) bullet:setLinearVelocity(500, 500) bullet:addEventListener("collision", onCollision) end newAsteroidAndBullet()

Hi @alfredocosta25,

One convenient “trick” is to check how the physics engine is actually regarding physical bodies by turning on “hybrid” draw mode. Do this on some line after you have called physics.start():

[lua]

physics.setDrawMode( “hybrid” )

[/lua]

Best regards,

Brent

If you don’t specify a radius, the physics will take up the whole image, transparent areas included. I presume this is the cause of the buggy behaviour, but’s impossible to say whithout seeing some code and a better description of “buggy”. If adding a radius is stopping collisions from working, I don’t know what’s happening unless you’re not doing it right, or the object is too small and fast and is going through things.

In any case, this might be helpful:

local physics = require "physics" physics.start() physics.setGravity(0, 0) local leftWall = display.newRect(0, 240, 10, 480) local rightWall = display.newRect(320, 240, 10, 480) local topWall = display.newRect(160, 0, 320, 10) local bottomWall = display.newRect(160, 480, 320, 10) physics.addBody(leftWall, "static", {bounce = 1}) physics.addBody(rightWall, "static", {bounce = 1}) physics.addBody(topWall, "static", {bounce = 1}) physics.addBody(bottomWall, "static", {bounce = 1}) local asteroid, bullet, newAsteroidAndBullet local function onCollision(event) if event.other ~= asteroid or event.phase ~= "began" then return true end asteroid:setLinearVelocity(0, 0) bullet:setLinearVelocity(0, 0) timer.performWithDelay(10, function() display.remove(asteroid); display.remove(bullet) end) timer.performWithDelay(2000, newAsteroidAndBullet) end function newAsteroidAndBullet() asteroid = display.newCircle(160, 240, 20) physics.addBody(asteroid, "dynamic", {radius = asteroid.width/2}) asteroid:setLinearVelocity(math.random(-100, 100), math.random(-100, 100)) bullet = display.newCircle(30, 30, 3) physics.addBody(bullet, "dynamic", {radius = bullet.width/2}) bullet:setLinearVelocity(500, 500) bullet:addEventListener("collision", onCollision) end newAsteroidAndBullet()

Hi @alfredocosta25,

One convenient “trick” is to check how the physics engine is actually regarding physical bodies by turning on “hybrid” draw mode. Do this on some line after you have called physics.start():

[lua]

physics.setDrawMode( “hybrid” )

[/lua]

Best regards,

Brent