Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Snippet: Get the distance of 2 vectors/sprites - Gideros Forum

Snippet: Get the distance of 2 vectors/sprites

MikeHartMikeHart Guru
edited January 2012 in Code snippets
Hi folks,

this snipped will calculate the distance in pixel of two given vector coordinates:
local vecDist = function(x, y, tx, ty)
	--Calculate the distance between 2 vectors
	local xdiff = x - tx
	local ydiff = y - ty
	local dist = (xdiff*xdiff+ydiff*ydiff)^0.5
	return dist
end

Likes: gorkem

+1 -1 (+1 / -0 ) Share on Facebook

Comments

  • And here a function to calculate the distance between two sprites:
    local getSpriteDist = function(source, target)
    	--Calculate the distance between 2 vectors
    	local xdiff = source:getX() - target:getX()
    	local ydiff = source:getY() - target:getY()
    	local dist = (xdiff*xdiff+ydiff*ydiff)^0.5
    	return dist
    end
Sign In or Register to comment.