Collision Issues

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
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Collision Issues

Post by mv2112 »

I'm trying to make 2 boxes bounce off each other in a relatively realistic way. The 2 boxes have a x and y velocity and they bounce off the edges of the screen. When the blocks collide, either their x or y velocities are reversed depending upon which side box a hit box b at. I can check for collision between the boxes no problem, but i cant figure out how to check where box a hit box b. I'm stumped...

Also, does anyone know any good C++ physics tutorials?
Physics is what i suck most at at programming :mrgreen: so i want to learn more
Last edited by mv2112 on Wed Jun 16, 2010 4:22 pm, edited 2 times in total.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Collision Issues

Post by Falco Girgis »

mv2112 wrote:I'm trying to make 2 boxes bounce off each other in a relatively realistic way. The 2 boxes have a x and y velocity and they bounce off the edges of the screen. When the blocks collide, either their x or y velocities are reversed depending upon which side box a hit box b at. I can check for collision between the boxes no problem, but i cant figure out how to check where box a hit box b. I'm stumped...

Also, does anyone know any good C++ physics tutorials?
Physics is what i suck most at at programming :mrgreen: so i want to learn more
This question comes up rather frequently, actually. What you need is the "normal" vector resulting from your collision--this is the minimal separating axis that resolves the collision.

Check the "Programmer's Education Index" for AABB collision. There should be lots of details there.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Collision Issues

Post by mv2112 »

Thanks Falco! I did a search on the forums for the AABB collision and found a algorithm you posted that worked very well (with a little tweaking). Now i'll have to do some more research on how it works in detail.
Link to post -> http://elysianshadows.com/phpBB3/viewto ... art=999999
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Collision Issues

Post by XianForce »

mv2112 wrote:Thanks Falco! I did a search on the forums for the AABB collision and found a algorithm you posted that worked very well (with a little tweaking). Now i'll have to do some more research on how it works in detail.
Link to post -> http://elysianshadows.com/phpBB3/viewto ... art=999999
Just to let you know, he posted an even BETTER collision function hidden somewhere in the forum. Search a bit harder. :roll:
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Collision Issues

Post by xiphirx »

XianForce wrote:
mv2112 wrote:Thanks Falco! I did a search on the forums for the AABB collision and found a algorithm you posted that worked very well (with a little tweaking). Now i'll have to do some more research on how it works in detail.
Link to post -> http://elysianshadows.com/phpBB3/viewto ... art=999999
Just to let you know, he posted an even BETTER collision function hidden somewhere in the forum. Search a bit harder. :roll:
I believe you linked it in one of my topics.

I ignored it and went with my shitty ass ghetto collision code |3.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Collision Issues

Post by XianForce »

xiphirx wrote:
XianForce wrote:
mv2112 wrote:Thanks Falco! I did a search on the forums for the AABB collision and found a algorithm you posted that worked very well (with a little tweaking). Now i'll have to do some more research on how it works in detail.
Link to post -> http://elysianshadows.com/phpBB3/viewto ... art=999999
Just to let you know, he posted an even BETTER collision function hidden somewhere in the forum. Search a bit harder. :roll:
I believe you linked it in one of my topics.

I ignored it and went with my shitty ass ghetto collision code |3.
Yeah I think I've linked to it quite a few times.

I just remembered, it's in the Programmer's Education Index thing. It's a topic stickied in this forum, so look around for it.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Collision Issues <SOLVED>

Post by mv2112 »

This is off topic but I'd like to retract my statement on hating VS2010. I'm actually starting to like it a little. It just takes a lot of getting used to for me (Specifically defining include and lib locations for EVERY project and having to re-compile some libs to work with VS2010).
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Collision Issues <SOLVED>

Post by eatcomics »

mv2112 wrote:This is off topic but I'd like to retract my statement on hating VS2010. I'm actually starting to like it a little. It just takes a lot of getting used to for me (Specifically defining include and lib locations for EVERY project and having to re-compile some libs to work with VS2010).
I think there is a way that you can easily import them... But seeing as how I don't start many new projects I haven't really gotten too annoyed with it and haven't looked...
Image
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Collision Issues

Post by mv2112 »

I thought i would just revive this thread instead of making a new one. The collision function i have works for squares and rectangles that are almost squares.

Code: Select all

//Location and Size are vector2d<float>'s
bool Entity::Collision(Entity & b)
{
	float xDist = abs(Location.x - b.Location.x);
	float yDist = abs(Location.y - b.Location.y);
	float xMinDist = Size.x/2 + b.Size.x/2;
	float yMinDist = Size.y/2 + b.Size.y/2;

	if(xDist >= xMinDist || yDist >= yMinDist)
	{
		return false;
	}

	float xOverlap = xDist - xMinDist;
	float yOverlap = yDist - yMinDist;

	if(xOverlap < yOverlap)
	{
		return true;
	}
	else if(xOverlap > yOverlap) 
	{
		return true;
	}
	return false;
}
However if i have rectangles that arn't like squares, the collision doesn't line up correctly. What am i doing wrong? Hopefully next year Geometry class will help me with this stuff.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Collision Issues

Post by XianForce »

Is your position the center? And check to make sure your not assigning the same width and height to the dimensions even when the dimensions are NOT the same.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Collision Issues

Post by mv2112 »

XianForce wrote:Is your position the center? And check to make sure your not assigning the same width and height to the dimensions even when the dimensions are NOT the same.
That was the problem, the position wasn't the center. But if this was the case, why wasnt the collision of squares offset like collision of rectangles?
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Collision Issues

Post by ibly31 »

Hopefully next year Geometry class will help me with this stuff.
I took the final exam for Geometry today. I can pretty honestly say that to take what you learn in Geometry and apply it is pretty hard, I don't think I learned anything this year that could really help me with programming.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Collision Issues

Post by dandymcgee »

ibly31 wrote:
Hopefully next year Geometry class will help me with this stuff.
I took the final exam for Geometry today. I can pretty honestly say that to take what you learn in Geometry and apply it is pretty hard, I don't think I learned anything this year that could really help me with programming.
Trust me, later in life you will realize how fundamental algebra and geometry are. Calculus will prove IMPOSSIBLE without a solid understanding of algebra, geometry, and trigonometry.

I used to think the same things back in middle school and high school, but I'm constantly finding myself going back to the simple concepts to solve far more complex problems.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply