Sign in to follow this  
Artificial_Institute

Bullet collision won't work.

Recommended Posts

Hey I am new to XNA and I am just playing around with the basics, so far I have a player an enemy and the player can shoot bullets, the problem I am having is that the collision test between the bullets Rectangle and the enemy's Rectangle won't return anything, in other words there is no collision, when there clearly is. I think the problem may be that the Rectangle surrounding the bullet is not attached to it properly, so when even though the draw bullet is colliding with the enemy, the bullets Rectangle is not, if that makes sence? Here is my Game1.cs code:
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame7
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D player;
        Texture2D enemy;
        Point position;
        Vector3 enemyPosition;
        Vector2 bulletPosition = Vector2.Zero;
        Vector2 velocity = new Vector2(450, 0);
        private bool enemyAlive;
        private int speed;
        Texture2D bulletTexture;
        Bullet bullet;
        List<Bullet> bulletList = new List<Bullet>();
        public int bulletDelay;
        SoundEffect bulletSound;
        string soundName = "shot";
        Texture2D w1;
        Texture2D w2;
        Texture2D w3;
        Texture2D w4;

        // The color data for the images; used for per pixel collision
        Color[] enemyTextureData;
        Color[] bulletTextureData;
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            w1 = Content.Load<Texture2D>("w1");
            w2 = Content.Load<Texture2D>("w2");
            w3 = Content.Load<Texture2D>("w3");
            w4 = Content.Load<Texture2D>("w4");
            //enemyPosition.X = 200;
            //enemyPosition.Y = 200;
            enemyPosition = new Vector3(400, 400,0);
            bulletPosition = new Vector2(position.X + 22, position.Y / 2);
            bulletSound = Content.Load<SoundEffect>(soundName);
            speed = 5;
            enemyAlive = true;
         //   bulletRec = new Rectangle(bulletPosition.X, bulletPosition.Y, bullet.Width, bullet.Height);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player = Content.Load<Texture2D>("player");
            enemy = Content.Load<Texture2D>("enemy");
            bulletTexture = Content.Load<Texture2D>("bullet");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            KeyboardState keyboard = Keyboard.GetState();
            Rectangle eRec = new Rectangle((int)enemyPosition.X, (int)enemyPosition.Y, enemy.Width, enemy.Height);
            Rectangle pRec = new Rectangle((int)position.X, (int)position.Y, player.Width, player.Height);
            bullet = new Bullet(bulletTexture);
            bullet.Position = new Vector2(position.X + player.Width + 2 - bulletTexture.Width / 2, position.Y + bulletTexture.Height/ 2);
            Rectangle bulletRec = new Rectangle((int)bullet.Position.X, (int)bullet.Position.Y, bulletTexture.Width, bulletTexture.Height);
            BoundingBox enemyBox = new BoundingBox();
            enemyBox.Min = new Vector3(enemyPosition.X, enemyPosition.Y, 0);
            enemyBox.Max = new Vector3(enemyPosition.X + enemy.Width, enemyPosition.Y + enemy.Height, 0);

            if (keyboard.IsKeyDown(Keys.Left))
            {
                position.X -= 1 * speed;

                if (pRec.Intersects(eRec))
                {
                    enemyAlive = false;
                }
            }
            else if (keyboard.IsKeyDown(Keys.Right))
            {
                position.X += 1 * speed;

                if (pRec.Intersects(eRec))
                {
                    enemyAlive = false;
                }
            }
            else if (keyboard.IsKeyDown(Keys.Up))
            {
                position.Y -= 1 * speed;

                if (pRec.Intersects(eRec))
                {
                    enemyAlive = false;
                }
            }
            else if (keyboard.IsKeyDown(Keys.Down))
            {
                position.Y += 1 * speed;

                if (pRec.Intersects(eRec))
                {
                    enemyAlive = false;
                }
            }

            else if (keyboard.IsKeyDown(Keys.LeftControl) && gameTime.ElapsedGameTime.TotalMilliseconds - bulletDelay > 16)
            {
                bulletDelay = (int)gameTime.ElapsedGameTime.TotalMilliseconds;
                bulletList.Add(bullet);
                //bulletSound.Play();
            }
            for (int i = 0; i < bulletList.Count; i++)
            {
                bulletList[i].Update(gameTime);
                if (bulletList[i].Position.X < -bulletTexture.Width)
                {
                    bulletList.RemoveAt(i);
                }
            }
            bullet.Update(gameTime);
            
            if (eRec.Intersects(bulletRec))
            {
                enemyAlive = false;
            }

            if (keyboard.IsKeyUp(Keys.LeftControl))
            {
                bulletDelay = 0;
            }

            if (enemyPosition.X < position.X)
            {
                enemyPosition.X += 1;
                if (eRec.Intersects(pRec))
                {
                    enemyAlive = false;
                }
            }
            if (enemyPosition.Y < position.Y)
            {
                enemyPosition.Y += 1;
                if (eRec.Intersects(pRec))
                {
                    enemyAlive = false;
                }
            }
            if (enemyPosition.X > position.X)
            {
                enemyPosition.X -= 1;
                if (eRec.Intersects(pRec))
                {
                    enemyAlive = false;
                }
            }
            if (enemyPosition.Y > position.Y)
            {
                enemyPosition.Y -= 1;
                if (eRec.Intersects(pRec))
                {
                    enemyAlive = false;
                }
            }


            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
            spriteBatch.Begin();
            spriteBatch.Draw(player, new Vector2(position.X, position.Y), Color.White);
            if (enemyAlive == true)
            {
                spriteBatch.Draw(enemy, new Vector2(enemyPosition.X, enemyPosition.Y), Color.White);
            }

            foreach (Bullet playerBullet in bulletList)
            {
                playerBullet.Draw(spriteBatch);
            }
            //spriteBatch.Draw(w1, new Vector2(10, 217), Color.White);
            //spriteBatch.Draw(w2, new Vector2(124, 75), Color.White);
            //spriteBatch.Draw(w3, new Vector2(131, 73), Color.White);
            //spriteBatch.Draw(w4, new Vector2(295, 0), Color.White);
            //spriteBatch.Draw(w4, new Vector2(350, 0), Color.White);
            spriteBatch.End();
        }

        static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                    Rectangle rectangleB, Color[] dataB)
        {
            // Find the bounds of the rectangle intersection
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

            // Check every point within the intersection bounds
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    // Get the color of both pixels at this point
                    Color colorA = dataA[(x - rectangleA.Left) +
                                         (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = dataB[(x - rectangleB.Left) +
                                         (y - rectangleB.Top) * rectangleB.Width];

                    // If both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        // then an intersection has been found
                        return true;
                    }
                }
            }

            // No intersection found
            return false;
        }
    }
}

