preCollision and event.contact.isEnabled = false - letting go through but slowing down ?

Hi all,

i’ve got a ball going through moving bricks. On the way up they are going THROUGH the bricks. and BOUNCE on the way down.

To allow the ball go through on the way up i’ve used preCollision and event.contact.isEnabled = false

Both the ball and the bricks are physics objects

it works … but going through the bricks, the ball is slowed down dramatically.

e.g.  with a vy velocity of -400 if it doesn’t touch any bricks the ball goes all the way to the top of the screen, if going through the bricks it will only pass 2 or 3 going to 30% of the screen.

I thought that the  event.contact.isEnabled = false  would make the collision event do nothing ? therefore 

letting the ball go through without slowing it down ?

local function on\_ball\_precollision(self,event) --print( "preCollision: " .. self.myName .. " is about to collide with " .. event.other.myName ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (self.myName == "myball" and not(event.other.myName == "floor")) then &nbsp;-- means collision with a brick &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local vx, vy = myball:getLinearVelocity() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ("precollision:", "\<\>",event.other.myName,"\<\>",vy) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if vy \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (event.contact==nil) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.contact.isEnabled = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp;

Am I wrong ? if so, is there another method to let it go through ? (i cant set the ball as “kinematic” otherwise it keeps going up, never come down …

thanks !

Edualc

Hi @Edualc,

Instead of using pre-collision, would this work for your scenario?

  1. Make the ball into a sensor object.

  2. Use a Runtime listener to check the ball’s velocity. When it starts falling (vy >= 0), make it a non-sensor object and stop checking the velocity (stop the Runtime listener for efficiency).

Brent

thank you Brent.

Actually, it was my buggy code that was doing it … :frowning: I was resetting the y velocity to -200 on each collision… silly bugger…

however this begs a question: I looked at the code PhysicsContact provider by the Corona sdk

function char:preCollision( event ) &nbsp;&nbsp;&nbsp;&nbsp;if ( event.other.collType == "passthru" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local charBase = self.y+20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local platTop = event.other.y-32 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( charBase \> platTop+8 ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.contact.isEnabled = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.isSensor = true ; self.setAsSensor = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp;

here the programmer uses BOTH contact.isEnabled  and isSensor=true

is there a reason for it or is that toBeSure  toBeSure ? shouldn’t either do the trick ?

thanks !

Edualc

Hi Edualc,

If I recall, from that code example, the purpose was as follows for “one-sided platforms”:

  1. When the jumping object pre-collides with the platform from the bottom, the collision is “voided” (ignored)

  2. At the same time, the object is turned to a sensor… this allows the object to behave like a ghost (no collision) while it remains “inside” the platform.

  3. When the object exits the platform, it reverts to a normal non-sensor object… this makes it start colliding as normal.

Hope that clarifies it,

Brent

thanks Brent. 

Hi @Edualc,

Instead of using pre-collision, would this work for your scenario?

  1. Make the ball into a sensor object.

  2. Use a Runtime listener to check the ball’s velocity. When it starts falling (vy >= 0), make it a non-sensor object and stop checking the velocity (stop the Runtime listener for efficiency).

Brent

thank you Brent.

Actually, it was my buggy code that was doing it … :frowning: I was resetting the y velocity to -200 on each collision… silly bugger…

however this begs a question: I looked at the code PhysicsContact provider by the Corona sdk

function char:preCollision( event ) &nbsp;&nbsp;&nbsp;&nbsp;if ( event.other.collType == "passthru" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local charBase = self.y+20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local platTop = event.other.y-32 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( charBase \> platTop+8 ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.contact.isEnabled = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.isSensor = true ; self.setAsSensor = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp;

here the programmer uses BOTH contact.isEnabled  and isSensor=true

is there a reason for it or is that toBeSure  toBeSure ? shouldn’t either do the trick ?

thanks !

Edualc

Hi Edualc,

If I recall, from that code example, the purpose was as follows for “one-sided platforms”:

  1. When the jumping object pre-collides with the platform from the bottom, the collision is “voided” (ignored)

  2. At the same time, the object is turned to a sensor… this allows the object to behave like a ghost (no collision) while it remains “inside” the platform.

  3. When the object exits the platform, it reverts to a normal non-sensor object… this makes it start colliding as normal.

Hope that clarifies it,

Brent

thanks Brent.