Substitute541's Useful-Code Dump [Closed]

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Substitute541's Useful-Code Dump [Closed]

Post by substitute541 »

[This thread is currently under construction. More code will be added soon]

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
And there's also a demo for that. It compares distance-based collision detection on bounding-box collision detection. There will be a black stationary ball of varying sizes(radius will be between 20 and 60) and position, a blue stationary ball of varying sizes(radius still between 20 and 60) but fixed position, and a ball that moves with the mouse of a constant radius(radius is 30.) What will happen is, you move the ball that's following the mouse(i'll call it mouseball) to one of the other balls. If you move it to the blue ball, you will notice that it will register collisions in the corners of the bounding-box, even though it is not really touching the ball. If you move it to the black ball, you will notice that it will register collisions only if it is really touching the ball. And you can press the "s" key on your keyboard to see the bounding box on the blue ball.
distancebasedcollision.love
Compares Distance-Based Collision Detection with Bounding-Box Based Collision Detection
(1.22 KiB) Downloaded 96 times
More to come soon!
Last edited by substitute541 on Tue Oct 02, 2012 2:21 am, edited 1 time in total.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: Substitute541's Useful-Code Dump [NEW;1 demo]

Post by qaisjp »

Lua is not an acronym.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Substitute541's Useful-Code Dump [NEW;1 demo]

Post by GijsB »

I like the idea :D! But yeah, there's already a wiki page for this kind of stuff ._.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Substitute541's Useful-Code Dump [NEW;1 demo]

Post by substitute541 »

GijsB wrote:I like the idea :D! But yeah, there's already a wiki page for this kind of stuff ._.
Just so you know. There are no snippets for these kinds of coding yet. Oh and, working on another one. Uploading it in a few minutes.

Edit : Err nevermind. Maybe tomorrow.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests