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...
Adding some code
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.