But when i wrote pretty much the same code for collision in my cell game, and since the movement is done differently, collision is done very wonky, where the payer would stop about 5 pixels away from the square object. I cant figure out how to stop the player from moving when colliding with this object with no problem.
My code;
namespace Fission_Alpha_Build
{
class Player
{
float _playerWidth, _playerHeight;
Texture2D _cell;
Vector2 _position, _prevPosition, mousePosition;
//KeyboardState _keyState, _prevKeyState;
//MouseState _mouseState, _prevMouseState;
ContentManager playerContent;
Player player;
public Rectangle _cellBox;
float _speed, _rotation;
public Player(ContentManager content)
{
player = this;
playerContent = content;
}
public void LoadContent()
{
_position = new Vector2(200, 200);
_playerWidth = 55;
_playerHeight = 55;
_cell = playerContent.Load<Texture2D>("cell 2");
_cellBox = new Rectangle(0, 0, (int)_playerWidth, (int)_playerWidth);
_speed = 5;
_rotation = 0;
}
public void UpdateMovement()
{
MouseState mouse = Mouse.GetState();
mousePosition = new Vector2(mouse.X, mouse.Y);
Vector2 direction = mousePosition - _position;
direction.Normalize();
_rotation = (float)Math.Atan2((double)direction.Y, (double)direction.X);
if (mouse.LeftButton == ButtonState.Pressed)
{
_position += direction * _speed;
}
if (CheckWallCollision() == true)
{
_position.X = _speed;
}
_prevPosition = _position;
}
private bool CheckWallCollision()
{
foreach (Square square in Game1.Instance.multiCollision)
{
if(Game1.Instance.Collision(_position, _playerWidth, _playerHeight, square.Position, square.Width, square.Height) == true)
{
return true;
}
}
return false;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_cell, _position, _cellBox, Color.White, _rotation, new Vector2(_cell.Width / 2, _cell.Height / 2), 1.0f, SpriteEffects.None, 1.0f);
}
}
}
namespace Fission_Alpha_Build
{
public class Square
{
Vector2 boxPosition;
float _width;
float _height;
bool visible;
static Square _levelSquare;
Rectangle _rect;
Texture2D texture;
public Vector2 Position { get { return boxPosition; } }
public float Height { get { return _height; } }
public float Width { get { return _width; } }
public Rectangle Rect { get { return _rect; } }
public static Square levelSquare { get { return _levelSquare; } }
public Square()
{
_levelSquare = this;
}
public void LoadContent(int number)
{
LevelOne(number);
_rect = new Rectangle((int)boxPosition.X, (int)boxPosition.Y, (int)_width, (int)_height);
texture = Game1.Instance.Content.Load<Texture2D>("box");
}
public void LevelOne(int number)
{
switch (number)
{
case 1: boxPosition = new Vector2(400f, 400f);
_height = 55;
_width = 55;
visible = true;
break;
}
}
public void Draw(SpriteBatch spriteBatch)
{
if (visible == true)
{
spriteBatch.Draw(texture, boxPosition, _rect, Color.White);
}
}
}
}
namespace Fission_Alpha_Build
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
static Game1 _instance;
Player _player;
Square _levelSquare;
List<Square> _multiCollision;
public static Game1 Instance { get { return _instance; } }
public List<Square> multiCollision { get { return _multiCollision; } }
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
_graphics.PreferredBackBufferHeight = 720;
_graphics.PreferredBackBufferWidth = 1280;
_instance = this;
}
protected override void Initialize()
{
_player = new Player(Content);
_levelSquare = new Square();
_multiCollision = new List<Square>();
IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
_player.LoadContent();
_spriteBatch = new SpriteBatch(GraphicsDevice);
for (int i = 0; i < 2; i++)
{
Square square = new Square();
square.LoadContent(1);
multiCollision.Add(square);
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
_player.UpdateMovement();
base.Update(gameTime);
}
public bool Collision(Vector2 rect1XY, float rect1Width, float rect1Height, Vector2 rect2XY, float rect2Width, float rect2Height)
{
if ((rect1XY.Y + rect1Height) > rect2XY.Y && rect1XY.Y < (rect2XY.Y + rect2Height) && rect1XY.X < (rect2XY.X + rect2Width) && (rect1XY.X + rect1Width) > rect2XY.X)
{
return true;
}
else
{
return false;
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
_player.Draw(_spriteBatch);
foreach (Square square in multiCollision)
{
square.Draw(_spriteBatch);
}
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
Im still green with programming with C# in the XNA platform, so i wasnt able to find the problem. A point in the right direction would be appreciated, thanks.

New Topic/Question
Reply



MultiQuote





|