4 Replies - 715 Views - Last Post: 19 July 2012 - 12:48 PM

#1 BBuschRN  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 83
  • Joined: 28-February 12

Collision help

Posted 15 July 2012 - 02:24 PM

Just starting with XNA and playing around with collision, but it seems like it does not work. I'm using a book (Learning XNA 4.9, O'Reily.. pretty good) and am doing exactly what it says, but I must be missing something. Any thoughts?

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        //variables - general
        Texture2D background1;
        

        //shiva variables
        Texture2D shivaTexture;
        const float shivaSpeed = 6;
        Vector2 shiva = Vector2.Zero;
        Point shivaFrameSize = new Point(81, 96);
        Point shivaCurrentFrame = new Point(0, 0);

        //wizard variables
        Texture2D wizardTexuture;
        Vector2 wizard = new Vector2(100,100);
        Vector2 wizardSpeed = new Vector2(50, 50);
        Point wizardFrameSize = new Point(131, 76);
        Point wizardCurrentFrame = new Point(0, 0);

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            shivaTexture = Content.Load<Texture2D>("Images/shiva");
            wizardTexuture = Content.Load<Texture2D>("Images/wizard");
            background1 = Content.Load<Texture2D>("Images/blue-sky");
        }
       
        protected override void UnloadContent()
        {

        }
        
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit via console controller
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            //user controlled shiva via keyboard
            KeyboardState keyboadState = Keyboard.GetState();
            if (keyboadState.IsKeyDown(Keys.Left))
                shiva.X -= shivaSpeed;
            if (keyboadState.IsKeyDown(Keys.Right))
                shiva.X += shivaSpeed;
            if (keyboadState.IsKeyDown(Keys.Down))
                shiva.Y += shivaSpeed;
            if (keyboadState.IsKeyDown(Keys.Up))
                shiva.Y -= shivaSpeed;

            //user controlled shiva via xbox controller
            GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
            shivaPosition.X += shivaSpeed * gamepadState.ThumbSticks.Left.X;
            shivaPosition.Y += shivaSpeed * gamepadState.ThumbSticks.Left.Y;

            //keep shiva within window
            if (shiva.X < 0)
                shiva.X = 0;
            if (shiva.Y < 0)
                shiva.Y = 0;
            if (shiva.X > window.ClientBounds.Width - shivaFrameSize.X)
                shiva.X = window.ClientBounds.Width - shivaFrameSize.X;
            if (shiva.Y > window.ClientBounds.Height - shivaFrameSize.Y)
                shiva.Y = window.ClientBounds.Height - shivaFrameSize.Y;

            //wizard moving
            wizard += wizardSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            int MaxX = graphics.GraphicsDevice.Viewport.Width - shivaTexture.Width;
            int MinX = 0;
            int MaxY = graphics.GraphicsDevice.Viewport.Height - shivaTexture.Height;
            int MinY = 0;

            if (wizard.X > MaxX)
            {
                wizardSpeed.X *= -1;
                wizard.X = MaxX;
            }
            else if (wizard.X < MinX)
            {
                wizardSpeed.X *= -1;
                wizard.X = MinX;
            }
            if (wizard.Y > MaxY)
            {
                wizardSpeed.Y *= -1;
                wizard.Y = MaxY;
            }
            else if (wizard.Y < MinY)
            {
                wizardSpeed.Y *= -1;
                wizard.Y = MinY;
            }

            //collision detection
            if (Collide())
                Exit();

            base.Update(gameTime);
        }

        //collision method
        Vector2 wizardPosition = new Vector2(100,100);
        Vector2 shivaPosition = Vector2.Zero;

        protected bool Collide()
        {
            Rectangle shivaRect = new Rectangle((int)shivaPosition.X,
                (int)shivaPosition.Y, shivaFrameSize.X, shivaFrameSize.Y);
            Rectangle wizRect = new Rectangle((int)wizardPosition.X,
                (int)wizardPosition.Y, wizardFrameSize.X, wizardFrameSize.Y);

            return shivaRect.Intersects(wizRect);
        }

        //drawing graphics to game
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            //draw background
            spriteBatch.Begin();
            spriteBatch.Draw(background1,
                new Rectangle(0, 0, window.ClientBounds.Width,
                window.ClientBounds.Height), null,
                Color.White, 0, Vector2.Zero,
                SpriteEffects.None, 0);
            spriteBatch.End();

            //draw shiva
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(shivaTexture, shiva, Color.White);
            spriteBatch.End();
            
            //draw wizard
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(wizardTexuture, wizard, Color.White);
            spriteBatch.End();



            base.Draw(gameTime);
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Collision help

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Collision help

Posted 16 July 2012 - 06:11 AM

The code looks okay. What I would do is set a break point on the line of code below. I'd then step through until the return statement. I'd then inspect the values of the rectangles. If the collision isn't working right it is likely a problem with your rectangles. If the rectangles are constructed properly then the collision detection should work. If they aren't constructed properly then the collision detection won't work properly.

            Rectangle shivaRect = new Rectangle((int)shivaPosition.X,
                (int)shivaPosition.Y, shivaFrameSize.X, shivaFrameSize.Y);


Was This Post Helpful? 0
  • +
  • -

#3 BBuschRN  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 83
  • Joined: 28-February 12

Re: Collision help

Posted 16 July 2012 - 07:58 AM

Thanks for the assist. I went ahead in the book and I was working with classes (separated into sprite base, user sprite, auto sprite and sprite mngr) and when I was done it ended up working. Not sure what was different.. ((shrug))

Was also having issues getting my controller to work properly, but after research I found that XNA is picky with 3rd party controllers (I have a gamestop one..). Oh well, I ordered a wireless receivers so I can use my real controllers.

This post has been edited by BBuschRN: 16 July 2012 - 08:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 580
  • View blog
  • Posts: 1,287
  • Joined: 24-April 12

Re: Collision help

Posted 18 July 2012 - 07:57 AM

I've been very happy with my actual XBox 360 controllers on the PC. I had a wireless transciever go out on me one and had to buy a new controller with the new transciever. I also discovered by working with MS tech support at that time that the controllers do not communicate with the PC through the USB cable but simply use that to recharge the rechargable battery. So even when the cable is connected, they still communicate wirelessly (go figure). But other than that, I've been very happy with my XBox 360 controllers hooked up to the PC.
Was This Post Helpful? 0
  • +
  • -

#5 BBuschRN  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 83
  • Joined: 28-February 12

Re: Collision help

Posted 19 July 2012 - 12:48 PM

Same. Receiver came in yesterday and works perfectly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1