3 Replies - 1543 Views - Last Post: 23 July 2013 - 08:21 PM

#1 shenglee@83  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-July 13

draw a rectangle box to check collision

Posted 23 July 2013 - 07:22 AM

I am trying to draw a rectangle box for my rectangle collision for my enemy class. The reason i wan to do that is the ease me on checking is my enemy rectangle intersects with other object in my game.

But somehow i have no idea where to start. I hope that someone here can point it out where should i start to draw a rectangle box.

Below is my code. The name of my enemy rectangle is "enemyBound". Thanks for anyone who spend time to read this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Project_Battle_Of_Tanks
{
    class Enemy:Sprite
    {
       
        //int damageTimer, damageTime = 120;
        Vector2 chasePoint, linear, initialPos, newPosition;
        List<Vector2> patrolPoint = new List<Vector2>();
        Random random = new Random();
        Texture2D pixel;
        float moveDistance = 100;
        const int maxAmmo = 32;
        int ammo, count;
        float rate = 20, bulletSpeed = 10;
        int radius;
        float shootTime = 0f, secondsBetweenShots = 3.0f, turningTime;
        Boolean canShoot = false, collided;
        int collideTimer = 0;
        int collideDelay = (int)(60 * 1);
        Rectangle enemyBound;

        public Enemy(Vector2 pos)
            : base(pos)
        {
            position = pos;
            initialPos = pos;
            textureName = "tank1";
            solid = true;
            health = 100;
            speed =1;
            velocity = new Vector2(1, 0);
            radius = 10;
            listInit();
            turningTime = 0;
            collided = false;
        }

        public void listInit()
        {
            patrolPoint.Add(new Vector2(100, 400));
            patrolPoint.Add(new Vector2(100, 150));
            patrolPoint.Add(new Vector2(400, 150));
            patrolPoint.Add(new Vector2(400, 400));
        }
        
        public override void Update(GameTime gameTime)
        {
            UpdateArea();
            shootTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            turningTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
          

            if (turningTime > 3)
            {
                wander();
                turningTime = 0;
                
            }
          
             collisionWall();
             if (collisionWall())
                { collided = true; }
             else
                { collided = false; }
            rotation = point_direction(position.X, position.Y, newPosition.X, newPosition.Y);
                     
            if (shootTime > secondsBetweenShots)
            {
                canShoot = true;

                if (canShoot)
                {
                    
                  //  shoot();
                }
                shootTime = 0f;
                
            }
            else
            {   
                canShoot = false;
            }

            pushTo(speed, rotation);            
            base.Update( gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            

            origin = new Vector2(texture.Width / 2, texture.Height / 2);
            
            float degAdj = 90;
            rotation = rotation - MathHelper.ToRadians(degAdj);
            
            if (health <= 0)
            {
                alive = false;
                
            }
            else
            {
                spriteBatch.DrawString(Game1.font, "Health : " + health, new Vector2(this.position.X - 30, this.position.Y - 40), Color.Red);

                spriteBatch.DrawString(Game1.font, "linearDistance: " + this.position.X, new Vector2(200, 0), Color.Red);
                spriteBatch.DrawString(Game1.font, "rotate: " + rotation, new Vector2(200, 20), Color.Red);
            }

            spriteBatch.DrawString(Game1.font, "Collided?: " + collided, new Vector2(200, 30), Color.Red);

            
            base.Draw(spriteBatch, gameTime);
        }

        public void wander()
        {
            int rndAngle = random.Next(0, 180);

            newPosition.X = position.X + moveDistance * radius * (float)Math.Cos(rndAngle);
            newPosition.Y = position.Y + moveDistance * radius * (float)Math.Sin(rndAngle);

        }

        private float point_direction(float x, float y, float x2, float y2)
        {
            float diffx = x - x2;
            float diffy = y - y2;
            float rot = (float)Math.Atan2(diffy, diffx);
            return rot;
        }

        public override void pushTo(float spd, float dir)
        {
            // XNA Client Space is in the opposite quadrant of the positive rotation quadrant
            float newX = -(float)Math.Cos(dir);
            float newY = -(float)Math.Sin(dir);

            position.X += spd * (float)newX;
            position.Y += spd * (float)newY;
           
         
        }

        public void shoot()
        {
            ammo--;

            foreach (Sprite s in SpriteList.spriteList)
            {
                if (s.GetType() == typeof(Bullet) && !s.alive)
                {
                    s.position = position;
                    s.UpdateArea();
                    s.rotation = rotation;
                    s.speed = bulletSpeed;
                    s.alive = true;

                    break;
                }
            }
        }

      

        public bool collisionWall()
        {
            int enemyOffSet = 2;
            enemyBound = new Rectangle((int)this.position.X + enemyOffSet, (int)this.position.Y + enemyOffSet,
                                    this.texture.Width + (enemyOffSet * 2), this.texture.Height + (enemyOffSet * 2));
            
            foreach (Sprite s in SpriteList.spriteList)
                if (s.GetType() == typeof(Walls) && s.solid)
                {
                    if (enemyBound.Intersects(s.area))
                    {
                        return true;
                    }
                  

                }
            return false;
        }
      
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: draw a rectangle box to check collision

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,197
  • Joined: 14-February 08

Re: draw a rectangle box to check collision

Posted 23 July 2013 - 07:55 AM

Quote

But somehow I have no idea where to start.


Really? You got this far though. You know how to draw in XNA?
Was This Post Helpful? 0
  • +
  • -

#3 shenglee@83  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-July 13

Re: draw a rectangle box to check collision

Posted 23 July 2013 - 08:07 AM

View Poststayscrisp, on 23 July 2013 - 07:55 AM, said:

Quote

But somehow I have no idea where to start.


Really? You got this far though. You know how to draw in XNA?


lol...sure i know how to draw... but nvr mind then, i have found the way the to do that.... thanks anyway to read this thread....
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 581
  • View blog
  • Posts: 1,290
  • Joined: 24-April 12

Re: draw a rectangle box to check collision

Posted 23 July 2013 - 08:21 PM

First of all, this is an XNA question, so it belongs in the XNA forum. :-)

Second, I cover this in my tutorial on my website:

http://virtuallyprog...odeckPart6.html

Note that XNA has Axis Aligned Bounding Boxes, so you can never rotate them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1