Hi,
I’m makeing a game and I am having trouble with a function I have made to help with Line of sight in my game. What I am trying to accomplish is to have my player sample points in a straight line to see if the points are walk able. What I have sofar is both points, the start and the end, but i can’t figure out how to find another point on the same line. I believe my math is off, can anyone point out what i am doing wrong?
function Actor:angleOfPoint( pt )
local x, y = pt.x, pt.y
local radian = math.atan2(y,x)
local angle = radian*180/math.pi
return angle
end
function Actor:hasLos(posx, posy, grid)
local myPoint = vec2(posx,posy)
if myPoint then
local angle = self:angleOfPoint(myPoint)
local xvel = 1
local yvel = 1
if angle >= 0 and angle < 90 then --q1
xvel = (angle/90)
yvel = 1 - (angle/90)
end
if angle >= 90 and angle < 180 then --q2
xvel = 1 - ((angle - 90)/90)
yvel = 0 - ((angle - 90)/90)
elseif angle >= 180 and angle < 270 then --q3
xvel = 0 - ((angle - 180)/90)
yvel = ((angle - 180)/90) - 1
elseif angle >= 270 and angle < 360 then --q4
xvel = ((angle - 270)/90) - 1
yvel = ((angle - 270)/90)
end
local increment = 8
local errorRange = 12
print("Angle:Start:End " .. angle ..":"
.. self.x .."," .. self.y .. ":"
.. posx .."," .. posy)
local vec = vec2(posx,posy) - vec2(self.x,self.y)
print ("P2 - P1: ")
print (vec)
local scaled = vec/math.sqrt((vec.x*vec.x) + (vec.y*vec.y))
print ("Vec Scaled: ")
print (scaled)
local point3 = vec2(posx,posy) + vec2(scaled.x*3,scaled.y*3) -- using 3 as the scale for
-- testing, this should make
-- the lines larger than the org.
print ("Point 3: ")
print (point3)
table.insert( self.angles,{x1=self.tweenObject.x,y1=self.tweenObject.y,x2=point3.x,y2=point3.y})
table.insert( self.angles,{x1=self.tweenObject.x,y1=self.tweenObject.y,x2=posx,y2=posy})
return false -- who cares if this works until i can plot an additional point properly
else -- if myPoint is false
print("Nope")
return false -- return false
end -- end of myPoint check
end