Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can anyone spot the obvious bug with this math (trig)? — Gideros Forum

Can anyone spot the obvious bug with this math (trig)?

Tom2012Tom2012 Guru
edited August 2013 in General questions
Hey everyone. Got what should be a simple problem but I'm pulling out my hair as to why I'm getting the results I am.

A character walks at an angle to a point on the screen:

local nextX = self.scene.path[self.id].vertices.x[self.pathCounter]
local nextY = self.scene.path[self.id].vertices.y[self.pathCounter]

I'm using 'distance' to stop him when he gets with a range.

But he never actually gets there. Instead, there is always a distance of around 16 pixels.

Any help very much appreciated.
function CreeperBug:onEnterFrame()
 
	local currentX,currentY = self:getPosition()
 
	local nextX = self.scene.path[self.id].vertices.x[self.pathCounter]
	local nextY = self.scene.path[self.id].vertices.y[self.pathCounter]
 
	local xDiff = nextX - currentX
	local yDiff = nextY - currentY
 
	local distance = math.sqrt((xDiff*xDiff)+(yDiff*yDiff))
 
	local angle = math.deg(math.atan2(yDiff,xDiff))
 
	local nextX = currentX + (math.cos(angle) * 1)
	local nextY = currentY + (math.sin(angle) * 1)
 
	if(self.phase == "walk") then
		self:setPosition(nextX, nextY)
	end
 
end

Comments

  • @Tom2012
    my most common mistake is forgetting that Gideros has Cartesian coordinate system, thus y axis is always inverted

    Other usual problem is that some math function might work not from the 0 to 360 degrees as expected, but for example from -180 to 180 degrees.

    This is my usual way on getting angle:
    local angle = math.acos(yDiff/distance)
    if(xDiff >0) then
    	angle = -angle
    end

    Likes: Tom2012

    +1 -1 (+1 / -0 )Share on Facebook
  • Distance between 2 points

    local x1, y1 = 100, 100
    local x2, y2 = 200, 200

    local distanceBetween = math.ceil(math.sqrt( ((y1-y2) ^ 2) + ((x1-x2) ^ 2) ))

    Likes: Tom2012

    +1 -1 (+1 / -0 )Share on Facebook
  • Tom2012Tom2012 Guru
    edited August 2013
    Thanks guys, still having problems with this (tried both suggestions)

    Here's a simple code that produces the problem I have:
    local apple = Bitmap.new(Texture.new("apple.png"))
    apple:setAnchorPoint(0,0)
    stage:addChild(apple)
     
    local pumpkin = Bitmap.new(Texture.new("pumpkin.png"))
    pumpkin:setAnchorPoint(0,0)
    stage:addChild(pumpkin)
    pumpkin:setPosition(100,100)
     
    local angle = 45
    local speed = 5
     
    local x1, y1 = apple:getPosition()
    local x2, y2 = pumpkin:getPosition()
     
    local nextX = x1+(math.cos(angle) * speed)
    local nextY = y1+(math.sin(angle) * speed)
     
    Timer.delayedCall(1000, function()
    	apple:setPosition(nextX,nextY)
    end)
  • adityaaditya Member
    edited August 2013 Accepted Answer
    apple:setPosition is being called once in your code.

    This should work.
     
    local apple = Bitmap.new(Texture.new("apple.png"))
    apple:setAnchorPoint(0,0)
    stage:addChild(apple)
     
    local pumpkin = Bitmap.new(Texture.new("pumpkin.png"))
    pumpkin:setAnchorPoint(0,0)
    stage:addChild(pumpkin)
    pumpkin:setPosition(100,100)
     
     
    local speed = 5 
    local angle,x1,y1,x2,y2,nextX,nextY
     
    function checkReached(x1,y1,x2,y2)
    	local res = false
    	if((x1>=x2-2 and x1<=x2+2) and (y1>=y2-2 and y1<=y2+2)) then
    		res = true
    	end
    	return res
    end
     
    function onEnterFrame(e)
    	x1, y1 = apple:getPosition()
    	x2, y2 = pumpkin:getPosition()
    	print(x1 .. " " .. y1 .. "   |   " .. x2 .. " " ..y2)
     
    	if(checkReached(x1,y1,x2,y2)) then
    		stage:removeEventListener(Event.ENTER_FRAME, onEnterFrame)
    	else
    		angle = math.atan2(y2-y1,x2-x1)
     
    		nextX = x1+(math.cos(angle) * speed)
    		nextY = y1+(math.sin(angle) * speed)
     
    		apple:setPosition(nextX,nextY)
    	end
     
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

    Likes: Tom2012

    Loon Games LinkedIn Facebook Twitter - "Something bit me, gaah!"
    +1 -1 (+1 / -0 )Share on Facebook
  • @aditya

    Thank you! That works perfectly. Will be using that code in my game.

    I'm embarrassed to say I was passing degrees rather than radians to math.cos / sin.

    I thought was getting better at this stuff. :\">

    Thank you again for the replies and help folks.
Sign In or Register to comment.