rectangular body 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
User avatar
hertzcastle
Party member
Posts: 100
Joined: Sun Jan 04, 2009 4:51 pm
Location: brighton, uk

rectangular body help

Post by hertzcastle »

hi!
is there any way to check which side of a rectangular body is being collided with? im not actually using love.physics in my project, im using an old-skool rectIntersect() function, but im having no joy with the maths(ive got it sort of working, but it needs to be pretty much perfect). is there a way of doing this with physics?
thanks.x
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: rectangular body help

Post by bartbes »

Well, I'd say that if the colliding object is above the base object it's colliding with the top, if it's below it's colliding with the bottom, etc.
Geti
Party member
Posts: 112
Joined: Tue Oct 20, 2009 6:38 am

Re: rectangular body help

Post by Geti »

That doesn't work if it'd been rotated though.
If you need need need this you could always construct the rectangle of lines and use each of their callback data's to feed into an overall collide function.
User avatar
hertzcastle
Party member
Posts: 100
Joined: Sun Jan 04, 2009 4:51 pm
Location: brighton, uk

Re: rectangular body help

Post by hertzcastle »

ok, i dont think im gonna use the physics module for this. this is the code im using:

Code: Select all

local xOff = self.x + anotherRect.x + (anotherRect.width / 2)
local yOff = self.y + anotherRect.y + (anotherRect.height / 2)
if(xOff > yOff) then
	if(self.x <= (anotherRect.x + (anotherRect.width / 2))) then return Left end
	if(self.x >= (anotherRect.x + (anotherRect.width / 2))) then return Right end
else
	if(self.y <= (anotherRect.y + (anotherRect.height / 2))) then return Up end
	if(self.y >= (anotherRect.y + (anotherRect.height / 2))) then return Down end
end
(this is from the OOP class i made for a rect-based collision system, self refers to one rect, anotherRect to the second)
works most of the time, but it needs to work all of the time.
any holes in this that anyone can spot?
x
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: rectangular body help

Post by Robin »

hertzcastle wrote:ok, i dont think im gonna use the physics module for this. this is the code im using:
I think you need to do this: (I also removed superfluous parentheses)

Code: Select all

local xOff = math.abs(anotherRect.x + anotherRect.width / 2 - self.x) -- subtracting here, because it's about the difference!
local yOff = math.abs(anotherRect.y + anotherRect.height / 2 - self.y) -- same here. also note the math.abs() call, this is to ensure xOff and yOff have the same sign
if(xOff > yOff) then
	if self.x <= anotherRect.x + anotherRect.width / 2 then return Left end
	return Right
else
	if self.y <= anotherRect.y + anotherRect.height / 2 then return Up end
	return Down
end
Also, you might want to replace self.x by (self.x + self.width / 2), and the same with y, because x,y is top-left, right?
Help us help you: attach a .love.
User avatar
hertzcastle
Party member
Posts: 100
Joined: Sun Jan 04, 2009 4:51 pm
Location: brighton, uk

Re: rectangular body help

Post by hertzcastle »

wow, yes!
much, much smoother! thanks rude!
now i can get on with the next million gameplay elements!
x

---DOUBLE POST---
and yeah, too much parenthesis. i come from a world of java, thats all i can say :?

xx
Last edited by bartbes on Sun Jun 13, 2010 8:59 am, edited 1 time in total.
Reason: Double post....
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: rectangular body help

Post by Robin »

hertzcastle wrote:thanks rude!
It's Robin. But you're welcome.
hertzcastle wrote:and yeah, too much parenthesis. i come from a world of java, thats all i can say :?
Poor thing.
Help us help you: attach a .love.
User avatar
hertzcastle
Party member
Posts: 100
Joined: Sun Jan 04, 2009 4:51 pm
Location: brighton, uk

Re: rectangular body help

Post by hertzcastle »

sorry dude, meant to type Robin :?
big thanks!
User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Re: rectangular body help

Post by Luiji »

So why not use the physics module and create a rectangular collision system? Is there a factor of the love.physics module that makes it slower then such a system? Or did you simply want to write a rectangular collision system for fun?
Good bye.
User avatar
hertzcastle
Party member
Posts: 100
Joined: Sun Jan 04, 2009 4:51 pm
Location: brighton, uk

Re: rectangular body help

Post by hertzcastle »

@luiji:
basically, i just wanted to set up something really simple and reusable. i find the physics stuff really good, but not really suited to what im doing at the moment. i guess im old fashioned and just want a list(table) of rectangles to iterate over. But some day I'm going to figure out the love.physics stuff, some day...
but yea, i dont really need "physics" as such, just collisions, usually find it a bit quicker to write my own stuff, this has obv been a bit of an exception ...
x
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests