Learning Gideros (and Lua) : Porting Advanced AS3 Animations code

Today I had a chance to play with Gideros SDK. In order to get a better understanding about the SDK and Lua in general, I decided to port some AS3 code. Here are some code from Keith Peter’s book, Advanced AS3 animations, in Lua.

I haven’t fully understood Lua so these code might look bad in the eyes of Lua experts 🙂 but at least it works.

[note color=”#FFCC00″]You can get the original, fully commented AS3 code from http://www.apress.com/9781430216087[/note]

First, the Vector2D. I don’t know if Lua has a built-in Vector class, maybe it does but as part of the learning process, I wrote one myself. Porting Vector2D.as to Vector2D.lua was almost as easy as copy-pasting.

--[[
Vector 2D class. Ported to Lua from Keith Peter's AS3 Vector2D
Author : Anggie Bratadinata | www.masputih.com
--]]

Vector2D = {}
Vector2D.__index = Vector2D

-- constructor
function Vector2D.new(initX,initY)
	local instance = {
		x = initX,
		y = initY
	}
	setmetatable(instance,Vector2D)
	return instance
end

--[[
	BASIC MATH OPS
--]]

function Vector2D:dotProduct(v2)
	return self.x * v2.x + self.y * v2.y
end

function Vector2D:angleFrom(v2)

	local v1 = self
	
	if self:isNormalized() == true 
	then
		v1 = self:clone()
		v1:normalize()
	end
	
	if v2:isNormalized() == true
	then
		v2 = v2:clone()
		v2:normalize()
	end
	
	return math.acos(v1:dotProd(v2))
	
end

function Vector2D:add(v2)
	self.x = self.x + v2.x
	self.y = self.y + v2.y
end

function Vector2D:subtract(v2)
	self.x = self.x - v2.x
	self.y = self.y - v2.y
end

function Vector2D:multiply(value)
	self.x = self.x * value
	self.y = self.y * value
end

function Vector2D:divide(value)
	self.x = self.x / value
	self.y = self.y / value
end

--[[
	LENGTH
--]]
function Vector2D:setLength(value)
	local a = self:getAngle()
	self.x = math.cos(a) * value
	self.y = math.sin(a) * value
end

function Vector2D:getLength()
	return math.sqrt(self:getSquareLength())
end

function Vector2D:getSquareLength()
	return self.x * self.x + self.y * self.y
end

-- normalization
function Vector2D:normalize()
	if self:getLength() == 0
	then
		self.x = 1
		self.y = 0
	else
		local l = self:getLength()
		self.x = self.x/l
		self.y = self.y/l
	end
	return self
end

function Vector2D:isNormalized()
	return self:getLength() == 1
end

--[[
	ANGLE
--]]
function Vector2D:getAngle()
	return math.atan2(self.y,self.x)
end

function Vector2D:setAngle(value)
	local l = self:getLength()
	self.x = math.cos(value) * l
	self.y = math.sin(value) * l
end

--[[
	DISTANCE
--]]

-- get the distance of the given vector from this vector
function Vector2D:getDistanceOf(v2)
	return math.sqrt(self:getSquareDistance(v2))
end

function Vector2D:getSquareDistance(v2)
	local dx = v2.x - self.x
	local dy = v2.y - self.y
	return dx * dx + dy * dy
end

--[[
	UTILITIES
--]]

-- determine if the given vector is to the right (+1) or to the left (-1)
-- of this vector
function Vector2D:getSignOf(v2)
	local perp = self:getPerpendicular()
	if perp:dotProduct(v2) < 0
	then
		return -1
	else
		return 1
	end
end

-- return a new vector that is perpendicular to this vector
function Vector2D:getPerpendicular()
	return Vector2D.new(-self.y,self.x)
end


function Vector2D:truncate(maxLength)
	self:setLength(math.min(maxLength,self:getLength()))
end

function Vector2D:zero()
	self.x = 0
	self.y = 0
end

function Vector2D:isZero()
	if self.x == 0 and self.y == 0
	then
		return true
	else
		return false
	end
	
end

function Vector2D:equals(v2)
	return self.x == v2.x and self.y == v2.y
end

function Vector2D:clone()
	return Vector2D.new(self.x,self.y)
end

function Vector2D:toString()
	print("Vector2D[",self.x,",",self.y,"]");
end

-- so that this class can be imported by "geom.Vector2D"
return Vector2D

Continue reading Learning Gideros (and Lua) : Porting Advanced AS3 Animations code