# Collision detection implementation suggestions

I'm working on a simple 2D game involving a rotating octagonal ring where a sphere sprite moves up/down avoiding objects as the ring rotates faster and faster. I'm having a difficult time with the collision between the sphere and the sides of the walls.

The ring is made up of 8 instances a primitive called a straightSection object. The camera is also zoomed in along the z-axis and positioned along the x and y to give the appears of a track. I've attempted to have 2 collision rectangles as part of the straightSection constructor. The rectangles would rotate with of the straightSection but what was happening was that the rectangles were aligning themselves with the axis and not rotating. I've then attempted to simply draw a vector across the top and bottom and use a BoundingSphere over the sprite and use the BoundingSphere.Intersect() method to determine but it was only working on vertices of the vector and not across the whole top.

I've tried attaching vectors and boxes to the vertices after they are transformed but also have used Viewport.Project() to obtain the coordinates for the box in the space as the sprite but it's the same thing

I've attached an illustration:

What I think is going wrong... well I'm not sure entirely. I've read so many tutorials and feel that I'm doing the same thing as them and it should be simple. My guess is that it has to do with the camera location, the ring location and the sphere location not being where I think they are.

If anyone needs to see the code, I can post that as well. I've been at this for weeks and I think I'm looking at the problem the same way over and over...

What I thought would be the simplest and most intuitive way to go about it was to have a ray traces starting from the top right of the primitive to the top left of the primitive. In the drawing above it would be the narrow red box (but in this case an attempt at a ray).

BoundingSphere bs = new BoundingSphere(new
Vector3(spriteManager.getPlayer.collisionRect.Center.X,
SpriteManager.getPlayer.collisionRect.Center.Y, 1) 25.0f)

Ray intersectRay = new Ray(section1.CoordTopR, section1.CoordTopL)
intersectRay.Direction.Normalize()

if (bs.Intersects(intersectRay) < 25.0f)
{
///

}


I've tried this with the View.Project coordinates of the vertices, or just using the transformed coordinates CoordTopR and CoordTopL and it doesn't work. It either always collides or only does over a small point. I've tried to change the intersect float value but that doesn't help. I'd like it to stretch over the whole top of the shape.

• I think it is a good idea to show some of the key areas of the code you think might be the source of the problem(s). – glampert Sep 12 '14 at 2:18
• Would point in polygon test help you? If the ball is not in the inner "oct-circle" and is in the outter "oct-circle", then it is within your ring. – wondra Sep 12 '14 at 10:22
• Any reasons why not using the methods that come with XNA would not work? Implementing these collision algorithms would be a good learning experience but I'm just wondering why what I'm doing doesn't work. @wondra Would you suggest creating a BoundingSphere that's the entire size of the ring? If the ball leaves that sphere then it's left the ring (thus hitting a wall). That's a good idea. I'll try that when I get home. – David Leacock Sep 12 '14 at 15:14
• @DavidLeacock not really, just express the "octagonal ring" as two oct polygons and hittest them by some well known point-in-polygon algorithms. – wondra Sep 12 '14 at 15:19