XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Rectangle intersection - incorrect detected

Last post 25/01/2009 2:58 by Fabian Viking. 4 replies.
  • 23/01/2009 23:13

    Rectangle intersection - incorrect detected

    Hi guys!

    I'm having a bit of trouble with my asteroids based game, where a collision between two rectangles is detected up to a certain distance away from the asteroid, to the left, as well as the actual asteroid (which is fine).

    I have two classes, a Game1 class, which contains most of my code, and a GameObject class, which contains a constructor to create new torpedos/bullets, and asteroids/enemies. The collision is detected in the UpdateTorpedos() method, in the Game1 class. I am using animated sprites for the torpedos and enemies. The spritesheet for the torpedos is 25 frames long, and each frame is 10x10 px. The spritesheet for the enemies is also 25 frames long, and each frame 70x70 px.

    The game's not complete, it's a working project that I'm using on my course to help learn the XNA environment and C# language, but the only part I'm stuck on atm is the collision detection :s At first I thought it was because the destination rectangle (initialised destEnemyRect in GameObject, updated in UpdateEnemies()) was actually wider than the source rectangle, which controls which frame of the enemy spritesheet is displayed, but it's not :/

    Any help/ideas appreciated, thanks =]

    public class Game1 : Microsoft.Xna.Framework.Game 
        { 
            //misc fields 
            GraphicsDeviceManager graphics; 
            SpriteBatch spriteBatch; //necessary for sprites and sprite commands 
            GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One); 
            KeyboardState previousKeyboardState = Keyboard.GetState(); 
            Texture2D backgroundTexture; 
            Rectangle viewportRect; //to go around the backgroundTexture, it's the window boundaries 
            GameObject spaceCraft, homePad; 
            float speedy = 1.0f, boost = 4.0f; // lowSpeed/highSpeed(holdKey) of the spacecraft 
            GameObject[] torpedos, enemies; //there will be multiple torpedoes and enemies 
            int maxTorp = 10; //set maximum torpedoes to 10 
            float torpSpeed = 12.0f; //speed of torpedo, make faster than boost, above 
            float maxDistAllowed = 9000f; //kill torpedo after this distance 
            int maxEnemy = 5; //maximum amount of enemies on screen at once 
            Random random = new Random(); //make a random number 
            float timer = 0f; //to hold game's elapsed time (per update) 
             
            int spriteWidthHoPad = 70, spriteHeightHoPad = 70; //size of each frame in the spriteSheet 
            int spriteWidthTorp = 10, spriteHeightTorp = 10; 
            int spriteWidthEnemy = 35, spriteHeightEnemy = 35; 
            Rectangle srcHoPadRect, srcTorpRect, srcEnemyRect; 
            Color transparentColor = new Color(255, 255, 255, 150); //to make a sprite semi transparent 
     
            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(); 
            } 
     
            /// <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); 
                spaceCraft = new GameObject(Content.Load<Texture2D>("Sprites\\spaceCraft")); //initialise spaceCraft 
                spaceCraft.position = new Vector2( 
                    graphics.GraphicsDevice.Viewport.Width / 2, 
                    graphics.GraphicsDevice.Viewport.Height / 2); //set initial position of spacecraft to centre of screen 
     
                homePad = new GameObject(Content.Load<Texture2D>("Sprites\\home")); //initialise homepad 
                homePad.position = new Vector2( 
                    (graphics.GraphicsDevice.Viewport.Width), 
                    (graphics.GraphicsDevice.Viewport.Height)); 
                homePad.frameCount = 25; 
                homePad.currentFrame = 0; 
                homePad.intervalTime = 1000f / 25f; 
     
                backgroundTexture = Content.Load<Texture2D>("Sprites\\background"); 
                viewportRect = new Rectangle(0, 0, 
                    graphics.GraphicsDevice.Viewport.Width, 
                    graphics.GraphicsDevice.Viewport.Height); 
     
                torpedos = new GameObject[maxTorp]; //create array of GameObjects 'torpedos' of size maxTorp 
                for (int i = 0; i < maxTorp; i++) 
                { 
                    torpedos[i] = new GameObject(Content.Load<Texture2D>("Sprites\\torpedoSprite")); 
                    torpedos[i].frameCount = 25; 
                    torpedos[i].currentFrame = 0; 
                    torpedos[i].intervalTime = 1000f / 10f; 
                } 
     
                enemies = new GameObject[maxEnemy]; //create array of GameObjects 'enemies' of size maxEnemy 
                for (int i = 0; i < maxEnemy; i++) 
                { 
                    enemies[i] = new GameObject(Content.Load<Texture2D>("Sprites\\enemy")); 
                    enemies[i].frameCount = 25; 
                    enemies[i].currentFrame = 0; 
                    enemies[i].intervalTime = 1000f / 25f; 
                } 
            } 
     
            /// <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(); //press back to escape 
     
                else if (Keyboard.GetState().IsKeyDown(Keys.Escape)) 
                    this.Exit(); //press esc to escape 
     
     
                /****HomePad Sprite Animation****/ 
                timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; 
                if (timer > homePad.intervalTime) 
                { 
                    homePad.intervalTime++; //next frame in the sprite (does nothing except provide a number) 
                    if (homePad.currentFrame > homePad.frameCount - 1) 
                    { 
                        homePad.currentFrame = 0; //if it's reached the end, reset frame number 
                        timer = 0; //reset the timer to 0 again 
                    } 
                } 
     
                srcHoPadRect = new Rectangle( 
                    homePad.currentFrame * spriteWidthHoPad, //see fields 
                    0, //always at the top (sprite is only one line of images) 
                    spriteWidthHoPad, 
                    spriteHeightHoPad); 
                /*End HomePad Sprite Animation****/ 
     
                /****Torpedo Sprite Animation****/ 
                foreach (GameObject torpedo in torpedos) 
                { 
                    timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; 
                    if (timer > torpedo.intervalTime) 
                    { 
                        torpedo.currentFrame++; //next frame in the sprite (does nothing except provide a number) 
                        if (torpedo.currentFrame > torpedo.frameCount - 1) 
                        { 
                            torpedo.currentFrame = 0; //if it's reached the end, reset frame number 
                            timer = 0; //reset the timer to 0 again 
                        } 
                    } 
     
     
                    srcTorpRect = new Rectangle( 
                        torpedo.currentFrame * spriteWidthTorp, //see fields 
                        0, //always at the top (sprite is only one line of images) 
                        spriteWidthTorp, 
                        spriteHeightTorp); 
                } 
                /*End Torpedo Sprite Animation****/ 
     
                /****Enemy Sprite Animation****/ 
                foreach (GameObject enemy in enemies) 
                { 
                    timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; 
                    if (timer > enemy.intervalTime) 
                    { 
                        enemy.currentFrame++; //next frame in the sprite (does nothing except provide a number) 
                        if (enemy.currentFrame > enemy.frameCount - 1) 
                        { 
                            enemy.currentFrame = 0; //if it's reached the end, reset frame number 
                            timer = 0; //reset the timer to 0 again 
                        } 
                    } 
     
                    srcEnemyRect = new Rectangle( 
                        enemy.currentFrame * spriteWidthEnemy, //see fields 
                        0, //always at the top (sprite is only one line of images) 
                        spriteWidthEnemy, 
                        spriteHeightEnemy); 
     
                } 
                /*End Enemy Sprite Animation****/ 
     
                /*** Moving the Spacecraft ***/ 
                GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); 
                spaceCraft.rotation += gamePadState.ThumbSticks.Left.X * 0.1f; 
    #if !XBOX 
                KeyboardState keyboardState = Keyboard.GetState(); //check the keyboard out, what's happening 
                 
                if (keyboardState.IsKeyDown(Keys.Left) ^ keyboardState.IsKeyDown(Keys.A)) 
                {spaceCraft.rotation -= 0.1f;} //if left is pressed, rotate left 
     
                if (keyboardState.IsKeyDown(Keys.Right) ^ keyboardState.IsKeyDown(Keys.D)) 
                {spaceCraft.rotation += 0.1f;} //if right is pressed, rotate right 
     
                if (keyboardState.IsKeyDown(Keys.Up) ^ keyboardState.IsKeyDown(Keys.W)) 
                { 
                    spaceCraft.velocity = new Vector2( 
                        (float)Math.Cos(spaceCraft.rotation - MathHelper.PiOver2), 
                        (float)Math.Sin(spaceCraft.rotation - MathHelper.PiOver2)); 
                    spaceCraft.position += spaceCraft.velocity * boost; 
                } //move forward (mathhelper because it originally moved to the right) 
     
                if (keyboardState.IsKeyDown(Keys.Down) ^ keyboardState.IsKeyDown(Keys.S)) 
                { 
                    spaceCraft.velocity = new Vector2( 
                            (float)Math.Cos(spaceCraft.rotation + MathHelper.PiOver2), 
                            (float)Math.Sin(spaceCraft.rotation + MathHelper.PiOver2)); 
                    spaceCraft.position += spaceCraft.velocity * boost; 
                } //move forward (mathhelper because it originally moved to the right) 
     
                if (keyboardState.IsKeyDown(Keys.E)) 
                { 
                    spaceCraft.velocity = new Vector2( 
                            (float)-Math.Cos(spaceCraft.rotation + MathHelper.Pi), 
                            (float)Math.Sin(spaceCraft.rotation + MathHelper.Pi)); 
                    spaceCraft.position += spaceCraft.velocity * boost; 
                } //strafe right 
     
                if (keyboardState.IsKeyDown(Keys.Q)) 
                { 
                    spaceCraft.velocity = new Vector2( 
                            (float)Math.Cos(spaceCraft.rotation + MathHelper.Pi), 
                            (float)Math.Sin(spaceCraft.rotation + MathHelper.Pi)); 
                    spaceCraft.position += spaceCraft.velocity * boost; 
                } //strafe left 
                 
                spaceCraft.position += spaceCraft.velocity * speedy; //move from current position in vector set 
     
                if (keyboardState.IsKeyDown(Keys.LeftControl)) 
                {spaceCraft.velocity = Vector2.Zero;} //press leftCtrl to stop moving (set velocity to 0) 
     
                if (keyboardState.IsKeyDown(Keys.LeftShift)) 
                { 
                    spaceCraft.position = new Vector2( 
                       graphics.GraphicsDevice.Viewport.Width / 2, 
                       graphics.GraphicsDevice.Viewport.Height / 2); 
                    spaceCraft.velocity = Vector2.Zero; 
                } //press leftShift to reset position to middle of screen, and set speed to 0 (stationary) 
     
                /******** CONTROLLING SPEED IS A BIT CUMBERSOME
                if (keyboardState.IsKeyDown(Keys.PageUp))
                {speedy++;} //decrease speed of spaceCraft
                if (keyboardState.IsKeyDown(Keys.PageDown))
                { speedy--; } //decrease speed of spaceCraft
                **********************************************/ 
    #endif 
                if (spaceCraft.position.Y < 0) //these four IFs sort out the spacecraft if it goes off screen 
                { spaceCraft.position.Y = graphics.GraphicsDevice.Viewport.Height; } 
     
                if (spaceCraft.position.Y > graphics.GraphicsDevice.Viewport.Height) 
                { spaceCraft.position.Y = 0; } 
     
                if (spaceCraft.position.X < 0) 
                { spaceCraft.position.X = graphics.GraphicsDevice.Viewport.Width; } 
     
                if (spaceCraft.position.X > graphics.GraphicsDevice.Viewport.Width) 
                { spaceCraft.position.X = 0; }  
                 
                /*** End : Moving the Spacecraft ***/ 
     
                /*** Firing from the Spacecraft ***/ 
                 
                if (gamePadState.Buttons.A == ButtonState.Pressed &&  
                    previousGamePadState.Buttons.A == ButtonState.Released) 
                { 
                    FireTorpedo(); 
                } 
                 
                previousGamePadState = gamePadState; 
    #if !XBOX 
                if (keyboardState.IsKeyDown(Keys.Space) && 
                    previousKeyboardState.IsKeyUp(Keys.Space)) 
                { 
                    FireTorpedo(); 
                } 
     
                previousKeyboardState = keyboardState; 
    #endif 
                UpdateTorpedo(); 
                UpdateEnemies(); 
                 
                /*** End: Firing from the Spacecraft ***/ 
     
                 
                base.Update(gameTime); 
            } 
     
     
            /* This method sets the velocity of torpedos */  
            public void FireTorpedo() 
            { 
                foreach (GameObject torpedo in torpedos) 
                { 
                    if (!torpedo.alive) 
                    { 
                        torpedo.alive = true
                        torpedo.currentPosition = Vector2.Zero; 
                        torpedo.distTrav = Vector2.Zero; 
                        torpedo.position = spaceCraft.position; //so that centers match up 
                        torpedo.startPosition = torpedo.position; //store the original position 
                        torpedo.velocity = new Vector2( 
                            (float)Math.Cos(spaceCraft.rotation - MathHelper.PiOver2), 
                            (float)Math.Sin(spaceCraft.rotation - MathHelper.PiOver2)) * torpSpeed; 
                        return
                    } 
                } 
            } 
            /* End setting velocity of torpedos */ 
     
            /*Update Enemies*
             * moves the enemies, resetting alive if dead*/ 
     
            public void UpdateEnemies() 
            { 
                foreach (GameObject enemy in enemies) 
                { 
                    if (enemy.alive) 
                    { 
                        enemy.insideDestRect = new Rectangle( 
                            (int)(enemy.position.X += enemy.velEnX), 
                            (int)(enemy.position.Y += enemy.velEnY), 
                            enemy.sprite.Width/(enemy.frameCount*2), 
                            enemy.sprite.Height/2); 
     
                        if (enemy.position.Y < 0) //these four IFs sort out the spacecraft if it goes off screen 
                        { enemy.position.Y = graphics.GraphicsDevice.Viewport.Height; } 
     
                        if (enemy.position.Y > graphics.GraphicsDevice.Viewport.Height) 
                        { enemy.position.Y = 0; } 
     
                        if (enemy.position.X < 0) 
                        { enemy.position.X = graphics.GraphicsDevice.Viewport.Width; } 
     
                        if (enemy.position.X > graphics.GraphicsDevice.Viewport.Width) 
                        { enemy.position.X = 0; }  
                    } 
                     
                    else //if its dead 
                    { 
                        if (!enemy.graveYard) 
                        { 
                            enemy.alive = true//reactivate the enemy 
                            enemy.insideDestRect = new Rectangle( 
                                random.Next(0, 800), 
                                random.Next(0, 600), 
                                spriteWidthEnemy, 
                                spriteHeightEnemy); 
                            enemy.velEnX = random.Next(-15, 15); //velocity here. 
                            enemy.velEnY = random.Next(-15, 15); 
                        } 
                    } 
                } 
            } 
             
     
     
     
            /* This method implements the velocity - makes it move.
             * It also checks to see if the torpedo hit an enemy,
             * kills the enemy + torpedo and ups the score */ 
     
            public void UpdateTorpedo()  
            { 
                foreach (GameObject torpedo in torpedos) //does "if" for all the objects in cannonballs 
                { 
                    if (torpedo.alive) 
                    { 
                        torpedo.position += torpedo.velocity; //move it 
                        if (torpedo.position.Y < 0) //if ball goes off the screen, redraw on other side 
                        { 
                            torpedo.position.Y = graphics.GraphicsDevice.Viewport.Height; 
                        } 
     
                        if (torpedo.position.Y > graphics.GraphicsDevice.Viewport.Height) //if ball goes off the screen, redraw on other side 
                        { 
                            torpedo.position.Y = 0; 
                        } 
     
                        if (torpedo.position.X > graphics.GraphicsDevice.Viewport.Width) //if ball goes off the screen, redraw on other side 
                        { 
                            torpedo.position.X = 0; 
                        } 
     
                        if (torpedo.position.X < 0) //if ball goes off the screen, redraw on other side 
                        { 
                            torpedo.position.X = graphics.GraphicsDevice.Viewport.Width; 
                        } 
                         
                        /*kill torpedo after distance*/ 
                        torpedo.currentPosition = new Vector2( 
                            torpedo.position.X, 
                            torpedo.position.Y); 
     
                        torpedo.distTrav += torpedo.currentPosition; 
                         
                        float distance = Vector2.Distance(torpedo.startPosition, torpedo.distTrav); 
                         
                        if (distance > maxDistAllowed) 
                        { 
                            System.Console.WriteLine(distance); 
                            torpedo.alive = false
                        } 
                        /*end kill torpedo after distance*/ 
     
                        Rectangle torpedoRect = new Rectangle( 
                            (int)torpedo.position.X, 
                            (int)torpedo.position.Y, 
                            torpedo.sprite.Width, 
                            torpedo.sprite.Height); 
                         
                        foreach (GameObject enemy in enemies) 
                        { 
                             
                            if (enemy.insideDestRect.Intersects(torpedoRect)) 
                            { 
                                torpedo.alive = false
                                enemy.alive = false//kill the torpedo and ship 
                                enemy.graveYard = true//if true, will NOT respawn 
                                System.Console.WriteLine("Enemy@" + enemy.insideDestRect.X + ", " + enemy.insideDestRect.Y); 
                                System.Console.WriteLine("Torp@" + torpedo.currentPosition); 
                                //score++; //add one point for every collision 
                                break
                            } 
                        } 
                    } 
                } 
            } 
            /*END move the torpedos checking if it hit */ 
     
            /// <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) 
            { 
                graphics.GraphicsDevice.Clear(Color.CornflowerBlue); 
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend); 
                spriteBatch.Draw(backgroundTexture, viewportRect, Color.White); 
                /* Torpedos ******/ 
                foreach (GameObject torpedo in torpedos) //for all that exist 
                { 
                    if (torpedo.alive) //no point if it's dead 
                    { 
                        Rectangle destTorpRect = new Rectangle( //create a rectangle for each torpedo alive 
                            (int)torpedo.position.X, 
                            (int)torpedo.position.Y, 
                            spriteWidthTorp, 
                            spriteHeightTorp); 
     
                        spriteBatch.Draw( 
                            torpedo.sprite, 
                            destTorpRect, 
                            srcTorpRect, 
                            Color.White); 
                    } 
                } 
                /*End Torpedos*/ 
     
     
                spriteBatch.Draw( //draw the spaceCraft on the screen 
                    spaceCraft.sprite, 
                    spaceCraft.position, 
                    null
                    Color.White, 
                    spaceCraft.rotation, 
                    spaceCraft.center, 
                    1.0f, 
                    SpriteEffects.None, 0); 
     
                /**** Home Pad ****/ 
                Rectangle destHoPadRect = new Rectangle( 
                    (int) (graphics.GraphicsDevice.Viewport.Width / 2)- (spriteWidthHoPad/7 *5)/2, //draw it in the centre of the screen 
                    (int)(graphics.GraphicsDevice.Viewport.Height / 2)- (spriteWidthHoPad/7 *5)/2, 
                    spriteWidthHoPad/7 *5, //draw it slightly smaller 
                    spriteHeightHoPad/7 *5); 
     
                spriteBatch.Draw( 
                    homePad.sprite, 
                    destHoPadRect, 
                    srcHoPadRect, 
                    transparentColor); 
     
                /****end Home Pad****/ 
     
                /*** Enemy SHips***/ 
                foreach (GameObject enemy in enemies) 
                { 
                    spriteBatch.Draw( 
                        enemy.sprite, 
                        enemy.insideDestRect, 
                        srcEnemyRect, 
                        transparentColor); 
                } 
                /*** End Enemy Ships ***/ 
                
     
                spriteBatch.End(); 
                base.Draw(gameTime); 
            } 
        } 


    class GameObject 
        { 
            public Texture2D sprite; 
            public Vector2 position; 
            public float rotation, intervalTime; 
            public int velEnX, velEnY, currentFrame, frameCount; 
            public Vector2 center, velocity, startPosition, currentPosition, distTrav; 
            public bool alive, graveYard; 
            public Rectangle insideDestRect; 
     
            public GameObject(Texture2D loadedTexture) 
            { 
                rotation = 0.0f; 
                position = Vector2.Zero; 
                sprite = loadedTexture; 
                intervalTime = 0; //time to spend on each frame of sprite animation (if any) 
                currentFrame = 0; //desired frame to be viewed on sprite animation 
                frameCount = 0; //how many frames are there in this animation? 
                center = new Vector2(sprite.Width / 2, sprite.Height / 2); //define the center of rotation 
                velocity = Vector2.Zero; //set inital velocity 
                alive = false//should this be drawn? 
                startPosition = Vector2.Zero; //where does this object start? 
                currentPosition = Vector2.Zero; //where is this object at the current state? 
                distTrav = Vector2.Zero; //how far has the object travelled? 
                graveYard = false//set true if never to be drawn again for session 
                insideDestRect = new Rectangle( 
                    (int)this.position.X, 
                    (int)this.position.Y, 
                    sprite.Width, 
                    sprite.Height); //where is the sprite to be displayed? 
     
            } 
        } 
     



  • 23/01/2009 23:46 In reply to

    Re: Rectangle intersection - incorrect detected

    In "UpdateTorpedo()", try to change

    "Rectangle torpedoRect = new Rectangle( (int)torpedo.position.X, (int)torpedo.position.Y, torpedo.sprite.Width, torpedo.sprite.Height); "

    To

    "Rectangle torpedoRect = new Rectangle( (int)torpedo.position.X, (int)torpedo.position.Y, spriteWidthTorp, spriteHeightTorp); "

  • 24/01/2009 13:46 In reply to

    Re: Rectangle intersection - incorrect detected

    ...i love you.


    haha thanks man XD I hope to learn enough in the next few weeks to be able to spot things like this, and hopefully answer a few questions too!
  • 25/01/2009 2:28 In reply to

    Re: Rectangle intersection - incorrect detected

    Another tip for you:

    Try to use the classes more, that would save you a lot of time. Don't forget that U can create a class and then make a another class that has the same functions.

    With this game I would create a class that has an image, can move around on the screen, and has a collision box (witch adapt to the size).
    Then you just make make a ship, enemy and bullet classes based on that.
  • 25/01/2009 2:58 In reply to

    Re: Rectangle intersection - incorrect detected

    Here is an exampel of how you can better up your code, I'm only showing the structure, you have to add the code yourself..
    I hope you will understand, if you don't - please ask.

        public class Game1 : Microsoft.Xna.Framework.Game 
        { 
            //All the objects can be collected in one list for updates 
            List<spaceObject> spaceObjects = new List<spaceObject>(); 
     
            protected override void Update(GameTime gameTime) 
            { 
                foreach (spaceObject oneObject in spaceObjects) 
                { 
                    oneObject.Update(gameTime); 
                } 
            } 
     
            protected override void Draw(GameTime gameTime) 
            { 
                foreach (spaceObject oneObject in spaceObjects) 
                { 
                    oneObject.Draw(spriteBatch); 
                } 
            } 
        } 
     
        abstract class spaceObject 
        { 
            //Constains its image, size, velocity, animationspeed and so on 
            //Exampel: 
            Vector2 mySize; //Use these variables both when you render the image and when checking collition 
            Vector2 myPos; 
     
            public void Update(GameTime gameTime) 
            { 
                //Update the animation 
                //Update the movement 
                //check collitions 
            } 
     
            public void Draw(SpriteBatch batch) 
            {  
                //Let the object draw itself 
            } 
     
            public Rectangle CollitionRectangle 
            { 
                get  
                { 
                    return new Rectangle((int)myPos.X, (int)myPos.Y, (int)mySize.X, (int)mySize.Y); 
                } 
            } 
     
        } 
     
        class ship : spaceObject 
        { 
           //contains all the code from spaceObject 
           //Override any changes you want to make, or add new 
           //Exampel: 
            public Projectile FireBullet() 
            { 
                //Create a new bullet in the ships direction 
                //return the new projectile 
            } 
        } 
     
        class Projectile : spaceObject 
        { 
           //contains all the code from spaceObject 
           //Override any changes you want to make, or add new 
            
        } 
     
        class Enemy : spaceObject 
        { 
            //contains all the code from spaceObject 
            //Override any changes you want to make, or add new 
     
        } 
     

Page 1 of 1 (5 items) Previous Next