removing only one element of a multi-element physics body object?

Greetings,

I’m curious if this is possible, and if so, how to do it.

I have one display object, setup with multiple-element physics bodies.


local object = display.newRect(0,0,600,600) local shape1 = { 0,-280, -75,-270, -75,-250, 0, -260 } local shape2 = { -70,-270, -170,-225, -155,-215, -70, -250 } local shape3 = { -230,-160, -268,-70, -250,-70, -215, -150 } local shape4 = { -268,-80, -275,40, -260,40, -250, -80 } local shape5 = { -275,30, -255,125, -240,125, -260, 30 } local shape6 = { -252,120, -200,200, -180,200, -235, 120 } physics.addBody( object, "static",     { density=6.0, friction=0.6, bounce=0.2, shape=shape1, filter = goThruFilter },     { density=6.0, friction=0.6, bounce=0.2, shape=shape2, filter = goThruFilter },     { density=6.0, friction=0.6, bounce=0.2, shape=shape3, filter = goThruFilter },     { density=6.0, friction=0.6, bounce=0.2, shape=shape4, filter = goThruFilter },     { density=6.0, friction=0.6, bounce=0.2, shape=shape5, filter = goThruFilter },     { density=6.0, friction=0.6, bounce=0.2, shape=shape6, filter = goThruFilter } )

I tried searching but can’t find an answer… but say if I just want to remove the physics body element ‘shape3’… but keep the rest.  Actually would be making it ‘object.isBodyActive’ but want to apply it only to ONE of those elements… This possible and how? Thanks!

Hi @henson802,

Once you create the multi-element body, you can’t remove just one part of it. You also can’t set just one element to inactive, since calling “.isBodyActive()” after creating it will toggle the entire body to active or inactive.

What you can do is use pre-collision and the PhysicsContact, then detect which element number is just about to register a collision, and if it’s the element that you want “off” or “ignored”, simply tell the PhysicsContact to disregard the collision entirely. That will make the simulation act as if that piece doesn’t really exist, even though it technically does.

Brent

Thanks Brent, I’ll take a look at your method to achieve what I had in mind.

Appreciate it

@Brent Sorrentino

Running into some issues now because the multi-element physics body is colliding with a Particle System object.  So it doesn’t seem to recognize pre-collision stuff with that.  If I substitute the Particle System with a circle display physics object, then all works as expected.

I tried setting up a collision detection with the Particle System… then using “event.element” to detect the collision element it makes contact with.  Which I can, but it doesn’t have the PhysicsContact property so that doesn’t really fit the ultimate goal of being able to pass thru one collision element and not the others…

It sounds strange but the multi-element method is best way to do this if it’s possible.  I suppose I could create a table of objects with their own physics body instead of using the multi-element method… but be my last resort as they are organized in a way to correspond with a rotating circle.  So I’d have to rotate all the objects at once to sync them correctly vs just having one object with multiple physics body elements…

*** Basically at any given time, I want to be able to have objects in the Particle System to be able to pass-thru a designated physics body element as if only that element was a sensor, but not the others.

Any idea about how to achieve this with a Particle System and a multi-element physics body?

Hi @henson802,

Hmmm… you are correct that particles don’t register pre-collisions (it might not even be possible within the LiquidFun framework), and thus the method won’t work the same.

Two alternatives to consider in this case:

  1. Does your main multi-element body actually need physical (force/impulse) reaction from colliding with the particles, or does it just need to sense that it collided with them? If the latter, and you can make the elements into sensors, you could just ignore the collision case for a “dead” element part in your Lua code and the body would show no physical reaction to the collision.

  2. Construct the multi-element body from unique bodies and join them with weld joints. While welded bodies can show a tiny bit of “flex” under extreme collision forces, it takes a really heavy hit and most users would never see that jiggle, if it even occurred. I know this isn’t necessary the “ideal” solution, but it would let you actually destroy (completely) any “dead” parts of the overall body.

Brent

Hey @Brent Sorrentino

Thanks for following up…

The Particles need to be able to collide (bounce off) with all the elements of the physics body of the object, except one… then at some point, it’ll switch so the Particles can collide with all elements except another one… so on and so forth.

The game itself is a rather simple concept but confusing to execute because it’s based off a circle which rotates.  And since you can’t make a hollow circular physics body, I’m creating the circle with multiple physics body elements that have custom rectangular shapes which outline the circle (they don’t need to be super precise).  At any given time - I need parts of the circle let the particles through, while keeping them out in other areas. And since this circle rotates, it kind of compounds the situation because the physics bodies need to rotate on same axis, hence why I attempted the multi-element method.

