user3258079
user3258079

Reputation: 3

Rotating around a fixed point from current position

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)

Upvotes: 0

Views: 117

Answers (1)

user3258079
user3258079

Reputation: 3

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.

Upvotes: 0

Related Questions