Rectangle Collision Function Help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
darkroom
Prole
Posts: 3
Joined: Sat Apr 27, 2013 1:43 pm

Rectangle Collision Function Help

Post by darkroom »

Hello so I am making a game where you eat smaller fish to grow larger to eat bigger fish and so I need a way for the player to collide with fish. I tried to make myself a collision function but it doesn't work. Can someone show me a rectangle collision function?
My collision function:

Code: Select all

function player:collidiesWith(other)
	if not other.rect then
		other.rect = makeRect(other)
	end
	local lMost, rMost, bMost, tMost
	if self.rect.left < other.rect.left then
		lMost = self.rect
		rMost = other.rect
	else 
		lMost = other.rect
		rMost = self.rect
	end
	if self.rect.top < other.rect.top then
		tMost = self.rect
		bMost = other.rect
	else
		tMost = other.rect
		bMost = self.rect
	end
	local xdist = rMost.left - lMost.right
	local ydist = bMost.top - tMost.bottom
	return xdist <= 0 and ydist <= 0
end
My full program code is below
Attachments
Archive.love
(114.01 KiB) Downloaded 94 times
User avatar
vitaminx
Citizen
Posts: 95
Joined: Fri Oct 19, 2012 7:16 am
Location: international
Contact:

Re: Rectangle Collision Function Help

Post by vitaminx »

Hi,

I've written a rectangular collision detection for your game and added the width/height of player and fishes to their tables to make calculation easier.

Here's the collision part:

Code: Select all

function player:collidiesWith(other)
    if other.x+other.width>self.x and other.x<self.x+self.width and other.y+other.height>self.y and other.y<self.y+self.height then return true
    else return false end
end
Other changes are in entities:spawn(), entities:load(), entities:checkCollision() and player:load().
Use diff to find them ^^

Another hint: you use a similar table.insert in both entities:spawn() and entities:load(). You should avoid any duplication of code, maybe make a function for the insert?

Greets,
vitaminx
Attachments
archive.love
(113.29 KiB) Downloaded 95 times
User avatar
spynaz
Party member
Posts: 152
Joined: Thu Feb 28, 2013 5:49 am

Re: Rectangle Collision Function Help

Post by spynaz »

Hey I just finished a game like that......well similar. XD
You can check out the code. It has circle and rectangle collision detection functions.

http://love2d.org/forums/viewtopic.php?f=5&t=33349
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Rectangle Collision Function Help

Post by Puzzlem00n »

I remember a time, long ago when I came on here often, when people would always link this for questions like these, and thus I shall do the same. :D

P.S @Vitaminx: Helping people doesn't always mean doing all their work for them. It's better to stop, guide them through it, and explain.
I LÖVE, therefore I am.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 7 guests