I think what I’ll probably end up doing is making multiple circle objects in a table or array - make each circle at same location so it rotates correctly, and make the circle objects invisible, and use 1 custom shape physics body to place on the outline of the ‘main’ visible circle.  It’d require probably 10 or so invisible circle objects - each with one custom physics shape body placed at the outter edge of the main visible circle… but I can’t see any other ideal way to accomplish this.  And it’d probably be easier to switch each to a sensor when it needs to.  It’ll be interesting to see how much memory/intensive this method consumes… but don’t think I have many other options.

Appreciate the input, helps me think it through :slight_smile:

Is this kind of what you’re going for?

-- simple rotation around an axis with both touch and enterFrame listeners local r = 180 local numPoints = 65 local xCenter = 200 local yCenter = 200 local rG = 1 -- starting amount of rotation local rotG = 5.5 -- rate at which objects will rotate local angleStep = 2 \* math.pi / numPoints local group1 = display.newGroup() local t = {} local circle = {} for i = 1, numPoints do circle[i] = display.newCircle(0, 0, 15) --circle[i].x = xCenter + radius\*math.cos(i\*angleStep) --circle[i].y = yCenter + radius\*math.sin(i\*angleStep) group1:insert( circle[i] ) end circle[2]:setFillColor(1,0,0) circle[2].width = 40 circle[2].height = 40 circle[2]:toFront() group1.x = display.contentCenterX group1.y = display.contentCenterY local function onSpin( ) rG = rG+rotG for i = 1, numPoints do t[i] = ((rG) \* .014)+ (i\*angleStep) circle[i].x = (r \* math.cos(t[i]))+50 circle[i].y = (r \* math.sin(t[i]))+200 print("circle["..i.."].x = "..circle[i].x) print("circle["..i.."].y = "..circle[i].y) end end Runtime:addEventListener( "enterFrame", onSpin )

The composite circle looks a little janky, I’ll admit, but at least you can control the circles and set a specific number to be sensor bodies as you want.

You could also use the above, but make all the circles invisible, and create a circle graphic yourself that does “look right”, and just overlay that.

If this isn’t what you’re going for, my apologies for misunderstanding.

Hey @Alex@Panc

Sorta!  The white in your demo would be the unpassable parts…  And the red would be passable (albeit slower).

Right now I’m using one circle graphic I made… much like the white part of that.  Then instead of that red, I’m using a rect mask on various parts of the border of my circle graphic to resemble a ‘hole’ through the border.  It’ll randomly appear on various parts of the circle border (there’s more to the code behind these parts)… So in theory, I’d like to have whatever physics body on that border part that the mask is on to become a sensor.  Since it’s rotating, it’d be smoothest if I used one object with various multi-element physics bodies.  To me that would have made things a little easier but I think I got it with a few more adjustments that aren’t too intensive.  

I’ll try to post up a video of the demo next week (or whenever the project is near completion :huh: ) ,depending on time !

Thanks for the input alex!

Hi @henson802,

Once you create the multi-element body, you can’t remove just one part of it. You also can’t set just one element to inactive, since calling “.isBodyActive()” after creating it will toggle the entire body to active or inactive.

What you can do is use pre-collision and the PhysicsContact, then detect which element number is just about to register a collision, and if it’s the element that you want “off” or “ignored”, simply tell the PhysicsContact to disregard the collision entirely. That will make the simulation act as if that piece doesn’t really exist, even though it technically does.

Brent

Thanks Brent, I’ll take a look at your method to achieve what I had in mind.

Appreciate it

@Brent Sorrentino

Running into some issues now because the multi-element physics body is colliding with a Particle System object.  So it doesn’t seem to recognize pre-collision stuff with that.  If I substitute the Particle System with a circle display physics object, then all works as expected.

I tried setting up a collision detection with the Particle System… then using “event.element” to detect the collision element it makes contact with.  Which I can, but it doesn’t have the PhysicsContact property so that doesn’t really fit the ultimate goal of being able to pass thru one collision element and not the others…

It sounds strange but the multi-element method is best way to do this if it’s possible.  I suppose I could create a table of objects with their own physics body instead of using the multi-element method… but be my last resort as they are organized in a way to correspond with a rotating circle.  So I’d have to rotate all the objects at once to sync them correctly vs just having one object with multiple physics body elements…

*** Basically at any given time, I want to be able to have objects in the Particle System to be able to pass-thru a designated physics body element as if only that element was a sensor, but not the others.

