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.
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)