Help! :/ body and physics body ripping apart on being moved

Hi All,

I have been trying to get this code for more than 2 weeks but with no success :wub: . Please note that I a newbie any help would be much appriciated. Thank you in advance for your time and help.   :slight_smile:

It seems that when I try to  rotate the a body the body and the physics body separates. Pls see the hybrid physics view of the object.  The code is as follows:  

local physics = require(“physics”)

physics.start( noSleep )

physics.setGravity(0, 0)

physics.setContinuous( enabled )

–physics.setVelocityIterations( 6 )

physics.setDrawMode( “hybrid” )

py=“static”

_W = display.contentWidth;

_H = display.contentHeight;

local lineGroup = display.newGroup()

myRoundedRect={}

iz=1

 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 )

 myRoundedRect[iz].anchorX = .5

 myRoundedRect[iz].anchorY = .5

 myRoundedRect[iz]:setFillColor( 0.5 )

 myRoundedRect[iz].x,  myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10

myRoundedRect[iz]:rotate( 0 )

  physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false})

myRoundedRect[iz].isSleepingAllowed = false

  lineGroup:insert(myRoundedRect[iz])

  

 iz=2

 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 )

 myRoundedRect[iz].anchorX = .5

 myRoundedRect[iz].anchorY = .5

 myRoundedRect[iz]:setFillColor( 0.5 )

 myRoundedRect[iz].x,  myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56

myRoundedRect[iz]:rotate( -120)

  physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false})

myRoundedRect[iz].isSleepingAllowed = false

 lineGroup:insert(myRoundedRect[iz])

  

  iz=3

 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 )

 myRoundedRect[iz].anchorX = .5

 myRoundedRect[iz].anchorY = .5

 myRoundedRect[iz]:setFillColor( 0.5 )

 myRoundedRect[iz].x,  myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56+112

myRoundedRect[iz]:rotate( -120*2)

  physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false})

 myRoundedRect[iz].isSleepingAllowed = false

  lineGroup:insert(myRoundedRect[iz])

  

   iz=4

 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 )

 myRoundedRect[iz].anchorX = .5

 myRoundedRect[iz].anchorY = .5

 myRoundedRect[iz]:setFillColor( 0.5 )

 myRoundedRect[iz].x,  myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10+225

myRoundedRect[iz]:rotate( -120*3)

  physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false})

 myRoundedRect[iz].isSleepingAllowed = false

  lineGroup:insert(myRoundedRect[iz])

  

   iz=5

 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 )

 myRoundedRect[iz].anchorX = .5

 myRoundedRect[iz].anchorY = .5

 myRoundedRect[iz]:setFillColor( 0.5 )

 myRoundedRect[iz].x,  myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56+112

myRoundedRect[iz]:rotate( -120*4)

  physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false})

 myRoundedRect[iz].isSleepingAllowed = false

  lineGroup:insert(myRoundedRect[iz])

  

  iz=6

 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 )

 myRoundedRect[iz].anchorX = .5

 myRoundedRect[iz].anchorY = .5

 myRoundedRect[iz]:setFillColor( 0.5 )

 myRoundedRect[iz].x,  myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56

myRoundedRect[iz]:rotate( -120*5)

 physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false})

   myRoundedRect[iz].isSleepingAllowed = false

   lineGroup:insert(myRoundedRect[iz])

  

lineGroup.anchorChildren = true

   lineGroup.isSleepingAllowed = false

 lineGroup.anchorX = .5

lineGroup.anchorY = .5

 lineGroup.x=_W/2

 lineGroup.y=_H/2

 local adjustment = 0

local PI = 3.14159265358

local function onTouch(e)

if(e.phase == “began”) then

local dx = e.x - lineGroup.x

local dy = e.y - lineGroup.y

adjustment =   math.atan2(dy,dx) * 180 / PI - lineGroup.rotation

end

if(e.phase == “moved”) then

local dx = e.x - lineGroup.x

local dy = e.y - lineGroup.y

lineGroup.rotation = (math.atan2(dy,dx) * 180 / PI)  - adjustment

