Public Group

# [DIRECTX C#] Simple vector subtraction to find collisions

This topic is 3822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In some source code I have looked at before written with XNA, I have seen this done to determine whether a collision has taken place: Vector2 distVec = new Vector2(Player.shipPos.X, Player.shipPos.Y) - new Vector2(position.X, position.Y); if (distVec.Length() < 5.0f) { // A collision has been detected } Although it was written in XNA and I am using DirectX, the overloaded subtraction operator of the Vector2 class should still be the same, right? Anyways, I don't really know what the 5 means. I assume it gives the entity a 5 pixel bounding box so you don't have to exactly hit it and == 0 would be an exact collision and > would mean you would have to go inside the entity to detect a collsion. Either way, it isn't working. This is the code I have now in my game: if (ball.Location.X <= paddle.Location.X + paddle.Width) { if (ball.Location.Y + ball.Height >= paddle.Location.Y && ball.Location.Y <= paddle.Location.Y + paddle.Height) { return true; } } And I am trying to replace that with this to get a more accurate and simpler collision detection: Vector2 distance = new Vector2(ball.Location.X, ball.Location.Y) - new Vector2(paddle.Location.X, paddle.Location.Y); if (distance.Length() < 5.0f) { // A collision has been detected. } However, at runtime a collision is not being detected. The old, sloppy code works fine but this new one I am trying out isn't! I would much rather use this way and I would sure love to hear some insight as to why this isn't working and hopefully someone could help me with Vectors and their subtraction operator a bit more. Thanks!

##### Share on other sites
It looks like the '5' means that the distance from the center of object A to the the center of object B is 5 'units' in the game. This would imply that every object has a circular bounding box of 2.5 units.

If you are doing simple spherical bounding box detection you can compute it like this:

posA = position of object A
posB = position of object B
r = radius of collision sphere.

if( length(posA-posB) < 2*r ) collision occurs

##### Share on other sites
Well they aren't all balls. This is pong. I need to check for collisions between paddles and balls, balls and balls, etc. However, this is what I came up with:

if (distance.Length() < 2.0f * ball.Width)
{

}

Does that even look right? It doesn't really work in game. It hits the paddle and then just does some crazy jubbly bouncing that doesn't even make sense.

Should I just use the old way? =/

##### Share on other sites
Quote:
 Original post by FromethiusWell they aren't all balls. This is pong. I need to check for collisions between paddles and balls, balls and balls, etc. However, this is what I came up with: Vector2 distance = new Vector2(ball.Location.X, ball.Location.Y) - new Vector2(paddle.Location.X, paddle.Location.Y); if (distance.Length() < 2.0f * ball.Width) { }Does that even look right? It doesn't really work in game. It hits the paddle and then just does some crazy jubbly bouncing that doesn't even make sense.Should I just use the old way? =/

You are using a test for collision between circles. A paddle isn't circular.

##### Share on other sites
Quote:
 Original post by FippyDarkpawIt looks like the '5' means that the distance from the center of object A to the the center of object B is 5 'units' in the game.

...when they touch. Yes. But
Quote:
 Original post by FippyDarkpawThis would imply that every object has a circular bounding box of 2.5 units.

isn't generally true. It implies that the sum of the radii of both bounding circles is 5 units.

Furthur may
if (distance.Length() < 2.0f * ball.Width){}
be incorrect, dependent on whether "ball.Width" actually means a diameter (then it would be wrong, i.e. 2 times too big) or else a radius (then it would be correct). It would work for collision checks of 2 balls only, of course.

• 23
• 10
• 19
• 15
• 14