Friday, September 30, 2016

xna - 2D platformer corner collision


Repost from my question on stack overflow, didnt know this site existed.


I have a 2d platformer and I need help with collision. To show you my current collision system, here is picture: http://i.imgur.com/romKF.png


Top picture is how game looks normally, bottom picture shows highlighted collision rectangles. Black ones are horizontal (If player rectangle intersects them, move player on Y axis), blue are vertical (move player on the X axis) and red are corners.


I've implemented corner rectangles, after bit of testing after I found out, that when I had black and blue overlapping each other, this happened: Player jumped and intersected both rectangles at once, game would recognize that and resolve both intersection at the same time, cause the player to spaz around the map and ending up in a another rectangle, which would then cause another intesection and resolution until the chain reaction stopped, and player would end up somewhere where he should not have been.


So I added corner rectangles, they should resolve this situation like this: if player hits corner from the bottom, act like horizotal rectangle, if he hits from side, act like vertical.


Thing is, I have no idea how to make them work like that.


Here is my code so far:


        if (InputHandler.KeyDown(Keys.W))
{

player.MoveUp();
Parallel.For(0, horizontalPlayerCollisionRectangles.Count, i =>
{
if (player.PlayerRectangleInWorld.Intersects(horizontalPlayerCollisionRectangles[i]))
{
player.PlayerPositionY = horizontalPlayerCollisionRectangles[i].Y + horizontalPlayerCollisionRectangles[i].Height;
}
});
}


if (InputHandler.KeyDown(Keys.D))
{
player.MoveRight();
Parallel.For(0, verticalPlayerCollisionRectangles.Count, i =>
{
if (player.PlayerRectangleInWorld.Intersects(verticalPlayerCollisionRectangles[i]))
{
player.PlayerPositionX = verticalPlayerCollisionRectangles[i].X - player.PlayerRectangleInWorld.Width;
}
});

}

if (InputHandler.KeyDown(Keys.A))
{
player.MoveLeft();
Parallel.For(0, verticalPlayerCollisionRectangles.Count, i =>
{
if (player.PlayerRectangleInWorld.Intersects(verticalPlayerCollisionRectangles[i]))
{
player.PlayerPositionX = verticalPlayerCollisionRectangles[i].X + verticalPlayerCollisionRectangles[i].Width;

}
});
}

if (player.Gravity)
{
Parallel.For(0, horizontalPlayerCollisionRectangles.Count, i =>
{
if (player.PlayerRectangleInWorld.Intersects(horizontalPlayerCollisionRectangles[i]))
{

player.PlayerPositionY = horizontalPlayerCollisionRectangles[i].Y - player.PlayerRectangleInWorld.Height;
}
});
}

Its pretty simple. Now that I think about it, my system is flawed, so thats why I'm asking this question. How to make collision work using collision rectangles?


P.S. I can't use any physics engine like Farseer, it's a school graduation project and I have to code as much as possible by myself.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...