1 Replies - 2197 Views - Last Post: 15 October 2012 - 09:27 AM Rate Topic: -----

#1 Raptormax9585  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-October 12

Collision Help

Posted 10 October 2012 - 11:44 PM

Ok so I've been programming a game as my final project, and its a simple game where you move a cell character around. right now its still in the prototype phase, since its early. So far I got the player movement down, based on where you clicked the mouse in the game and by keep holding the left mouse button, the player would move towards the mouse's position. I was also able to draw both the player and a second object, used to get collision done. Which is where the problem comes in. Now I know how to do collision, i had done it before, using a simple bool method in the Game1 class that accepts object 1's position, width and height, and object 2's position, width and height. the bool method works as i wanted, collision between any two objects that come into contact would make the player's position stop right at the edge of the second object. the working collision had different movement for the player, using keys to move instead of the mouse.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Collision Help

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Collision Help

Posted 15 October 2012 - 09:27 AM

It really would be helpful if you commented your code to make it easier to follow. Anyway, I can see basic code in there to detect your collision, but nothing in there to tell your game how the collision should be resolved, so what you need to do is call your ';resolver as soon as the collision is detected then adjust the positions of your objects accordingly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1