Touch joint, making object rotate accordingly?

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]

Also, is there a way to always move the rectangle at a constant speed? Right now it’s possible to make it very slow after a few touches and that’s a problem. Setting the speed variable atm doesnt seem to help

Hi @marcusresell,

Can you describe in more detail what you’re ideal behavior is? Do you want the rectangle to be a constant, fixed distance away from the dot, like it’s the tip of a clock hand rotating around? Or, should the distance be variable?

When the user touches the screen, do you want the rectangle to immediately change directions and move toward the touch at a fixed speed?

Best regards,

Brent Sorrentino

Hello Brent, thanks for your reply.

I do not want the rectangle to be at a constant fixed distance, the touch joint where the target is set to the circle in the middle has many of the features I’m after, so the distance shall be variable for a more smooth effect.

When the user touches the screen the rectangle shall change direction towards the circle in the middle and depending on how long the user holds down, the more it rotates towards the circle. And the moving speed shall always be the same, touch or not. The touch shall only affect the path of the rectangle depending on where it is relatively to the circle when it occurs. Will try to find an example somehwere

If you runt he code at the moment, the rectangle doesnt rotate its body at all, when the joint makes it change it’s course. I want it to rotate accordingly. I suck at explaining. Blah.

Second Edit:

Maybe i could use some kind of magnetic force field or similar?

Also, is there a way to always move the rectangle at a constant speed? Right now it’s possible to make it very slow after a few touches and that’s a problem. Setting the speed variable atm doesnt seem to help

Hi @marcusresell,

Can you describe in more detail what you’re ideal behavior is? Do you want the rectangle to be a constant, fixed distance away from the dot, like it’s the tip of a clock hand rotating around? Or, should the distance be variable?

When the user touches the screen, do you want the rectangle to immediately change directions and move toward the touch at a fixed speed?

Best regards,

Brent Sorrentino

Hello Brent, thanks for your reply.

I do not want the rectangle to be at a constant fixed distance, the touch joint where the target is set to the circle in the middle has many of the features I’m after, so the distance shall be variable for a more smooth effect.

When the user touches the screen the rectangle shall change direction towards the circle in the middle and depending on how long the user holds down, the more it rotates towards the circle. And the moving speed shall always be the same, touch or not. The touch shall only affect the path of the rectangle depending on where it is relatively to the circle when it occurs. Will try to find an example somehwere

If you runt he code at the moment, the rectangle doesnt rotate its body at all, when the joint makes it change it’s course. I want it to rotate accordingly. I suck at explaining. Blah.

Second Edit:

Maybe i could use some kind of magnetic force field or similar?