Welcome to Substitute541's Useful-Code Dump!
This is just a list of my useful code that are useful(duh.)
You are free to use the code in here. Although, I prefer people to not copy-and-paste the code. You should be used to typing code yourself.
The first on the list is : Distance-Based Collision Detection
This kind of collision works for circles/circular objects. It detects whether the distance between two circles(for example) is less than the sum of their radii(plural of radius.) The code itself is very simple. I added two if statements and an argument if they will use the mouse to detect collisions or not. However, the general code(the code inside the if statements; specifically the first one) is simple, it uses the Pythagorean Theorem(maybe a blast to non-math fans.)
Code: Select all
-- Please note. When isUsingCursorCoordinates == true, use objectA, set objectB to null
-- Btw, this has nothing to do with what /will/ happen when colliding. It only checks
-- /if/ they are colliding.
function checkDistanceCollision(objectA, objectB, radiusA, radiusB, isUsingCursorCoordinates)
if isUsingCursorCoordinates == false then
local dx = objectB.x - objectA.x
local dy = objectB.y - objectA.y
local dist = math.sqrt(dx * dx + dy * dy)
local minDist = radiusA + radiusB
if dist < minDist then
return true
else
return false
end
elseif isUsingCursorCoordinates then
local mdx = love.mouse.getX() - objectA.x
local mdy = love.mouse.getY() - objectA.y
local mouseDist = math.sqrt(mdx * mdx + mdy * mdy)
local mouseMinDist = radiusA
if mouseDist < mouseMinDist then
return true
else
return false
end
end
end
More to come soon!