Any idea about how to achieve this with a Particle System and a multi-element physics body?

Hi @henson802,

Hmmm… you are correct that particles don’t register pre-collisions (it might not even be possible within the LiquidFun framework), and thus the method won’t work the same.

Two alternatives to consider in this case:

  1. Does your main multi-element body actually need physical (force/impulse) reaction from colliding with the particles, or does it just need to sense that it collided with them? If the latter, and you can make the elements into sensors, you could just ignore the collision case for a “dead” element part in your Lua code and the body would show no physical reaction to the collision.

  2. Construct the multi-element body from unique bodies and join them with weld joints. While welded bodies can show a tiny bit of “flex” under extreme collision forces, it takes a really heavy hit and most users would never see that jiggle, if it even occurred. I know this isn’t necessary the “ideal” solution, but it would let you actually destroy (completely) any “dead” parts of the overall body.

Brent

Hey @Brent Sorrentino

Thanks for following up…

The Particles need to be able to collide (bounce off) with all the elements of the physics body of the object, except one… then at some point, it’ll switch so the Particles can collide with all elements except another one… so on and so forth.

The game itself is a rather simple concept but confusing to execute because it’s based off a circle which rotates.  And since you can’t make a hollow circular physics body, I’m creating the circle with multiple physics body elements that have custom rectangular shapes which outline the circle (they don’t need to be super precise).  At any given time - I need parts of the circle let the particles through, while keeping them out in other areas. And since this circle rotates, it kind of compounds the situation because the physics bodies need to rotate on same axis, hence why I attempted the multi-element method.

I think what I’ll probably end up doing is making multiple circle objects in a table or array - make each circle at same location so it rotates correctly, and make the circle objects invisible, and use 1 custom shape physics body to place on the outline of the ‘main’ visible circle.  It’d require probably 10 or so invisible circle objects - each with one custom physics shape body placed at the outter edge of the main visible circle… but I can’t see any other ideal way to accomplish this.  And it’d probably be easier to switch each to a sensor when it needs to.  It’ll be interesting to see how much memory/intensive this method consumes… but don’t think I have many other options.

Appreciate the input, helps me think it through :slight_smile:

Is this kind of what you’re going for?

-- simple rotation around an axis with both touch and enterFrame listeners local r = 180 local numPoints = 65 local xCenter = 200 local yCenter = 200 local rG = 1 -- starting amount of rotation local rotG = 5.5 -- rate at which objects will rotate local angleStep = 2 \* math.pi / numPoints local group1 = display.newGroup() local t = {} local circle = {} for i = 1, numPoints do circle[i] = display.newCircle(0, 0, 15) --circle[i].x = xCenter + radius\*math.cos(i\*angleStep) --circle[i].y = yCenter + radius\*math.sin(i\*angleStep) group1:insert( circle[i] ) end circle[2]:setFillColor(1,0,0) circle[2].width = 40 circle[2].height = 40 circle[2]:toFront() group1.x = display.contentCenterX group1.y = display.contentCenterY local function onSpin( ) rG = rG+rotG for i = 1, numPoints do t[i] = ((rG) \* .014)+ (i\*angleStep) circle[i].x = (r \* math.cos(t[i]))+50 circle[i].y = (r \* math.sin(t[i]))+200 print("circle["..i.."].x = "..circle[i].x) print("circle["..i.."].y = "..circle[i].y) end end Runtime:addEventListener( "enterFrame", onSpin )

The composite circle looks a little janky, I’ll admit, but at least you can control the circles and set a specific number to be sensor bodies as you want.

You could also use the above, but make all the circles invisible, and create a circle graphic yourself that does “look right”, and just overlay that.

If this isn’t what you’re going for, my apologies for misunderstanding.

Hey @Alex@Panc

Sorta!  The white in your demo would be the unpassable parts…  And the red would be passable (albeit slower).

Right now I’m using one circle graphic I made… much like the white part of that.  Then instead of that red, I’m using a rect mask on various parts of the border of my circle graphic to resemble a ‘hole’ through the border.  It’ll randomly appear on various parts of the circle border (there’s more to the code behind these parts)… So in theory, I’d like to have whatever physics body on that border part that the mask is on to become a sensor.  Since it’s rotating, it’d be smoothest if I used one object with various multi-element physics bodies.  To me that would have made things a little easier but I think I got it with a few more adjustments that aren’t too intensive.  

I’ll try to post up a video of the demo next week (or whenever the project is near completion :huh: ) ,depending on time !

Thanks for the input alex!