[RESOLVED] Stop Physics body been moved on Collision

Hi

I have a physics body (bird) that when  collides with another object (nut) the nut is removed.

What I want to stop happening is when the collision occurs sometimes the bird is affected by the collsion. I have tried setting the isSensor = true to bird but it only works some of the time. I may have five nuts in a row, some off nuts will effect / budge the bird.

–Bird

physics.addBody( bird ,“dynamic”, { density=1.0, friction=1, bounce=0, radius=20})\

–nuts

nut[i].type = “nuts”

nut[i].id = physics.addBody( ball[i] , “kinematic”,{density=0, friction=0, bounce=0, radius=20})

transition.to(nut[i], { time = 1500 + score, x =-100, y=cenerY}) 

nut[i].collision = myCollision

 nut[i]:addEventListener(“collision”, nut[i])

local function myCollision(self, event)

  

  if  event.phase == “began”  then 

  self.isSensor = true

 if event.target.type == “nuts”  and event.other.type == “bird” then

    

 if (nut[event.target.id]) ~= nil  then

    nut[event.target.id] = nil         – We remove object from table

       event.target:removeSelf()  

end

display.getCurrentStage():setFocus(nil)

end

elseif (event.phase == “ended”) then

self.isSensor = false

end

end

Hi @andrew085,

You should probably cancel the transition on the nut when it collides with the bird. There may be an additional frame of action where the transition is forcing some effect on the bird.

Alternatively, can you just make the bird into a sensor from the very beginning? Or does it need to be a non-sensor in all other cases except for colliding with a nut?

The last solution, if you need absolutely no collision force reaction whatsoever, is to use a pre-collision listener, and when that happens, you make the bird into a sensor or whatever.

Hope this helps,

Brent

Thanks Brent

Interesting, I have made the bird and the nuts set to isSensor = true when they spawn.  The bird is still bumped by the nuts when there is  more than 1 in row hitting the bird.

I just tried using transition.cancel on event.phase == “began” but I think to late by then

I then set the physics body type for the nuts to  dynamic  and the effects on the bird stop! unfortunitlly I need the nuts to be static or kinematic, where the bird is dynamic.

so will now look into pre-collision listener…

Thanks

Hi Andrew,

Are you sure you’re making them sensors after you apply the physics body? That might seem like a “dumb” question, but I’ve seen several cases where developers say that a particular physics body property isn’t working, and it’s because they set something like .isSensor or .isFixedRotation before the object was made physical.

Brent

Agghh that was it, silly mistake, thank you!

Hi @andrew085,

You should probably cancel the transition on the nut when it collides with the bird. There may be an additional frame of action where the transition is forcing some effect on the bird.

Alternatively, can you just make the bird into a sensor from the very beginning? Or does it need to be a non-sensor in all other cases except for colliding with a nut?

The last solution, if you need absolutely no collision force reaction whatsoever, is to use a pre-collision listener, and when that happens, you make the bird into a sensor or whatever.

Hope this helps,

Brent

Thanks Brent

Interesting, I have made the bird and the nuts set to isSensor = true when they spawn.  The bird is still bumped by the nuts when there is  more than 1 in row hitting the bird.

I just tried using transition.cancel on event.phase == “began” but I think to late by then

I then set the physics body type for the nuts to  dynamic  and the effects on the bird stop! unfortunitlly I need the nuts to be static or kinematic, where the bird is dynamic.

so will now look into pre-collision listener…

Thanks

Hi Andrew,

Are you sure you’re making them sensors after you apply the physics body? That might seem like a “dumb” question, but I’ve seen several cases where developers say that a particular physics body property isn’t working, and it’s because they set something like .isSensor or .isFixedRotation before the object was made physical.

Brent

Agghh that was it, silly mistake, thank you!