Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A simple Vector2D class used in Corona SDK games.

View Vector2D.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
Vector2D = {}
 
function Vector2D:new(x, y)
local object = { x = x, y = y }
setmetatable(object, { __index = Vector2D })
return object
end
 
function Vector2D:copy()
return Vector2D:new(self.x, self.y)
end
 
function Vector2D:magnitude()
return math.sqrt(self.x^2 + self.y^2)
end
 
function Vector2D:normalize()
local temp
temp = self:magnitude()
if temp > 0 then
self.x = self.x / temp
self.y = self.y / temp
end
end
 
function Vector2D:limit(l)
if self.x > l then
self.x = l
end
if self.y > l then
self.y = l
end
end
 
function Vector2D:equals(vec)
if self.x == vec.x and self.y == vec.y then
return true
else
return false
end
end
 
function Vector2D:add(vec)
self.x = self.x + vec.x
self.y = self.y + vec.y
end
 
function Vector2D:sub(vec)
self.x = self.x - vec.x
self.y = self.y - vec.y
end
 
function Vector2D:mult(s)
self.x = self.x * s
self.y = self.y * s
end
 
function Vector2D:div(s)
self.x = self.x / s
self.y = self.y / s
end
 
function Vector2D:dot(vec)
return self.x * vec.x + self.y * vec.y
end
 
function Vector2D:dist(vec2)
return math.sqrt( (vec2.x - self.x) + (vec2.y - self.y) )
end
 
-- Class Methods
 
function Vector2D:Normalize(vec)
local tempVec = Vector2D:new(vec.x,vec.y)
local temp
temp = tempVec:magnitude()
if temp > 0 then
tempVec.x = tempVec.x / temp
tempVec.y = tempVec.y / temp
end
return tempVec
end
 
function Vector2D:Limit(vec,l)
local tempVec = Vector2D:new(vec.x,vec.y)
if tempVec.x > l then
tempVec.x = l
end
if tempVec.y > l then
tempVec.y = l
end
return tempVec
end
 
function Vector2D:Add(vec1, vec2)
local vec = Vector2D:new(0,0)
vec.x = vec1.x + vec2.x
vec.y = vec1.y + vec2.y
return vec
end
 
function Vector2D:Sub(vec1, vec2)
local vec = Vector2D:new(0,0)
vec.x = vec1.x - vec2.x
vec.y = vec1.y - vec2.y
return vec
end
 
function Vector2D:Mult(vec, s)
local tempVec = Vector2D:new(0,0)
tempVec.x = vec.x * s
tempVec.y = vec.y * s
return tempVec
end
 
function Vector2D:Div(vec, s)
local tempVec = Vector2D:new(0,0)
tempVec.x = vec.x / s
tempVec.y = vec.y / s
return tempVec
end
 
function Vector2D:Dist(vec1, vec2)
return math.sqrt( (vec2.x - vec1.x) + (vec2.y - vec1.y) )
end
 
return Vector2D

Hello,
its a great work. I need some pointer from you.
I want to implement some racing game. for that I need to implement the track, on that track the player will run his car.
I am not able to implement the track which will run infinitely.
I am spites of the background i need to create the track from that and also need to auto scroll that background.
Please give me some advice how to this.

Vector2D:dist and Vector2D:Dist are wrong, here's the correct implementation of Euclidian distance:

function Vector2D:Dist(vec1, vec2)
dx = (vec2.x - vec1.x)
dy = (vec2.y - vec1.y)
return math.sqrt( dx*dx + dy*dy )
end

function Vector2D:dist(vec2)
dx = (vec2.x - self.x)
dy = (vec2.y - self.y)
return math.sqrt( dx*dx + dy*dy )
end

Nice work anyway :)

poisa commented

Here's how to limit the actual magnitude of a vector (the limit function in the code above only deals with individual components, not magnitude). I ported this from the openFrameworks ofVec3f class (https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/math/ofVec3f.h#L931)

function Vector2D:LimitMagnitude(vec, max)
    local tempVec = Vector2D:new(vec.x, vec.y)
    local lengthSquared = tempVec.x * tempVec.x + tempVec.y * tempVec.y
    if lengthSquared > max * max and lengthSquared > 0 then
        local ratio = max / math.sqrt(lengthSquared)
        tempVec.x = tempVec.x * ratio
        tempVec.y = tempVec.y * ratio
    end

    return tempVec
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.