–myText.text = lineGroup.rotation

end

end

Runtime:addEventListener(‘touch’, onTouch)

come on guys anyone…Rob and Brent pls guide the way 

Hi cgeorge,

You seem to be creating six individual bodies - I assume what you want is a single body, which is a hexagon.

If you want a solid/filled-in hexagon, you can declare that as a single body using addBody - check out the ‘polygonal body’ section half way down the page.

If you want to make an outline only (e.g. you may need to put something inside the hexagon and have it bounce around) but you want it to permanently be the hexagon shape you start with, check out multi-element bodies.

Finally, if you want the initial shape to be a hexagon, but for the sides to be able to deviate from that shape (e.g. you can ‘squish’ the hexagon) then you’ll want to use physics joints to connect the six separate rectangles.

Also, fyi, please use the ‘Code’ button (next to insert image, etc.) when making posts with code in them, as it’s much easier to follow and you’re more likely to get a response  :slight_smile:

Cheers,

Simon

Dixon Court Entertainment

Hi Simon,

Thanks man for the reply. You are right I am trying to: create an outline of a hexagon using multiple bodies which can rotate on touch.

I tried both multibody and weld…but I am not able to execute the code properly…the nearest I code get to it was this code I had posted earlier. I am totally new to physics…The problems I faced are 1) not able to join multiple elements using multibody. 2) dont know how to rotate multibody using physics…

Best I could do was this combination. Dont know how to fix this.

local physics = require("physics") physics.start( noSleep ) physics.setGravity(0, 0) physics.setContinuous( enabled ) --physics.setVelocityIterations( 6 ) physics.setDrawMode( "hybrid" ) py="static" \_W = display.contentWidth; \_H = display.contentHeight; local lineGroup = display.newGroup() myRoundedRect={} iz=1 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10 myRoundedRect[iz]:rotate( 0 ) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=2 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=3 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*2) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=4 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10+225 myRoundedRect[iz]:rotate( -120\*3) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=5 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*4) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=6 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120\*5) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) lineGroup.anchorChildren = true lineGroup.isSleepingAllowed = false lineGroup.anchorX = .5 lineGroup.anchorY = .5 lineGroup.x=\_W/2 lineGroup.y=\_H/2 local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - lineGroup.x local dy = e.y - lineGroup.y adjustment = math.atan2(dy,dx) \* 180 / PI - lineGroup.rotation end if(e.phase == "moved") then local dx = e.x - lineGroup.x local dy = e.y - lineGroup.y lineGroup.rotation = (math.atan2(dy,dx) \* 180 / PI) - adjustment --myText.text = lineGroup.rotation end end Runtime:addEventListener('touch', onTouch)

Hi,

So, just to clarify, do you want the whole hexagon(outline) to rotate as one body, or for the six sides of the hexagon to be able to rotate independently?

Cheers,

Simon

Hi Simon,

 

You are correct, I want the hexagon outline to rotate as a whole in the direction of touch.

 

Independent sides were made to allow detection of side (of the hexagon) on collision by an object.

 

In the current code, I am able to rotate the hexagon on touch, but the physics body of the hexagon gets all screwed up when I rotate (the snapshot of the hybrid view after rotating the Hexagon already attached).

 

Thanks

Hi @cgeorge.personal,

In this case, a multi-element physics body using per-element collision detection is your best option. See this tutorial on how to implement it:

http://coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Best regards,

Brent

Hi Brent

 

I tried both multibody and weld (Simon had also showed me this before)…but was not able to configure with multibody or even with weld…maybe because I am a newbie…another problem was how to rotate a physics body on touch. I am totally new to physics.

 

…the nearest I code could make get to it was this code I had posted earlier.The problems I faced are 1) not able to join multiple elements using multibody. 2) dont know how to rotate multibody using physics…

 

Dont know how to fix this.

 

Thanks,

George

Hi George,

Multi-element physics bodies must be constructed as one unified physics.addBody() call, not multiple calls. Within, you specify each part, using tables of vertices to define each part.

Perhaps the documentation will help more than the tutorial:

https://docs.coronalabs.com/api/library/physics/addBody.html#multi-element-body

