Page 1 of 1

Collision Issues

Posted: Wed Jun 02, 2010 5:01 pm
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

Re: Collision Issues

Posted: Wed Jun 02, 2010 5:10 pm
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.

Re: Collision Issues

Posted: Wed Jun 02, 2010 7:54 pm
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

Re: Collision Issues

Posted: Wed Jun 02, 2010 8:19 pm
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:

Re: Collision Issues

Posted: Wed Jun 02, 2010 10:38 pm
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.

Re: Collision Issues

Posted: Wed Jun 02, 2010 11:13 pm
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.

Re: Collision Issues <SOLVED>

Posted: Thu Jun 03, 2010 1:02 pm
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).

Re: Collision Issues <SOLVED>

Posted: Thu Jun 03, 2010 3:02 pm
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...

Re: Collision Issues

Posted: Wed Jun 16, 2010 4:28 pm
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.

Re: Collision Issues

Posted: Wed Jun 16, 2010 4:37 pm
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.

Re: Collision Issues

Posted: Wed Jun 16, 2010 5:06 pm
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?

Re: Collision Issues

Posted: Wed Jun 16, 2010 9:12 pm
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.

Re: Collision Issues

Posted: Wed Jun 16, 2010 11:25 pm
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.