Pythagoras theorem for colission detection between squares

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Pythagoras theorem for colission detection between squares

Post by Zer0XoL »

Well, this might be more of a math problem but still...
I recently found out that i could easily check the distance between two objects using pythagoras theorem :P, so i used it for colission detection, it works great for circles and that but, when for example i want to check the colission between walls i need to do it with squares, so i was wondering, does anyone have a thought for how i could use pythagoras theorem for colission detection between two squares?
my idea was doing it for each edge of the square and somehow compare the different distances, but i have no succes, it would be intresting thought if it worked because that would save a bit of energy and maby speed some things up. :P
forgive my spelling
Image
Im Blue
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Re: Pythagoras theorem for colission detection between squares

Post by Bullet Pulse »

I think you would be better off using a normal square collision algorithm, or if you need pixel perfect, then use that.
If you're using trig, then there is usually a simpler way to do it. ;)

Search around the forum for the topics on collision and you'll find more help.
Image
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Pythagoras theorem for colission detection between squares

Post by Bakkon »

Uhh, by Pythagorean Theorem collision, do you mean "iff distance < radius1 + radius 2 they are colliding"? Yeah, you'd be better off using AABB for squares/rectangles.
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: Pythagoras theorem for colission detection between squares

Post by Zer0XoL »

So you are saying i should use normal collision detection for squares? Yeah I probably should, but i want to see if its possible with this, and if so then i could probably use it with rotating squares too.
Im just interested in if something like this is possible, not for what i should use but tnx anyway :P
Image
Im Blue
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Pythagoras theorem for colission detection between squares

Post by Ginto8 »

Zer0XoL wrote:So you are saying i should use normal collision detection for squares? Yeah I probably should, but i want to see if its possible with this, and if so then i could probably use it with rotating squares too.
Im just interested in if something like this is possible, not for what i should use but tnx anyway :P
It depends. Circular collision detection is a LOT less expensive than SAT for when things are far away. Using circular for an initial collision detection is perfect, because if you have a circle circumscribed around the rects and those circles aren't touching, there's NO way the rects can be touching. So for OBBs using circular collision for an initial check is very good. For AABBs, circular detection is just a waste of CPU.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pythagoras theorem for colission detection between squares

Post by avansc »

If you want to to simple AABB rect collision you can make it about half as simple if you ad the width and heigh of one rect to another(with the appropriate half offsets), then it just becomes a point in rect test.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: Pythagoras theorem for colission detection between squares

Post by Zer0XoL »

Ginto8 wrote:
Zer0XoL wrote:So you are saying i should use normal collision detection for squares? Yeah I probably should, but i want to see if its possible with this, and if so then i could probably use it with rotating squares too.
Im just interested in if something like this is possible, not for what i should use but tnx anyway :P
It depends. Circular collision detection is a LOT less expensive than SAT for when things are far away. Using circular for an initial collision detection is perfect, because if you have a circle circumscribed around the rects and those circles aren't touching, there's NO way the rects can be touching. So for OBBs using circular collision for an initial check is very good. For AABBs, circular detection is just a waste of CPU.
yeah, tnx :)
Image
Im Blue
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: Pythagoras theorem for colission detection between squares

Post by Zer0XoL »

avansc wrote:If you want to to simple AABB rect collision you can make it about half as simple if you ad the width and heigh of one rect to another(with the appropriate half offsets), then it just becomes a point in rect test.
that seems nice :) i should probably check this AABB out, thx :D
Image
Im Blue
Post Reply