I am new to game programming and to these sites for help. I am making a 2D game but I can't seem to get the collision between my character and certain tiles. I have a map filled with grass tiles and water tiles and I want to keep my character from walking on the water tiles. I have a Tiles class that I use so that the tiles are objects and also has the collision method in it, a TileEngine class used create the map and it also holds a list of Tiles, and the class James which is for my character. I also have a Camera class that centers the camera on my character if that has anything to do with the problem. The character's movement is intended to be restricted to 4 directions(up, down, left, right). As an extra note, the bottom right water tile does have collision, but the collision does not occur for any of the other water tiles.
Here is my TileEngine class
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Test2DGame2
{
class TileEngine : Microsoft.Xna.Framework.Game
{
//makes a list of Tiles objects
public List<Tiles> tilesList = new List<Tiles>();
public TileEngine()
{}
public static int tileWidth = 64;
public static int tileHeight = 64;
public int[,] map = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,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List[map[y, x]].rectangle = new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
//draw the Tiles objects
spriteBatch.Draw(tilesList[map[y, x]].texture,
tilesList[map[y, x]].rectangle,
Color.White);
}
}
}
}
}
Here is my Tiles class
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Test2DGame2
{
class Tiles
{
public Texture2D texture;
public Rectangle rectangle;
public Tiles(Texture2D texture)
{
this.texture = texture;
}
//check to see if james collides with the tile from the right side
public void rightCollision(James james)
{
if (james.GetBounds().Intersects(rectangle))
{
james.position.X = rectangle.Left - james.front.Width;
}
}
}
}
I have a method for rightCollision because I could only figure out how to get the collisions from specifying directions.
and here is the James class for my character
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Test2DGame2
{
class James
{
public Texture2D front;
public Texture2D back;
public Texture2D left;
public Texture2D right;
public Vector2 center;
public Vector2 position;
public James(Texture2D front)
{
position = new Vector2(0, 0);
this.front = front;
center = new Vector2(front.Width / 2, front.Height / 2);
}
public James(Texture2D front, Vector2 newPosition)
{
this.front = front;
position = newPosition;
center = new Vector2(front.Width / 2, front.Height / 2);
}
public void move(GameTime gameTime)
{
KeyboardState keyboard = Keyboard.GetState();
float SCALE = 20.0f;
float speed = gameTime.ElapsedGameTime.Milliseconds / 100.0f;
if (keyboard.IsKeyDown(Keys.Up))
{
position.Y -=speed * SCALE;
}
else if (keyboard.IsKeyDown(Keys.Down))
{
position.Y += speed * SCALE;
}
else if (keyboard.IsKeyDown(Keys.Left))
{
position.X -= speed * SCALE;
}
else if (keyboard.IsKeyDown(Keys.Right))
{
position.X += speed * SCALE;
}
}
public void draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(front, position, null, Color.White, 0, center, 1.0f, SpriteEffects.None, 0.0f);
}
//get the boundingbox for James
public Rectangle GetBounds()
{
return new Rectangle(
(int)position.X,
(int)position.Y,
front.Width, front.Height);
}
}
}