isBodyActive() on a shape that is part of a physics body?

Hi,

This is somewhat related to a similar post that I put up earlier today. Hope I am not duplicating myself here, but i’m creating a physics body which has 3 complex bodies (polygonal shapes) that are part of it. Can I apply “isBodyActive = false” to one or more of these polygonal bodies specifically? If you see what I am getting at, is it possible to do this?

Please help in any way possible or provide any alternative methods. If this post is considered a duplicate I will remove my earlier (long-winded) post.

Kind Regards [import]uid: 125022 topic_id: 34019 reply_id: 334019[/import]

Hi @richieloco,
If you don’t mind me asking, what is your skill level with Corona physics? The solution to this isn’t exactly difficult, but it involves the use of the Corona “PhysicsContact” userdata object, a pre-collision listener, and complex-body index numbers. Essentially, you tell the collision listener which of the body elements (by its integer index in the overall body) should be “inactive”, then you tell the PhysicsContact to disable the collision on that exact part IF it’s one of the inactive ones… but none of this done using the isBodyActive property, because that affects the body as a whole.

If you want me to share some sample code for this, I can… I already have it written up, basically. Again, it’s not highly-advanced, but it’s definitely beyond the scope of basic physics in Corona. :slight_smile:

Best regards,
Brent
[import]uid: 200026 topic_id: 34019 reply_id: 135297[/import]

Hi Brent,

I think it’s fair to say that my skills level with Corona Physics is pretty basic :stuck_out_tongue: I picked up what I could from a beginners book but this, however, didn’t really touch much upon the collision detection for complex-bodies.

Please do share some sample code for this :slight_smile:

Kind Regards,
Rich [import]uid: 125022 topic_id: 34019 reply_id: 135302[/import]

Hi Rich,
Here’s a sample project for you, showing how it works. Please turn on “hybrid” physics view to see what’s going on.

The idea here is, when you create a multi-element body, those bodies will be assigned a number based on the order in which you add them to the overall body. You can then use these values to “turn off” one of the parts in the pre-collision listener… you’ll need to figure out a bit more enhanced logic than what I’ve provided, but it should be simple enough. Anyway, once you have that detection working, the use of the event.contact.isEnabled (a PhysicsContact feature) boolean determines if the actual collision happens or not.

So, check it out and see how it works for you. If you have any other questions, let me know.

Brent

[code]
local physics = require(“physics”) ; physics.start()
physics.setGravity( 0.0, 2.0 ) ; physics.setDrawMode( “hybrid” )

local function ballCollide( self, event )
–print(event.otherElement)
if( event.otherElement == 1 ) then
event.contact.isEnabled = false
end
return true
end

local box = display.newRect(0,0,200,100) ; box:setFillColor(255,0,0)
local bodyPart1 = { -150,0, 20,0, 20,34, -150,34 }
local bodyPart2 = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }

physics.addBody( box, “static”,
{ density=3.0, friction=0.5, bounce=0.1, shape=bodyPart1 },
{ density=4.0, friction=0.5, bounce=0.1, shape=bodyPart2 }
)
box.x = 140 ; box.y = 200

local ball = display.newCircle( testGroup, 150, 700, 10 ) ; ball:setFillColor(255,100,100)
physics.addBody( ball, “dynamic”, { density=1.0, friction=0.3, bounce=0.4, radius=10 } )
ball.preCollision = ballCollide ; ball:addEventListener( “preCollision”, ball )
ball.x = 120 ; ball.y = 0
[/code] [import]uid: 200026 topic_id: 34019 reply_id: 135365[/import]

Brent, this seems to be exactly what I am looking for :slight_smile: I will give this a try soon then let you know how I got on.

Thanks a lot
Rich [import]uid: 125022 topic_id: 34019 reply_id: 135410[/import]

This works perfectly Brent, thanks a lot :slight_smile: I just spot your article on physics events contact too (http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/) - wish I had seen it sooner :slight_smile:

I’ve shared my solution below.

 physics.addBody( character, "static",   
 { density=1.0, bounce=0.4, friction=0.15, shape=leftBody },  
 { density=1.0, bounce=0.4, friction=0.15, shape=rightBody },  
 { density=1.0, bounce=0.4, friction=0.15, shape=frontFacingBody }  
 )  
  
 local onDroppablePreCollision = function( self, event )  
 if character.state.direction == "F" then -- front-facing  
 if event.otherElement == 1 or event.otherElement == 2 then  
 event.contact.isEnabled = false  
 end  
 elseif character.state.direction == "R" then -- facing right  
 if event.otherElement == 2 or event.otherElement == 3 then  
 event.contact.isEnabled = false  
 end  
 elseif character.state.direction == "L" then -- facing left  
 if event.otherElement == 1 or event.otherElement == 3 then  
 event.contact.isEnabled = false  
 end  
 end  
 end  

