6 Replies - 1280 Views - Last Post: 23 January 2013 - 08:00 PM

#1 cookie73942  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-January 13

How to add collision detection to an object in the game.

Posted 13 January 2013 - 06:07 PM

Ok I am trying to add collison detection to an object in my game. real basic stuff. just started programming. this is my code.

 using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        

        Texture2D stickFigure;
        Vector2 texturePosition = new Vector2(0, 0);
        Texture2D Chess;
        Vector2 chessPosition = new Vector2(100, 100);
        


        KeyboardState keyboardState;

        float speed = 10;

        

        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);

            stickFigure = Content.Load <Texture2D>("StickFigure");
            Chess = Content.Load <Texture2D>("chess");


            

            // TODO: use this.Content to load your game content here
        }

        /// <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();

            keyboardState = Keyboard.GetState();
            
            if (keyboardState.IsKeyDown(Keys.Up))
            texturePosition.Y -= speed;
            if (keyboardState.IsKeyDown(Keys.Down))
                texturePosition.Y += speed;
            if (keyboardState.IsKeyDown(Keys.Left))
                texturePosition.X -= speed;
            if (keyboardState.IsKeyDown(Keys.Right))
                texturePosition.X += speed;

            if (texturePosition.X > window.ClientBounds.Width - 75)
            {
                texturePosition.X = window.ClientBounds.Width - 75;  
            }
            if (texturePosition.Y > window.ClientBounds.Height-stickFigure.Height)
            {
                texturePosition.Y = window.ClientBounds.Height-stickFigure.Height;
            }
            if (texturePosition.X <0)
            {
                texturePosition.X = 0;
            }
            if (texturePosition.Y <0)
            {
                texturePosition.Y = 0;
            }

            if (chessPosition.Y > window.ClientBounds.Height - 2)
                chessPosition.Y = window.ClientBounds.Height -2;
            



           


            // TODO: Add your update logic here

            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.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(stickFigure, texturePosition, Color.White);
            spriteBatch.End();

            spriteBatch.Begin();
            spriteBatch.Draw(Chess, chessPosition, Color.White);
            spriteBatch.End();
            

            base.Draw(gameTime);
        }
    }
}



I want the stickFigure to be able to hit the Chess and go back to 0,0 on the screen. I have tried looking for this. I am new to this if anyone can help.

Is This A Good Question/Topic? 0
  • +

Replies To: How to add collision detection to an object in the game.

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 314
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: How to add collision detection to an object in the game.

Posted 14 January 2013 - 12:28 AM

First of all, stop what you're doing. Do not attempt to learn by trying: follow some XNA and C# tutorials until you're ready. I recommend C# Station to learn the basics of C#, then Reimer's XNA tutorials to start getting into XNA.

What you've written doesn't follow an object-oriented approach, and will become very complex as it expands: you'll end up writing multiple lines of code you just don't need.

The best way to check for collision in 2D XNA games, by the way, is to use a Rectangle object that holds the size and position of each collidable object. In OO, this rectangle, as well as other members and methods for the class, will be part of a separate class such as "Player" or "Enemy". You can check if a Rectangle is about to intersect another by using its Intersects() method.
Was This Post Helpful? 0
  • +
  • -

#3 AllegroGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-October 12

Re: How to add collision detection to an object in the game.

Posted 18 January 2013 - 09:03 AM

View PostMrShoes, on 14 January 2013 - 12:28 AM, said:

First of all, stop what you're doing. Do not attempt to learn by trying


This is how I learn most things in programming :)
Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 314
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: How to add collision detection to an object in the game.

Posted 21 January 2013 - 02:32 AM

View PostAllegroGuy, on 18 January 2013 - 04:03 PM, said:

This is how I learn most things in programming :)/>


This is not good practice. Would you learn how electricity works by plugging yourself into the mains? Without a foundation understanding of what is happening, you will run into problems. Unfortunately, you can't make games in XNA without learning how to write code, and you will not learn code by taking a running jump. We see it all the time: people try it, say it will work, they create some major headaches for themselves.

I'm not saying don't experiment; without experimentation, there could be no innovation. But software development is a science as well as an art form, and demands it be studied correctly.
Was This Post Helpful? 0
  • +
  • -

#5 LarcaCode  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 21
  • Joined: 30-July 12

Re: How to add collision detection to an object in the game.

Posted 21 January 2013 - 03:29 AM

View PostMrShoes, on 14 January 2013 - 12:28 AM, said:

...
I recommend C# Station to learn the basics of C#, then Reimer's XNA tutorials to start getting into XNA.
...

Reimer's XNA Tutorials seem to be the most known :lol: . I've worked through them and in my point of view they are sometimes not as good as they should be for a beginner... but thats just my honest opinion.
I'd prefer RB Whitakers XNA Tutorials.

Cheers,
Larca
Was This Post Helpful? 0
  • +
  • -

#6 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: How to add collision detection to an object in the game.

Posted 21 January 2013 - 08:11 AM

@MrShoes - there is nothing wrong with jumping in and trying your own way. Not everyone learns the same way.

I whole heartedly believe in jumping in the deep end. It's not like you can actually drown in this medium.

It can be helpful to just try it your own and fail at it. There is a lot to learn in failure!

Quote

“I have not failed, I have just found 10,000 ways that don't work.”
-Albert Einstein


At the same time, OP, don't forget that there are tutorials and books out there that can give you direction. You will hit walls, and you will fail. Don't be afraid to seek out this already written material to research off of. We here in the forums love to help, but we don't have infinite amounts of time. So please take the time to do some solo-research before coming to us with questions because you've hit a wall.

This post has been edited by lordofduct: 21 January 2013 - 08:12 AM

Was This Post Helpful? 1
  • +
  • -

#7 AllegroGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-October 12

Re: How to add collision detection to an object in the game.

Posted 23 January 2013 - 08:00 PM

To the original poster, I started an XNA 2D space shooter tutorial series on youtube about 2 weeks ago, Were already about 12 videos in but it already covers:

Player class
Scrolling Backgrounds
Asteroid Class
Bullet Class
Shooting
And I'm recording the collision video's tonight

Check out the playlist here:
http://www.youtube.c...9QDbq2_2AS-0R1I

hope it helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1