Hope this helps…
main.lua:
[lua]-- https://developer.anscamobile.com/content/game-edition-physics-joints
– https://developer.anscamobile.com/code/maths-library
– engage
physics = require(“physics”)
physics.start()
physics.setGravity(0,10)
physics.setDrawMode(“hybrid”)
– the anchor
local anchor = display.newCircle( 5,5,5 )
– the squares
local red, green, blue = display.newRect( 0, 0, 100, 100 ), display.newRect( 0, 0, 100, 100 ), display.newRect( 0, 0, 100, 100 )
red.x, red.y = 100, 300
green.x, green.y = 300, 300
blue.x, blue.y = 500, 300
red:setFillColor( 255, 0, 0 )
green:setFillColor( 0, 255, 0 )
blue:setFillColor( 0, 0, 255 )
– the physics bodies
physics.addBody( anchor, “static” )
physics.addBody( red, “dynamic” )
physics.addBody( green, “dynamic” )
physics.addBody( blue, “dynamic” )
– the physics joints - hold the wheels in place
local redwheel = physics.newJoint( “pivot”, anchor, red, red.x, red.y )
local greenwheel = physics.newJoint( “pivot”, anchor, green, green.x, green.y )
local bluewheel = physics.newJoint( “pivot”, anchor, blue, blue.x, blue.y )
– drive the red wheel with a motor (the easy one)
redwheel.isMotorEnabled = true
redwheel.maxMotorTorque = 100000
redwheel.motorSpeed = 100
– touch joints to move the green and blue squares with
local greentouch = physics.newJoint( “touch”, green, green.x, green.y+100 )
local bluetouch = physics.newJoint( “touch”, blue, blue.x, blue.y+100 )
– create a display group to be used as a rotation reference for the green square
local rotategroup = display.newGroup()
rotategroup.x, rotategroup.y = green.x, green.y
– display group powered rotation of the green square
function rotateGreen()
– rotate the display group
rotategroup.rotation = rotategroup.rotation + 15
– convert a fixed point in the group to a world coordinate location
local x, y = rotategroup:localToContent( 0, 100 )
– perform the touch joint rotation of the blue wheel
greentouch:setTarget( x, y )
end
– function to calculate rotation (used to rotate the blue square)
– taken and modified slightly from my math library: https://developer.anscamobile.com/code/maths-library
function rotatePoint( x, y, degrees )
local theta = math.rad( degrees )
local px = x * math.cos(theta) - y * math.sin(theta)
local py = x * math.sin(theta) + y * math.cos(theta)
return px, py
end
– calculation-powered rotation of the blue square
function rotateBlue()
– calculate the rotation from the current rotation
local x, y = rotatePoint( 0, 100, blue.rotation + 15 ) – rotates around 0,0
– perform the touch joint rotation of the blue square
bluetouch:setTarget( x + blue.x, y + blue.y )
end
– timers to call the rotation functions
timer.performWithDelay( 100, rotateGreen, 0 )
timer.performWithDelay( 100, rotateBlue, 0 )
– drops a ball into the world
– gets auto-removed if it drops off the bottom of the world
function dropBall( event )
local ball = display.newCircle( event.x, event.y, 30 )
ball:setFillColor( 0,0,0,0 )
ball:setStrokeColor( 255, 0, 0, 255 )
ball.strokeWidth = 5
physics.addBody( ball, “dynamic”, { radius=30} )
function ball:timer()
if (ball.y > display.contentHeight + 200) then
print(‘removed’)
timer.cancel( ball.t )
ball.t = nil
ball:removeSelf()
end
end
ball.t = timer.performWithDelay( 1000, ball, 0 )
return true
end
Runtime:addEventListener( “tap”, dropBall )
– lets you rotate the squares
function touch( event )
if (event.phase == “began”) then
event.target.touchjoint = physics.newJoint( “touch”, event.target, event.x, event.y )
event.target.touchjoint.dampingRatio = 0.2
event.target.touchjoint.frequency = 50
event.target.touchjoint.maxForce = 5000
display.getCurrentStage():setFocus( event.target )
elseif (event.phase == “moved”) then
print(event.phase, event.x, event.y )
event.target.touchjoint:setTarget( event.x, event.y )
else
event.target.touchjoint:removeSelf()
event.target.touchjoint = nil
display.getCurrentStage():setFocus( nil )
end
return true
end
– add touch listeners to allow the user to rotate the squares
red:addEventListener( “touch”, touch )
green:addEventListener( “touch”, touch )
blue:addEventListener( “touch”, touch )[/lua]
[import]uid: 8271 topic_id: 6677 reply_id: 100081[/import]