Many Thanks,
Rich [import]uid: 125022 topic_id: 34019 reply_id: 135425[/import]

Hi @richieloco,
If you don’t mind me asking, what is your skill level with Corona physics? The solution to this isn’t exactly difficult, but it involves the use of the Corona “PhysicsContact” userdata object, a pre-collision listener, and complex-body index numbers. Essentially, you tell the collision listener which of the body elements (by its integer index in the overall body) should be “inactive”, then you tell the PhysicsContact to disable the collision on that exact part IF it’s one of the inactive ones… but none of this done using the isBodyActive property, because that affects the body as a whole.

If you want me to share some sample code for this, I can… I already have it written up, basically. Again, it’s not highly-advanced, but it’s definitely beyond the scope of basic physics in Corona. :slight_smile:

Best regards,
Brent
[import]uid: 200026 topic_id: 34019 reply_id: 135297[/import]

Hi Brent,

I think it’s fair to say that my skills level with Corona Physics is pretty basic :stuck_out_tongue: I picked up what I could from a beginners book but this, however, didn’t really touch much upon the collision detection for complex-bodies.

Please do share some sample code for this :slight_smile:

Kind Regards,
Rich [import]uid: 125022 topic_id: 34019 reply_id: 135302[/import]

Hi Rich,
Here’s a sample project for you, showing how it works. Please turn on “hybrid” physics view to see what’s going on.

The idea here is, when you create a multi-element body, those bodies will be assigned a number based on the order in which you add them to the overall body. You can then use these values to “turn off” one of the parts in the pre-collision listener… you’ll need to figure out a bit more enhanced logic than what I’ve provided, but it should be simple enough. Anyway, once you have that detection working, the use of the event.contact.isEnabled (a PhysicsContact feature) boolean determines if the actual collision happens or not.

So, check it out and see how it works for you. If you have any other questions, let me know.

Brent

[code]
local physics = require(“physics”) ; physics.start()
physics.setGravity( 0.0, 2.0 ) ; physics.setDrawMode( “hybrid” )

local function ballCollide( self, event )
–print(event.otherElement)
if( event.otherElement == 1 ) then
event.contact.isEnabled = false
end
return true
end

local box = display.newRect(0,0,200,100) ; box:setFillColor(255,0,0)
local bodyPart1 = { -150,0, 20,0, 20,34, -150,34 }
local bodyPart2 = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }

physics.addBody( box, “static”,
{ density=3.0, friction=0.5, bounce=0.1, shape=bodyPart1 },
{ density=4.0, friction=0.5, bounce=0.1, shape=bodyPart2 }
)
box.x = 140 ; box.y = 200

local ball = display.newCircle( testGroup, 150, 700, 10 ) ; ball:setFillColor(255,100,100)
physics.addBody( ball, “dynamic”, { density=1.0, friction=0.3, bounce=0.4, radius=10 } )
ball.preCollision = ballCollide ; ball:addEventListener( “preCollision”, ball )
ball.x = 120 ; ball.y = 0
[/code] [import]uid: 200026 topic_id: 34019 reply_id: 135365[/import]

Brent, this seems to be exactly what I am looking for :slight_smile: I will give this a try soon then let you know how I got on.

Thanks a lot
Rich [import]uid: 125022 topic_id: 34019 reply_id: 135410[/import]

This works perfectly Brent, thanks a lot :slight_smile: I just spot your article on physics events contact too (http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/) - wish I had seen it sooner :slight_smile:

I’ve shared my solution below.

 physics.addBody( character, "static",   
 { density=1.0, bounce=0.4, friction=0.15, shape=leftBody },  
 { density=1.0, bounce=0.4, friction=0.15, shape=rightBody },  
 { density=1.0, bounce=0.4, friction=0.15, shape=frontFacingBody }  
 )  
  
 local onDroppablePreCollision = function( self, event )  
 if character.state.direction == "F" then -- front-facing  
 if event.otherElement == 1 or event.otherElement == 2 then  
 event.contact.isEnabled = false  
 end  
 elseif character.state.direction == "R" then -- facing right  
 if event.otherElement == 2 or event.otherElement == 3 then  
 event.contact.isEnabled = false  
 end  
 elseif character.state.direction == "L" then -- facing left  
 if event.otherElement == 1 or event.otherElement == 3 then  
 event.contact.isEnabled = false  
 end  
 end  
 end  

Many Thanks,
Rich [import]uid: 125022 topic_id: 34019 reply_id: 135425[/import]