And my bullet class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame7
{
    class Bullet
    {
        public Texture2D texture;
        private Vector2 position = Vector2.Zero;
        private Vector2 speed = new Vector2(450, 0);
        private Ray bulletRay = new Ray();

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector2 Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public Bullet(Texture2D texture)
        {
            this.texture = texture;
        }

        public void Update(GameTime gameTime)
        {
            //Ray bulletRay = new Ray();
            position += speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            Rectangle bulletRec = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            
            
            //spriteBatch.Begin();
            spriteBatch.Draw(texture, new Vector2(position.X,position.Y), Color.White);
            //spriteBatch.End();
        }


    }
}

Any and all help will be much appreciated!

Share this post


Link to post
Share on other sites
I'm not really sure we are talking about the bullet physics library here. There is probably something wrong with the intersection code. Have you tried stepping through the function with the debugger to see if the values make sense?

Share this post


Link to post
Share on other sites
click on the column just to the left of the line number or the line itself if you don't have line numbers shown. A red circle will appear and the game will pause and bring you back to VS each time it is about to execute that line of code. You will then be able to see in the bottom left corner what all the local variables are.

EDIT: A possible reason you're not getting an intersection is that the bullet is travelling too quickly. In one frame it could be the near side of the enemy and the next frame it could be the far side. In this case it would not intersect in either frame and the hit would be missed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this