Delete very fast body?

I’m having problems with the elimination of a very fast physical body.

I have a 30 * 30 ball body that anchored a 36 * 36 circular shield.

At this point I have a very quick bullet that in contact with the shield should eliminate and not touch the ball.

The problem is that it still touches the ball by eliminating it. How can I fix it? I also tried with “.isBodyActive = false” but it does not work. I attach the shield code:

 local shield = display.newImageRect( "images/shield.png", 36, 36) shield:translate( ball.x, ball.y ) shield.name = "shield" physics.addBody(shield, "dynamic", {isSensor = true, radius = 18, filter = filterShield} ) shield.isBullet = true shield.joint= physics.newJoint( "pivot", ball, shield, ball.x, ball.y) function shield:preCollision( event ) local phase, other = event.phase, event.other if(other.name == "bullet")then other.isBodyActive = false local t1 = timer.performWithDelay( 2, function() --remove bullet display.remove(other) other = nil --remove shield display.remove(self) self = nil end, 1) end end shield:addEventListener( "preCollision" )
 function shield:preCollision( event ) local phase, other = event.phase, event.other if(other.name == "bullet" and not other.dirty) then other.dirty = true local t1 = timer.performWithDelay( 0, function() --remove bullet display.remove(other) --remove shield display.remove(self) end, 1) end end

Thanks, but that does not solve anything…

If the shield is directly anchored to the ball, and they are centrally aligned with each other, can you just make the ball a 2-element physics body? And then just detect if the bullet hits the shield first (by its element index) and, if so, process that it has been “blocked” and won’t kill the player (ball)?

Brent

Thanks  Brent.

It’s an idea but I wonder if there are other ways.

The shield is not always present in the ball and as a bonus then goes and comes …

That’s OK… you could still keep the shield as a permanent fixture (element) of the ball, but just choose to honor whether it’s “on” or “off” using conditional code. In other words, if the shield is “on” and the bullet hits that element, you remove the bullet and consider the player safe. If the shield is “off”, you just let the bullet continue on to strike the player.

So like this:

[lua]

local player = display.newCircle( 100, 300, 20 )

physics.addBody( player, “kinematic”,  – could also be “dynamic” of course

    { radius=20 },  – element 1, the player itself

    { radius=50, isSensor=true }  – element 2, the shield around the player

)

local shieldOn = false  – variable to determine if shield is on or off (you can store this whatever way is best)

local function objectCollisionEvent( self, event )

    if ( event.selfElement == 2 and shieldOn == true ) then

        bullet:removeSelf()

    else

        print( “PLAYER HIT!!!” )

    end

end

player.collision = objectCollisionEvent

player:addEventListener( “collision” )

[/lua]

Hope this helps,
Brent

P.S. - I didn’t test this with a super-fast bullet object, so ensure it works and that the bullet doesn’t somehow register a hit with both elements.

Thanks again Brent ,

But the bullet continues to strike even the central part of the ball…

It works like my starting code, that is, it works well if the shield is very large and otherwise it will continue to shoot with the ball. But I need a little bigger shield than the ball.

It’s possible?

Hi again,

Is the bullet moving so fast that it might actually be skipping through the shield and hitting the player first? That is possible I suppose, if the bullet is moving extremely fast.

In your initial code posting, I just noticed that you’re adding the “.isBullet” property to the shield. Shouldn’t the bullet get the .isBullet property? Just curious…

In addition to the “.isBullet” property, you can increase global physics sensitivity with these two APIs:

https://docs.coronalabs.com/api/library/physics/setPositionIterations.html

https://docs.coronalabs.com/api/library/physics/setVelocityIterations.html

Note that changing those values can potentially affect performance (not likely but possible) so use them with caution.

Finally, is your shield just a tiny bit larger than the player? Like a very thin surrounding ring? If so, I would just skip trying to detect which element the bullet strikes, and conditional say “Is the player currently shielded?”. If so, just let the bullet strike the player and then remove it, conditionally taking no action like killing/damaging the player. Basically, act like the player is Superman and bullets just bounce off him.

Brent

Thanks again for patience

1.I do not know if the bullet is so fast but can be here the code

 physics.addBody(bullet, "dynamic", {density = 1, bounce = 0, filter=filterBullet} ) bullet.gravityScale = 0 bullet.isBullet = true bullet.isFixedRotation = true bullet:setLinearVelocity(0, 800, bullet.x, bullet.y)

2.As you can see both the shield and the bullet are “isBullet = true”

3.I’ve seen the links might be useful but I wonder if it’s worth it, these controls increase for each item, if I have many things the performance falls enough. right?