Brent

come on guys anyone…Rob and Brent pls guide the way 

Hi cgeorge,

You seem to be creating six individual bodies - I assume what you want is a single body, which is a hexagon.

If you want a solid/filled-in hexagon, you can declare that as a single body using addBody - check out the ‘polygonal body’ section half way down the page.

If you want to make an outline only (e.g. you may need to put something inside the hexagon and have it bounce around) but you want it to permanently be the hexagon shape you start with, check out multi-element bodies.

Finally, if you want the initial shape to be a hexagon, but for the sides to be able to deviate from that shape (e.g. you can ‘squish’ the hexagon) then you’ll want to use physics joints to connect the six separate rectangles.

Also, fyi, please use the ‘Code’ button (next to insert image, etc.) when making posts with code in them, as it’s much easier to follow and you’re more likely to get a response  :slight_smile:

Cheers,

Simon

Dixon Court Entertainment

Hi Simon,

Thanks man for the reply. You are right I am trying to: create an outline of a hexagon using multiple bodies which can rotate on touch.

I tried both multibody and weld…but I am not able to execute the code properly…the nearest I code get to it was this code I had posted earlier. I am totally new to physics…The problems I faced are 1) not able to join multiple elements using multibody. 2) dont know how to rotate multibody using physics…

Best I could do was this combination. Dont know how to fix this.

local physics = require("physics") physics.start( noSleep ) physics.setGravity(0, 0) physics.setContinuous( enabled ) --physics.setVelocityIterations( 6 ) physics.setDrawMode( "hybrid" ) py="static" \_W = display.contentWidth; \_H = display.contentHeight; local lineGroup = display.newGroup() myRoundedRect={} iz=1 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10 myRoundedRect[iz]:rotate( 0 ) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=2 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=3 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*2) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=4 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10+225 myRoundedRect[iz]:rotate( -120\*3) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=5 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*4) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=6 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120\*5) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) lineGroup.anchorChildren = true lineGroup.isSleepingAllowed = false lineGroup.anchorX = .5 lineGroup.anchorY = .5 lineGroup.x=\_W/2 lineGroup.y=\_H/2 local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - lineGroup.x local dy = e.y - lineGroup.y adjustment = math.atan2(dy,dx) \* 180 / PI - lineGroup.rotation end if(e.phase == "moved") then local dx = e.x - lineGroup.x local dy = e.y - lineGroup.y lineGroup.rotation = (math.atan2(dy,dx) \* 180 / PI) - adjustment --myText.text = lineGroup.rotation end end Runtime:addEventListener('touch', onTouch)

Hi,

So, just to clarify, do you want the whole hexagon(outline) to rotate as one body, or for the six sides of the hexagon to be able to rotate independently?

Cheers,

Simon

Hi Simon,

 

You are correct, I want the hexagon outline to rotate as a whole in the direction of touch.

 

Independent sides were made to allow detection of side (of the hexagon) on collision by an object.

 

In the current code, I am able to rotate the hexagon on touch, but the physics body of the hexagon gets all screwed up when I rotate (the snapshot of the hybrid view after rotating the Hexagon already attached).

 

Thanks

Hi @cgeorge.personal,

In this case, a multi-element physics body using per-element collision detection is your best option. See this tutorial on how to implement it:

http://coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Best regards,

Brent

Hi Brent

 

I tried both multibody and weld (Simon had also showed me this before)…but was not able to configure with multibody or even with weld…maybe because I am a newbie…another problem was how to rotate a physics body on touch. I am totally new to physics.

 

…the nearest I code could make get to it was this code I had posted earlier.The problems I faced are 1) not able to join multiple elements using multibody. 2) dont know how to rotate multibody using physics…

 

Dont know how to fix this.

 

Thanks,

George

Hi George,

Multi-element physics bodies must be constructed as one unified physics.addBody() call, not multiple calls. Within, you specify each part, using tables of vertices to define each part.

Perhaps the documentation will help more than the tutorial:

https://docs.coronalabs.com/api/library/physics/addBody.html#multi-element-body

Brent