Collision

Hi,

is possible to implement vent (or ventgroup) collisions with other objects, like sprites?

Thanks in advance.

-j

Yep; you’ll just need to make particles physical with Box2D. There’s a sample that demonstrates that called “Box2D” in the SampleCache folder of the CBEffects GitHub repository.

Once you’ve added physics, just add a collision listener in the onCreation function and you should be good to go.

  • C

Great!

Thanks

-j

Caleb, thanks to your instructions I managed to collide a vent with an external object (meteor sprite). The vent that I used is based on your preset “hyperspace”, but the collision occurs only at the source point of the particles.

Perhaps because the listener collision was included into the property “OnCreation”, but also moving it in “OnUpdate” I can’t detect the collision between the spokes and the external sprite. 

Any suggestions? 

Thank you

What is your physics hybrid draw mode showing?

By the way, you should put your “add collision listener” section into the onCreation function.

  • Caleb

I use “kinematic” phisics body type for my vent.

Yes p:addEventListener(“collision”, collisionHandler) is into onCreation function.

Sorry, I mean can you show me a screenshot of when you do this:

[lua]

physics.setDrawMode(“hybrid”)

[/lua]

  • Caleb

Thanks Caleb,

sorry I had not realized… Attached the screenshot required.

-j

I see…

What’s happening is that particles are getting created as tiny squares, and physics is being added to them. When they grow, the physics body isn’t growing with them.

You’ll need to either specify a physics “box” parameter, or try some body-growing “hack”.

Check out this page for the “box” parameter: http://docs.coronalabs.com/api/library/physics/addBody.html

  • Caleb

I tried with the box, but maybe I have not properly implemented the collision system. 

This is the code of my vent:

--------------------------------------------------------------------------------- -- -- vent.lua -- --------------------------------------------------------------------------------- local display\_newImageRect = display.newImageRect local physics = require("physics") physics.start() physics.setDrawMode("hybrid") local M = {} local CBE = require("CBEffects.Library") function M:new(xv ,yv, collisionVentHandler) if ( collisionVentHandler and type(collisionVentHandler) == "function" ) then M.callback = collisionVentHandler else error( "No callback function listed" ) end local function collisionVent( event ) M.callback( event ) end local xvent = xv local yvent = yv local self = CBE.newVent { title = "hyperspace", positionType = "atPoint", x = xvent, y = yvent, build = function() return display\_newImageRect("CBEffects/textures/glow.png", 10, 5) end, color = {{1}}, emitDelay = 100, perEmit = 9, inTime = 500, lifeTime = 0, outTime = 1200, startAlpha = 0, onCreation = function(p) p.anchorX = 0 p.name = "spacestorm" physics.addBody(p, "kinematic") p:addEventListener('collision', collisionVent) end, onUpdate = function(p) if p.\_numUpdates \> 3 then p.\_cbe\_reserved.rotateTowardVel = false end end, propertyTable = {blendMode = "screen"}, rotateTowardVel = true, physics = { linearDamping = 0.9, velocity = 0.01, angles = {{1, 360}}, scaleRateX = 1.16, maxScaleX = 100, } } return self end return M

I noticed in your code you’re still not specifying a box parameter in your physics.addBody call.

[lua]

physics.addBody(p, “kinematic”, {box = {halfWidth = 256, halfHeight = 2.5, x = 0, y = 0}})

[/lua]

Try that and see where it gets you.

  • Caleb

Great! Now I’m on the right track! I had tried with the box, but I had not implemented in the right way…

Thank you very much, Caleb!

-j

Yep; you’ll just need to make particles physical with Box2D. There’s a sample that demonstrates that called “Box2D” in the SampleCache folder of the CBEffects GitHub repository.

Once you’ve added physics, just add a collision listener in the onCreation function and you should be good to go.

  • C

Great!

Thanks

-j

Caleb, thanks to your instructions I managed to collide a vent with an external object (meteor sprite). The vent that I used is based on your preset “hyperspace”, but the collision occurs only at the source point of the particles.

Perhaps because the listener collision was included into the property “OnCreation”, but also moving it in “OnUpdate” I can’t detect the collision between the spokes and the external sprite. 

Any suggestions? 

Thank you

What is your physics hybrid draw mode showing?

By the way, you should put your “add collision listener” section into the onCreation function.

  • Caleb

I use “kinematic” phisics body type for my vent.

Yes p:addEventListener(“collision”, collisionHandler) is into onCreation function.

Sorry, I mean can you show me a screenshot of when you do this:

[lua]

physics.setDrawMode(“hybrid”)

[/lua]

  • Caleb

Thanks Caleb,

sorry I had not realized… Attached the screenshot required.

-j

I see…

What’s happening is that particles are getting created as tiny squares, and physics is being added to them. When they grow, the physics body isn’t growing with them.

You’ll need to either specify a physics “box” parameter, or try some body-growing “hack”.

Check out this page for the “box” parameter: http://docs.coronalabs.com/api/library/physics/addBody.html

  • Caleb