What Charles said about batching up the collisions and then acting on them did the trick. The only problem now is that I'm doing it wrong. :)
I'm just going through all the collisions for all the polygons and adding the vectors together and then offsetting my player by that new vector. If my player for example collide with 2 polygons which push him out in the same direction he gets pushed twice as far as he should which doesn't look good. I know what the problem is I'm just to dumb to fix it. :)
Here's what I have now:
PolyColli.zip (54kb)
Here's what my update method looks like:
| protected override void Update(GameTime gameTime) { |
| Vector2 movement = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left * 5.5f; |
| movement.Y *= -1; |
| |
| Vector2 velocity = movement; |
| Vector2 MTD = Vector2.Zero; |
| |
| foreach (Polygon polygon in polygons) { |
| if (polygon == player) |
| continue; |
| |
| Collision r = new Collision(player, polygon, velocity); |
| if (r.WillIntersect) { |
| MTD += r.MinimumTranslationVector; |
| } |
| } |
| |
| Vector2 playerTranslation = velocity + MTD; |
| player.Offset(playerTranslation); |
| } |
I've based it on the tutorial on pogopixels, but I've modified it to use XNA's Vector2 instead of the Vector class he implemented in the tutorial as well as use the PrimitiveBatch for drawing the polygons. The code is a bit messy and I've made changes all around, some of them may be stupid and unnecessary, but oh well. :)
The BoundingRectangles are definately unnecessary, but I just left them in there because I spent so much time setting them up. :P
My math skills have taken a considerable hit over the years so now I'm sitting here with a few math and physics books, but I'm still unable to fix the problem at the moment. Maybe someone can take a look and help me out? :)