0

Need to move a sprite around a fixed point. The caveat is, the motion should start from the 'current position' of the sprite.

Offsetting the angle i.e.using magic numbers, doesn't really do it since it will be different in case the distance between the sprite and the fixed point is changed.

Reference Image:

enter image description here


    local block1Texture = Texture.new("block1.png",true)
    local block1 = Bitmap.new(block1Texture)
    block1:setAnchorPoint(0.5,0.5)
    stage:addChild(block1)
    block1:setPosition(50,50)

    local block2Texture = Texture.new("block2.png",true)
    local block2 = Bitmap.new(block2Texture)
    block2:setAnchorPoint(0.5,0.5)
    stage:addChild(block2)
    block2:setPosition(350,450)

    local block3Texture = Texture.new("block3.png",true)
    local block3 = Bitmap.new(block3Texture)
    block3:setAnchorPoint(0.5,0.5)
    stage:addChild(block3)
    block3:setPosition(300,700)

    local timer = Timer.new(500, 1)

    local rotateAroundBlock = block2

    function getCharAngleFromRope(startX, startY, targetX, targetY)  
        local xdiff = targetX - startX
        local ydiff = targetY - startY
            local ang = math.atan2( ydiff, xdiff )
        ang = math.deg(ang) + 90.0
        if ang =application:getDeviceWidth()) then
            --print("1")
            --px = block3:getX()
        end

        if(py=application:getContentHeight()) then
            --print("2")
            --py = block3:getY()
            --px = block3:getX()
        end

        block3:setPosition(px,py)
    end
    dist = math.sqrt((rotateAroundBlock:getX()-block3:getX())^2+(rotateAroundBlock:getY()-block3:getY())^2)
    angle = getCharAngleFromRope(block3:getX(),block3:getY(),rotateAroundBlock:getX(),rotateAroundBlock:getY())
    timer:addEventListener(Event.TIMER_COMPLETE, onTimerComplete)

    function startMoving()
        timer:start()
    end
    stage:addEventListener(Event.TOUCHES_BEGIN, startMoving)

Tim
  • 2,121
  • 2
  • 20
  • 30
  • 1
    Same as always: translate, rotate, translate. – Ignacio Vazquez-Abrams Mar 02 '15 at 05:50
  • If you just use a+rCos(t), b+rSin(t), and start t from 0 - 360, then it moves from that point around (a,b). Need it to move from it's current position, so am trying to calculate the current angle between the sprite and the fixed point, but am missing something since it doesn't start from the exact current position right now. – user3258079 Mar 02 '15 at 05:58
  • That's because the rotation doesn't happen around the current position, it happens around the origin. – Ignacio Vazquez-Abrams Mar 02 '15 at 06:02

1 Answers1

0

Solved. For anyone else stumbling on this, the parameter values need to be in radians and not degrees for math.cos and math.sin. Works like a charm.