So im making a simple RPG using c# and xna. For collision, i am using a text file that contains data about whether a tile is passable or not. I iterate through them and create rectangles based on the position of the 'bad' tile in the text file. I then used these rectangles for collision detectiong.
Now, I can easily check whether or not the player is collision with these 'bad' tiles by using the 'Intersection()' method of the Rectangle type. What I am struggling with however, is what do I do once the collision is detected.
So far, I am creating a tmp Rectangle 'checkBounds' this has the players rectangle info in, however I add the players doirection velocity to it, check if the checkBounds rectangle intersects the 'bad' tiles rectangle. If it does, I do nothing, if it dosent, I set the players rectangle coor's to the checkBounds coor. Here is the code:
public const int PLAYER_WIDTH = 30; // Sets the player tile width.
public const int PLAYER_HEIGHT = 60; // Sets the player tile height.
//
public Rectangle playerBounds = new Rectangle(360, 250, PLAYER_WIDTH, PLAYER_HEIGHT); // Sets the players initial bounds
//
public Rectangle futurePos;
// Which way is the player facing?
public enum Direction
{
Up,
Down,
Left,
Right,
None
};
Direction dirUp = Direction.Up;
Direction dirDown = Direction.Down;
Direction dirLeft = Direction.Left;
Direction dirRight = Direction.Right;
public Player()
{
futurePos = new Rectangle(playerBounds.X, playerBounds.Y, PLAYER_WIDTH, PLAYER_HEIGHT);
}
public void Update(Player player, Core core, Camera camera)
{
// Gets an array list of possible key inputs.
Keys[] keys = Keyboard.GetState().GetPressedKeys();
if(keys != null && keys.Length > 0){
if (keys[0] == Keys.Up)
{
// Up arrow pressed, move player up.
CalcCollision(player, core, dirUp);
}
else if (keys[0] == Keys.Down)
{
// Down arrow pressed, move player down.
CalcCollision(player, core, dirDown);
}
else if (keys[0] == Keys.Left)
{
// Left arrow pressed, move player left.
CalcCollision(player, core, dirLeft);
}
else if (keys[0] == Keys.Right)
{
// Right arrow pressed, move player right.
CalcCollision(player, core, dirRight);
}
}
}
private void CalcCollision(Player player, Core core, Direction dir)
{
//Rectangle futurePos = new Rectangle(player.playerBounds.X, player.playerBounds.Y, PLAYER_WIDTH, PLAYER_HEIGHT);
if (dir == dirUp)
{
futurePos.Y -= (int)player.playerVel.Z;
CollisionResolve(player, core, dir);
}
if (dir == dirDown)
{
futurePos.Y += (int)player.playerVel.W;
CollisionResolve(player, core, dir);
}
if (dir == dirLeft)
{
futurePos.X -= (int)player.playerVel.X;
CollisionResolve(player, core, dir);
}
if (dir == dirRight)
{
futurePos.X += (int)player.playerVel.Y;
CollisionResolve(player, core, dir);
}
}
private void CollisionResolve(Player player, Core core, Direction dir)
{
int width = core.collisionMap.GetLength(1);
int height = core.collisionMap.GetLength(0);
for(int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (futurePos.Intersects(core.collisionMap[y, x]))
{
// Players future position will intersect a bad tile, dont change players position vector.
}
else
{
// Players future position will not intersect a bad tile, chenge players position vector.
if (dir == dirUp || dir == dirDown)
player.playerBounds.Y = futurePos.Y;
else if (dir == dirLeft || dir == dirRight)
player.playerBounds.X = futurePos.X;
}
}
}
}
My point is, it dosent really work. That is, if I hold an arrow key long enough when colliding with a bad tile, the player just teleports to the other side of the bad tile. Why is this happening and how can I resolve it?
Any and all responses are welcome!