Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- btk.AABB = {type = "AABB"}
- function btk.AABB:new(x, y, w, h)
- o = {}
- setmetatable(o, self)
- o.x = x
- o.y = y
- o.w = w
- o.h = h
- o.sides = {}
- return o
- end
- function btk.AABB:__index(key)
- if key == "left" then
- return self.x
- elseif key == "top" then
- return self.y
- elseif key == "bottom" then
- return self.y + self.h
- elseif key == "right" then
- return self.x + self.w
- elseif key == "cx" then
- return self.x + self.w / 2
- elseif key == "cy" then
- return self.y + self.h / 2
- end
- return btk.AABB[key]
- end
- function btk.AABB:__newindex(k, v)
- if k == "left" then
- local diffX = v - self.x
- self.x = self.x + diffX
- self.w = self.w - diffX
- elseif k == "top" then
- local diffY = v - self.y
- self.y = self.y + diffY
- self.h = self.w - diffY
- elseif k == "right" then
- self.w = v - self.x
- elseif k == "bottom" then
- self.h = v - self.y
- elseif k == "cx" then
- self:setCenter(v, self.cy)
- elseif k == "cy" then
- self:setCenter(self.cx, v)
- else
- rawset(self, k, v)
- end
- end
- function btk.AABB:center()
- return btk.Vec2:new(
- self.x + self.w / 2,
- self.y + self.h / 2
- )
- end
- function btk.AABB:grow(...)
- local dx, dy = nil, nil
- local n = select("#", ...)
- local arg1, arg2 = select(1, ...), select(2, ...)
- if n == 1 then
- local argType = getType(arg1)
- if argType == "number" then
- dx = arg1
- dy = arg1
- elseif argType == "Vec2" then
- dx = arg1.x
- dy = arg1.y
- else
- assert(false, "Unexpected type " .. argType)
- end
- elseif n == 2 then
- dx = arg1
- dy = arg2
- end
- self.x = self.x - dx
- self.y = self.y - dy
- self.w = self.w + 2 * dx
- self.h = self.h + 2 * dy
- return self
- end
- function btk.AABB:draw()
- love.graphics.rectangle("line", self.x, self.y, self.w, self.h);
- end
- function btk.AABB:halfWidths()
- return btk.Vec2:new(self.w / 2, self.h / 2)
- end
- function btk.AABB:containsPoint(...)
- local x = nil
- local y = nil
- if select("#", ...) == 1 then
- local point = select(1, ...)
- assert(getType(point) == "Vec2", "Vec2 expected")
- x = point.x
- y = point.y
- elseif select("#", ...) == 2 then
- x = select(1, ...)
- y = select(2, ...)
- end
- if x > self.left and
- y > self.top and
- x < self.right and
- y < self.bottom then
- return {
- left = x - self.left,
- right = self.right - x,
- top = y - self.top,
- bottom = self.bottom - y,
- }
- end
- return nil
- end
- function btk.AABB:setCenter(...)
- local n = select("#", ...)
- local arg1 = select(1, ...)
- local arg2 = select(2, ...)
- if n == 1 then
- assert(getType(arg1) == "Vec2", "Unexpected type " .. getType(arg1))
- self.x = arg1.x - self.w / 2
- self.y = arg1.y - self.h / 2
- elseif n == 2 then
- self.x = arg1 - self.w / 2
- self.y = arg2 - self.h / 2
- end
- return self
- end
- function btk.AABB:__tostring()
- return "[" ..
- self.x .. " "
- .. self.y .. " "
- .. self.w .. " "
- .. self.h .. "]"
- end
- function btk.AABB:move(...)
- local n = select("#", ...)
- local arg1 = select(1, ...)
- local arg2 = select(2, ...)
- local x, y = 0, 0
- if n == 1 then
- vec = arg1
- assert(getType(vec) == "Vec2", "Unexpected type " .. getType(vec))
- x = vec.x
- y = vec.y
- elseif n == 2 then
- x = arg1
- y = arg2
- end
- self.x = self.x + x
- self.y = self.y + y
- return self
- end
- function btk.AABB.collide(a, b)
- local sides = {}
- if next(a.sides) == nil then -- If sides is empty
- sides = {
- top = true,
- right = true,
- bottom = true,
- left = true
- }
- else
- sides = a.sides
- end
- local collisionBox = clone(a):grow(b:halfWidths())
- local ds = collisionBox:containsPoint(b:center())
- local dVec = nil
- if ds then
- local other = btk.Vec2:new(0, -ds.top)
- if sides.top then
- dVec = other
- end
- other = btk.Vec2:new(ds.right, 0)
- if sides.right and (not dVec or other < dVec) then
- dVec = other
- end
- other = btk.Vec2:new(0, ds.bottom)
- if sides.bottom and (not dVec or other < dVec) then
- dVec = other
- end
- other = btk.Vec2:new(-ds.left, 0)
- if sides.left and (not dVec or other < dVec) then
- dVec = other
- end
- end
- return dVec
- end
- function btk.AABB.sweepCollide(a, b, v)
- assert(false, "Not yet implemented")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement