Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- system.physics = {
- -- [[ Check for collisions ]] --
- -- Checks if an object at position (x, y) collides with another object at position (x2, y2) in a range (width, height)
- -- returns true if it collides, false otherwise.
- collidepoint = function(x, y, x2, y2, w, h)
- return (x>=x2-w and x<=x2+w and y>=y2-h and y<=y2+h)
- end,
- -- Check if two circular objects collided
- -- returns true if it collides, false otherwise.
- collidecircle = function(x, y, x2, y2, radius1, radius2)
- local dx = x - x2
- local dy = y - y2
- local distance = Math.sqrt(dx * dx + dy * dy)
- return distance < radius1 + radius2
- end,
- -- Check if two retangular objects collided
- -- returns true if it collides, false otherwise.
- colliderect = function(x, y, x2, y2, w, h, w2, h2)
- return (x < x2 + w2 and x + w > x2 and y < y2 + h2 and y + h > y2)
- end,
- -- Casts a radius from a position (x, y) to a position (x2, y2) in a straight line with a limit (range)
- -- returning information such as angle, mapping (to check for direct collisions), distance and offsets
- -- returns table {distance, offsetX, offsetY, angle, map}
- raycast = function(x, y, x2, y2, range)
- local angle = math.atan2(y2 - y, x2 - x)
- local distance = ((x - x2)^2 + (y - y2)) ^ .5
- if (range and distance > range) then
- distance = range
- end
- local offsetX = math.cos(angle)
- local offsetY = math.sin(angle)
- local map = {}
- for i=0, distance do
- map[#map+1] = {
- x = x+(i*offsetX),
- y = y+(i*offsetY)
- }
- end
- return {
- distance = distance,
- offsetX = offsetX,
- offsetY = offsetY,
- angle = angle,
- map = map
- }
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement