fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
eefe2ce
GUI
1local VECTOR ={} 2VECTOR . __index =VECTOR 3 4function isvector ( vec ) 5return vec and type ( vec ) == "table" and vec . x and vec . y and vec . z 6end 7 8function vector ( x , y , z ) 9return setmetatable ( { x =x or 0 , y =y or 0 , z =z or 0 } , VECTOR ) 10end 11 12function VECTOR : __tostring () 13return ( "[vector][%d, %d, %d]" ): format ( self . x , self . y , self . z ) 14end 15 16function VECTOR : __add ( vec ) 17return vector ( self . x + vec . x , self . y + vec . y , self . z + vec . z ) 18end 19 20function VECTOR : __div ( num ) 21return vector ( self . x / num , self . y / num , self . z / num ) 22end 23 24function VECTOR : __eq ( vec ) 25return self . x == vec . x and self . y == vec . y and self . z == vec . z 26end 27 28function VECTOR : __mul ( vec ) --Multiply a vector by a number, or multiply a vector by a vector 29if isvector ( self ) and isvector ( vec ) then 30return vector ( self . x * vec . x , self . y * vec . y , self . z * vec . z ) 31elseif type ( self ) == "number" then 32return vector ( self * vec . x , self * vec . y , self * vec . z ) 33else 34return vector ( self . x * vec , self . y * vec , self . z * vec ) 35end 36end 37 38function VECTOR : __sub ( vec ) 39return vector ( self . x - vec . x , self . y - vec . y , self . z - vec . z ) 40end 41 42function VECTOR : Cross ( vec ) 43return vector ( self . y * vec . z - self . z * vec . y , self . z * vec . x - self . x * vec . z , self . x * vec . y - self . y * vec . x ) 44end 45 46function VECTOR : Distance ( vec ) --The distance between two vectors 47return ( self - vec ): Length () 48end 49 50function VECTOR : DistToSqr ( vec ) 51return ( self . x - vec . x ) ^ 2 + ( self . y - vec . y ) ^ 2 + ( self . z - vec . z ) ^ 2 52end 53 54function VECTOR : Dot ( vec ) --The dot product of two vectors 55return vec . x * self . x + vec . y * self . y + vec . z * self . z 56end 57 58function VECTOR : DotProduct ( vec ) --Duplicate of Dot 59return vec . x * self . x + vec . y * self . y + vec . z * self . z 60end 61 62function VECTOR : GetNormalized () --Gets the normal of two vectors 63local length =self : Length () 64return vector ( self . x / length , self . y / length , self . z / length ) 65end 66 67function VECTOR : GetNormal () --Duplicate of GetNormalized 68local length =self : Length () 69return vector ( self . x / length , self . y / length , self . z / length ) 70end 71 72function VECTOR : WithinAABox ( mins , maxs ) 73if self . x < mins . x or self . y < mins . y or self . z < mins . z or self . x > maxs . x or self . y > maxs . y or self . z > maxs . z then 74return false 75end 76return true 77end 78 79--[[function VECTOR:IsEqualTol(vec, tol) 8081 end]] 82 83function VECTOR : IsZero ( vec ) 84return vec . x == 0 and vec . y == 0 and vec . z == 0 85end 86 87function VECTOR : Length2D () 88return math . sqrt ( self . x * self . x + self . y * self . y ) 89end 90 91function VECTOR : Length () --The length of a vector 92return math . sqrt ( self . x * self . x + self . y * self . y + self . z * self . z ) 93end 94 95function VECTOR : Normalize () --Directly modifies the vector you pass it 96local length =self : Length () 97self . x =self . x / length 98self . y =self . y / length 99self . z =self . z / length 100end 101 102--[[function VECTOR:Rotate(ang) --Rotate a vector by degrees 103ang = math.rad(ang) 104return vector(self.x * math.cos(ang) - self.y * math.sin(ang), self.x * math.sin(ang) + self.y * math.cos(ang)) 105end]] 106 107function VECTOR : Add ( vec ) 108self . x =self . x + vec . x 109self . y =self . y + vec . y 110self . z =self . z + vec . z 111end 112 113function VECTOR : Set ( vec ) 114self . x =vec . x 115self . y =vec . y 116self . z =vec . z 117end 118 119function VECTOR : Mul ( scale ) 120self . x =self . x * scale 121self . y =self . y * scale 122self . z =self . z * scale 123end 124 125function VECTOR : Zero () --Not sure why this would ever be needed, but hey who cares.. (taken from the garrysmod library) 126self . x =0 127self . y =0 128self . z =0 129end 130 131function VECTOR : RayQuadIntersect ( vDirection , vPlane , vX , vY ) 132local vp =vDirection : Cross ( vY ) 133 134local d =vX : DotProduct ( vp ) 135 136if ( d <= 0.0 ) then return end 137 138local vt =self - vPlane 139local u =vt : DotProduct ( vp ) 140if ( u < 0.0 or u > d ) then return end 141 142local v =vDirection : DotProduct ( vt : Cross ( vX )) 143if ( v < 0.0 or v > d ) then return end 144 145return vector ( u / d , v / d , 0 ) 146end 147 148function VECTOR : IsInFront ( vec , normal ) 149return ( normal : Dot (( vec - self ): GetNormalized ()) < 0 ) 150end 151 152function VECTOR : Clamp ( min , max ) 153self . x =math . Clamp ( self . x , min , max ) 154self . y =math . Clamp ( self . y , min , max ) 155self . z =math . Clamp ( self . z , min , max ) 156return self 157end 158 159function VECTOR : Project ( vec ) 160return self : Dot ( vec ) / vec : Dot ( vec ) * vec 161end