How Can I Determine the Collided Side in C++ Collision Detection?

In summary, the conversation discusses a problem with determining which side is collided when checking for a collision between two boxes. One person suggests using SDL_Rect, a structure that holds x and y coordinates along with width and height, to determine the collided side. Another person suggests using a pixel by pixel search or a series of if statements to check for this. They also discuss the possibility of using vertices to determine the collided side and using an integer to represent the status of the collision.
  • #1
Peter P.
23
0
My problem is not so much with programming it, but rather determining which side is collided.
And for those who aren't familiar with SDL, SDL_Rect is just a structure that hold an x and y
position along with a width and height ( (x,y) is the top left corner of the rectangle).

So here is how I am planning on going about determining the collided side:
1. I check to see if there is a collision, then
2. I determine the side that collided.

Here is the function I am using for part 1.
Code:
bool collision_check (SDL_Rect box1, SDL_Rect box2, bool isOutside) {
	// A collision is determined from the perspective of box1.
	if (isOutside) {
		// box1 must be located somewhere outside box2
		// Checking if the bottom of box1 hits the top of box2
		if (box1.y + box1.h <= box2.y)
			return false;
		// Checking if the top of box1 hits the bottom of box2
		if (box1.y >= box2.y + box2.h)
			return false;
		// Checking if the right side of box1 hits the left side of box2
		if (box1.x + box1.w <= box2.x)
			return false;
		// Checking if the left side fo box1 hits the right side of box2
		if (box1.x >= box2.x + box2.w)
			return false;
	}
	else {
		// box1 must be located somewhere within box2
		// Checking if the top of box1 hits the top of box2
		if (box1.y >= box2.y)
			return false;
		// Checking if the bottom of box1 hits the bottom of box2
		if (box1.y + box1.h <= box2.y + box2.h)
			return false;
		// Checking if the left side of box1 hits the left side of box2
		if (box1.x >= box2.x)
			return false;
		// Checking if the right side of box1 hits the right side of box2
		if (box1.x + box1.w <= box2.x + box2.w)
			return false;
	}
	// If it failed the 4 requirements, it will return true (there was a
	// collision between the boxes.
	return true;
}
For part 2, I was reluctantly planning on doing a pixel by pixel search on all four sides of box1 to
determine which side collided with box2. But I am hoping that i can use a more simple series of if
statements (like part 1), to check for this.

I hope I made sense to anyone reading this. Thanks for your time, and any help is much
appreciated.

-Peter
 
Technology news on Phys.org
  • #2
Can we assume that the sides of the boxes are always vertical, and that the top and bottom are always horizontal? If so, that makes things easier.

Going with that assumption, each point on the top of a box has the same y-coordinate. It's only the x-coordinates that change. For example, suppose that the upper left corner is at (100, 200), and the width is 300 and height is 400.

The corners on the top edge are at (100, 200) and (400, 200). The corners at the bottom edge are (100, 400) and (400, 400). Notice the the top vertices have the same y-coordinate, as do the bottom vertices. Along the left edge, both vertices have the same x-coordinate (100), and both vertices along the right edge have the same x-coordinate (400). It should be a simple matter of checking whether the inside box has any vertices on any of the four edges of the outer box.
 
  • #3
Are you trying to figure out if a collision happened between two boxes?

If so you will get a maximum of four sides involved in a collision (if they overlap) and at a minimum one side.

The best way IMO to approach this is to return an integer where you use four bits for status: the four bits correspond to a collision of a particular side of a particular box.

You can if you want return the status of not just one box but both boxes resulting in returning collision information for both boxes involved if you want this: this will require a total of eight bits or simply one byte.

To check for a collision, test if the return value is non-zero and if a collision exists simply check the relevant bit for a particular collision.

To implement this you can use a for loop that sets the appropriate bits corresponding to the appropriate check of any pair of two sides (one from each rectangle) where you initialize this return value to 0 and simply set the bit upon an overlap.
 
  • #4
