If i have a rectangle circling around a small dot in the middle when i touch, how do i make the rectangle body rotate accordingly? Right now it doesnt rotate at all, and using object.rotating = object.rotating +1 etc is way too bad. Any ideas?
Code:
[lua] local background = display.newRect( 0, 0, 480, 320 )
background:setFillColor( 0, 0, 0 )
–The Dot
local dot
–The playerangle
local player
–In case you need physics
local physics = require “physics”
physics.start()
physics.setGravity( 0, 0 )
physics.setDrawMode( “hybrid” )
–Math functions
local Cos = math.cos
local Sin = math.sin
local Rad = math.rad
local Atan2 = math.atan2
local Deg = math.deg
local radius = 10
local speed = 1
local angle = 90 --Start angle
local function getYref()
local xDist = math.abs(dot.x - player.x)
local yDist = math.abs(dot.y - player.y)
local totalDist = math.sqrt( xDist*xDist + yDist*yDist )
return totalDist
end
local function setSpeedBackToNormal()
speed = 1.0
end
– Add a method to the foo object to handle the touch event
local function onTouch( event )
if event.phase == “began” then
display.getCurrentStage():setFocus( player )
player.isFocus = true
Runtime:addEventListener( “enterFrame”, doTurn )
elseif player.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” then
Runtime:removeEventListener( “enterFrame”, doTurn )
display.getCurrentStage():setFocus( nil )
player.isFocus = false
setSpeedBackToNormal()
end
end
end
function doTurn()
speed = 0.5
local joint2 = physics.newJoint( “touch”, player, player.x, player.y )
joint2.frequency = 0.2
joint2.dampingRatio = 0
joint2:setTarget( dot.x, dot.y )
joint2:removeSelf()
setSpeedBackToNormal()
end
local function moveForward()
player:translate(Cos(math.rad(player.rotation))*speed,Sin(math.rad(player.rotation))*speed )
--player.y = player.y + Sin(math.rad(player.rotation))*speed
end
dot = display.newCircle(display.contentWidth/2,display.contentHeight/2,5)
dot:setFillColor(200,0,0)
–The playerangle (could be an image)
player = display.newRect(30,20,30, 30)
player:setReferencePoint(display.CenterReferencePoint)
player.name = “player”
physics.addBody(player)
physics.addBody( dot, “static” ) ; dot.isSensor = true
–local box1 = display.newRect( 50,50,100,100 ) ; box1:setFillColor(255,255,255,100)
–physics.addBody( box1, “static” ) ; box1.isSensor = true
–local box2 = display.newRect( 75,110,50,200 ) ; box2:setFillColor(255,255,255,100)
–physics.addBody( box2 )
–local joint = physics.newJoint( “pivot”, box1, box2, 100, 100 )
–box2:applyAngularImpulse( 20 )
–Run the animation
Runtime:addEventListener( “enterFrame”, moveForward )
Runtime:addEventListener( “touch”, onTouch) [/lua]