Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Rotating around a fixed point — Gideros Forum

Rotating around a fixed point

adityaaditya Member
edited March 2015 in General questions
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.
image
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 < 0 then
		ang = 360.0 + ang
	end
	return ang
end
 
local dx = application:getLogicalTranslateX() / application:getLogicalScaleX()
local dy = application:getLogicalTranslateY() / application:getLogicalScaleY()
 
local function onTimerComplete(event)
	timer:reset()
	timer:start()
 
	if(rotateAroundBlock==block1) then
		angle = angle - 0.1
	else
		angle = angle + 0.1
	end
 
	local px = rotateAroundBlock:getX() + dist*math.cos(angle)
	local py = rotateAroundBlock:getY() + dist*math.sin(angle)
 
	if(px<=0 or px>=application:getDeviceWidth()) then
		--print("1")
		--px = block3:getX()
	end
 
	if(py<=0 or 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)
[Edit: Solved. math.cos and math.sin take parameters as radians and not degrees]
Loon Games LinkedIn Facebook Twitter - "Something bit me, gaah!"
Sign In or Register to comment.