Mark44 said:
Can we assume that the sides of the boxes are always vertical, and that the top and bottom are always horizontal? If so, that makes things easier.

Going with that assumption, each point on the top of a box has the same y-coordinate. It's only the x-coordinates that change. For example, suppose that the upper left corner is at (100, 200), and the width is 300 and height is 400.

The corners on the top edge are at (100, 200) and (400, 200). The corners at the bottom edge are (100, 400) and (400, 400). Notice the the top vertices have the same y-coordinate, as do the bottom vertices. Along the left edge, both vertices have the same x-coordinate (100), and both vertices along the right edge have the same x-coordinate (400). It should be a simple matter of checking whether the inside box has any vertices on any of the four edges of the outer box.

Yes, all the sides are either vertical or horizontal. And I just thought about using the vertices
to check which side collided in the shower last night as well. :)

chiro said:
Are you trying to figure out if a collision happened between two boxes?

If so you will get a maximum of four sides involved in a collision (if they overlap) and at a minimum one side.

The best way IMO to approach this is to return an integer where you use four bits for status: the four bits correspond to a collision of a particular side of a particular box.

You can if you want return the status of not just one box but both boxes resulting in returning collision information for both boxes involved if you want this: this will require a total of eight bits or simply one byte.

To check for a collision, test if the return value is non-zero and if a collision exists simply check the relevant bit for a particular collision.

To implement this you can use a for loop that sets the appropriate bits corresponding to the appropriate check of any pair of two sides (one from each rectangle) where you initialize this return value to 0 and simply set the bit upon an overlap.

Thanks for the idea of using bits that correspond to different sides and changing them to
represent which side collided with another.
 
  • #5




Hello Peter, thank you for sharing your issue with C++ collision detection. It seems like you have a good understanding of the basic concepts and have already come up with a solution for determining the collision. However, I would suggest considering a different approach for determining the collided side.

Instead of doing a pixel by pixel search, you could use basic geometry and vector operations to determine the direction of the collision. For example, you can calculate the center points of both rectangles and use vector subtraction to get the direction of the collision. Then, based on the direction, you can determine which side was hit.

Another option would be to use the rectangle's edges as vectors and calculate the dot product between them. The dot product will give you a positive or negative value, which can be used to determine the direction of the collision.

I would also suggest looking into using a physics engine or a library specifically designed for collision detection. These tools can save you time and effort and provide more accurate results.

Overall, I would recommend exploring different methods and experimenting to find the most efficient and accurate solution for your specific project. I hope this helps and good luck with your collision detection!
 

What is C++ collision detection?

C++ collision detection is a technique used in computer graphics and game development to determine when two or more objects are intersecting or overlapping in a virtual environment.

Why is collision detection important in C++?

Collision detection is important in C++ because it allows for realistic and accurate interactions between objects in a virtual environment. This is crucial for creating immersive and engaging games and simulations.

What are the main challenges of implementing collision detection in C++?

One of the main challenges of implementing collision detection in C++ is the complexity of the algorithms and data structures required to accurately detect and handle collisions. Additionally, the performance and efficiency of the collision detection system must be carefully optimized to ensure smooth and responsive gameplay.

How can I improve the performance of collision detection in C++?

There are several ways to improve the performance of collision detection in C++. One strategy is to use simpler and more efficient collision detection algorithms, such as axis-aligned bounding boxes or sphere collision detection, instead of more complex ones like ray-casting or convex hulls. Additionally, reducing the number of objects that need to be checked for collisions can also improve performance.

Are there any libraries or frameworks available for collision detection in C++?

Yes, there are several libraries and frameworks available for collision detection in C++. Some popular options include Bullet Physics, Box2D, and Nvidia PhysX. These libraries provide pre-built collision detection algorithms and data structures, making it easier to implement collision detection in your C++ projects.

Similar threads

  • Programming and Computer Science
Replies
24
Views
1K
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
906
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
2
Views
713
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Introductory Physics Homework Help
Replies
10
Views
864
  • Programming and Computer Science
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
188
Back
Top