I believe maximum I could have about 10 objects at the same time no more…

4.The idea of superman is beautiful, however a shot at that speed would marry the ball that would be pushed (having the shield should not).Is there a way I do not know about this?

5.Anyway I think I’ve solved using a trick (inspired by your superman):

 local function preCollisionEvent( self, event ) local collideObject = event.other if ( self.isShield ) then event.contact.isEnabled = false --disable collision end end ball.preCollision = preCollisionEvent ball( "preCollision", ball)

For now it’s fine but could you still clarify points 3 and 4?

 function shield:preCollision( event ) local phase, other = event.phase, event.other if(other.name == "bullet" and not other.dirty) then other.dirty = true local t1 = timer.performWithDelay( 0, function() --remove bullet display.remove(other) --remove shield display.remove(self) end, 1) end end

Thanks, but that does not solve anything…

If the shield is directly anchored to the ball, and they are centrally aligned with each other, can you just make the ball a 2-element physics body? And then just detect if the bullet hits the shield first (by its element index) and, if so, process that it has been “blocked” and won’t kill the player (ball)?

Brent

Thanks  Brent.

It’s an idea but I wonder if there are other ways.

The shield is not always present in the ball and as a bonus then goes and comes …

That’s OK… you could still keep the shield as a permanent fixture (element) of the ball, but just choose to honor whether it’s “on” or “off” using conditional code. In other words, if the shield is “on” and the bullet hits that element, you remove the bullet and consider the player safe. If the shield is “off”, you just let the bullet continue on to strike the player.

So like this:

[lua]

local player = display.newCircle( 100, 300, 20 )

physics.addBody( player, “kinematic”,  – could also be “dynamic” of course

    { radius=20 },  – element 1, the player itself

    { radius=50, isSensor=true }  – element 2, the shield around the player

)

local shieldOn = false  – variable to determine if shield is on or off (you can store this whatever way is best)

local function objectCollisionEvent( self, event )

    if ( event.selfElement == 2 and shieldOn == true ) then

        bullet:removeSelf()

    else

        print( “PLAYER HIT!!!” )

    end

end

player.collision = objectCollisionEvent

player:addEventListener( “collision” )

[/lua]

Hope this helps,
Brent

P.S. - I didn’t test this with a super-fast bullet object, so ensure it works and that the bullet doesn’t somehow register a hit with both elements.

Thanks again Brent ,

But the bullet continues to strike even the central part of the ball…

It works like my starting code, that is, it works well if the shield is very large and otherwise it will continue to shoot with the ball. But I need a little bigger shield than the ball.

It’s possible?

Hi again,

Is the bullet moving so fast that it might actually be skipping through the shield and hitting the player first? That is possible I suppose, if the bullet is moving extremely fast.

In your initial code posting, I just noticed that you’re adding the “.isBullet” property to the shield. Shouldn’t the bullet get the .isBullet property? Just curious…

In addition to the “.isBullet” property, you can increase global physics sensitivity with these two APIs:

https://docs.coronalabs.com/api/library/physics/setPositionIterations.html

https://docs.coronalabs.com/api/library/physics/setVelocityIterations.html

Note that changing those values can potentially affect performance (not likely but possible) so use them with caution.

Finally, is your shield just a tiny bit larger than the player? Like a very thin surrounding ring? If so, I would just skip trying to detect which element the bullet strikes, and conditional say “Is the player currently shielded?”. If so, just let the bullet strike the player and then remove it, conditionally taking no action like killing/damaging the player. Basically, act like the player is Superman and bullets just bounce off him.

Brent

Thanks again for patience

1.I do not know if the bullet is so fast but can be here the code

 physics.addBody(bullet, "dynamic", {density = 1, bounce = 0, filter=filterBullet} ) bullet.gravityScale = 0 bullet.isBullet = true bullet.isFixedRotation = true bullet:setLinearVelocity(0, 800, bullet.x, bullet.y)

2.As you can see both the shield and the bullet are “isBullet = true”

3.I’ve seen the links might be useful but I wonder if it’s worth it, these controls increase for each item, if I have many things the performance falls enough. right?

I believe maximum I could have about 10 objects at the same time no more…

4.The idea of superman is beautiful, however a shot at that speed would marry the ball that would be pushed (having the shield should not).Is there a way I do not know about this?

5.Anyway I think I’ve solved using a trick (inspired by your superman):

 local function preCollisionEvent( self, event ) local collideObject = event.other if ( self.isShield ) then event.contact.isEnabled = false --disable collision end end ball.preCollision = preCollisionEvent ball( "preCollision", ball)

For now it’s fine but could you still clarify points 3 and 4?