Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Bouncing Ball Movement in a 2D Level
Date 8/24/2008    Tags XNA    (2)

Update: The article A Better 2D Bouncing Ball Movement has a vastly improved algorithm for performing the 2D bouncing ball movement detailed in this article. You probably don't want to use the code below anymore (unless you love poor framerates). If you do and you get bad performance, don't say I didn't warn you.

In the game I am currently working on, I found the need to implement a bouncing ball type mechanic for a couple enemies. I essentially wanted some dumb enemies that bounce around the level as fodder for the player. Searching online, I found tutorials on how to make a ball type object bounce when it leaves the limits of the playing area, but I needed my enemies to bounce whenever they hit into a block onscreen.

Unable to find a suitable tutorial online (and satisfied that if it wasn't in Google, it doesn't exist), I went about creating my own. My game is too big to post everything, so I'm just going to include some snippets and walk through the logic of how it all comes together.

First off, I've got a few constant variables (should they really still be called variables if they're constant?) that allow me to easily check things like moving up and down. I have these included in a sprite class that all objects in my game inherit from.
        public const int MOVE_UP = -1;
        public const int MOVE_DOWN = 1;
        public const int MOVE_LEFT = -1;
        public const int MOVE_RIGHT = 1;

Now in my enemy class, I run the update function for each enemy during each game loop. LevelBlocks is a collection of every block in my level (they're all 16x16 tiles) that is passed into this function and Collision.checkLevelCollision() is a function that returns true if there is a collision between the levelBlocks and the vector position passed in. Source is a rectangle that holds the size of the enemy. EnemyDirection is a vector2 that holds the direction of (gasp) the enemy.

        public void Update(GameTime theGameTime, List<Block> levelBlocks)
        {

            Vector2 futurePosition = new Vector2();

            if (currentState == enemyState.Active)
            {

                futurePosition = CurrentPosition;
                futurePosition.Y += MOVE_UP * enemySpeed.Y;

                if (Collision.checkLevelCollision(levelBlocks, futurePosition, source))
                {
                    enemyDirection.Y = -enemyDirection.Y;
                }

                futurePosition = CurrentPosition;
                futurePosition.Y += MOVE_DOWN * enemySpeed.Y;

                if (Collision.checkLevelCollision(levelBlocks, futurePosition, source))
                {
                    enemyDirection.Y = -enemyDirection.Y;
                }

                futurePosition = CurrentPosition;
                futurePosition.X += MOVE_LEFT * enemySpeed.X;

                if (Collision.checkLevelCollision(levelBlocks, futurePosition, source))
                {
                    enemyDirection.X = -enemyDirection.X;
                }

                futurePosition = CurrentPosition;
                futurePosition.X += MOVE_RIGHT * enemySpeed.
                    
                    

Wellplayed.net
This article has been view 123 times.

Advertisement:

Comments

rAndall

Avatar

8/28/2008 1:11:18 PM

Nice tutorial. However, would you be interested in sharing your Collision class and methods. I'm an XNA/programming noob and im currently trying to implement some basic collision functionality in my own test case. Thanks again!"0"
Kris

Avatar

8/29/2008 4:57:34 AM

Sure thing, I'll post something about my collission detection routines next week. src="


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.