Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Collision Help (XNA) : Please Help!

Collision Help (XNA) : Please Help!

Scheduled Pinned Locked Moved C#
csharpgraphicsgame-devperformancehelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MasterSharp
    wrote on last edited by
    #1

    #region Using using System; using System.Collections.Generic; 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.Net; using Microsoft.Xna.Framework.Storage; #endregion namespace Bouncer { #region Bouncer Game : Main public class BouncerGame : Microsoft.Xna.Framework.Game { #region Variables GraphicsDeviceManager Graphics; SpriteBatch SpriteBatch; SpriteFont SpriteFont; Player Player; StaticBlock[] StaticBlock = new StaticBlock[200]; int Stage = 1; enum GameState { Title, Game, StageSelect, Help } GameState State = new GameState(); #endregion #region Performance Variables int FPS, FrameCounter; TimeSpan ElapsedTime = TimeSpan.Zero; #endregion #region Initialization public BouncerGame() { Graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { IsFixedTimeStep = false; InitializePlayer(); InitializeStaticBlock(); base.Initialize(); } private void InitializePlayer() { Player = new Player(); } private void InitializeStaticBlock() { for (int numberOfStaticBlocks = 0; numberOfStaticBlocks < 200; ++numberOfStaticBlocks) { StaticBlock[numberOfStaticBlocks] = new StaticBlock(); } } #endregion #region Load protected override void LoadContent() { SpriteBatch = new SpriteBatch (GraphicsDevice); SpriteFont = Content.Load<SpriteFont> ("Trebuchet MS"); Player.Texture_Player = Content.Load<Texture2D> ("BouncerSprite_Bouncer"); for (int numberOfStaticBlocks = 0; numberOfStaticBlocks < 200; ++numberOfStaticBlocks) { StaticBlock[numberOfStaticBlocks].Texture_StaticBlock = Content.Load<Texture2D> ("BouncerSprite_StaticBlock");

    L M 2 Replies Last reply
    0
    • M MasterSharp

      #region Using using System; using System.Collections.Generic; 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.Net; using Microsoft.Xna.Framework.Storage; #endregion namespace Bouncer { #region Bouncer Game : Main public class BouncerGame : Microsoft.Xna.Framework.Game { #region Variables GraphicsDeviceManager Graphics; SpriteBatch SpriteBatch; SpriteFont SpriteFont; Player Player; StaticBlock[] StaticBlock = new StaticBlock[200]; int Stage = 1; enum GameState { Title, Game, StageSelect, Help } GameState State = new GameState(); #endregion #region Performance Variables int FPS, FrameCounter; TimeSpan ElapsedTime = TimeSpan.Zero; #endregion #region Initialization public BouncerGame() { Graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { IsFixedTimeStep = false; InitializePlayer(); InitializeStaticBlock(); base.Initialize(); } private void InitializePlayer() { Player = new Player(); } private void InitializeStaticBlock() { for (int numberOfStaticBlocks = 0; numberOfStaticBlocks < 200; ++numberOfStaticBlocks) { StaticBlock[numberOfStaticBlocks] = new StaticBlock(); } } #endregion #region Load protected override void LoadContent() { SpriteBatch = new SpriteBatch (GraphicsDevice); SpriteFont = Content.Load<SpriteFont> ("Trebuchet MS"); Player.Texture_Player = Content.Load<Texture2D> ("BouncerSprite_Bouncer"); for (int numberOfStaticBlocks = 0; numberOfStaticBlocks < 200; ++numberOfStaticBlocks) { StaticBlock[numberOfStaticBlocks].Texture_StaticBlock = Content.Load<Texture2D> ("BouncerSprite_StaticBlock");

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I don't think we needed all that code to try and answer the question at the bottom... I once did multi-object collision by drawing everything in a hidden bitmap, using: - two colors only (empty and filled); - reduced resolution (if everything is a multiple of 25, then divide all coordinates by 25). In such a bitmap you can detect possible moves and collisions before you apply them to the "real world". You may want to use more than two colors, say one for empty and one for each of the objects, that way you know immediately with which object you are going to collide. Final remark: it does not have to be a real Bitmap and GDI+ operations, you could simply use a two-dimensional array and manipulate the "pixels" yourself, removing a rectangular object is a simple clear in two nested fors, adding it at a (slightly) different position is similar. Collision detection can be handled by just peeking at the intended new positions of the leading edges. BTW: nevertheless I like to do it in a paintable way, and for debugging purposes I have a checkbox that chooses to show either the real world or the simulated world, at the same location on screen (rescaled to undo the reduced resolution); doing so any bugs become noticeable right away. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I don't think we needed all that code to try and answer the question at the bottom... I once did multi-object collision by drawing everything in a hidden bitmap, using: - two colors only (empty and filled); - reduced resolution (if everything is a multiple of 25, then divide all coordinates by 25). In such a bitmap you can detect possible moves and collisions before you apply them to the "real world". You may want to use more than two colors, say one for empty and one for each of the objects, that way you know immediately with which object you are going to collide. Final remark: it does not have to be a real Bitmap and GDI+ operations, you could simply use a two-dimensional array and manipulate the "pixels" yourself, removing a rectangular object is a simple clear in two nested fors, adding it at a (slightly) different position is similar. Collision detection can be handled by just peeking at the intended new positions of the leading edges. BTW: nevertheless I like to do it in a paintable way, and for debugging purposes I have a checkbox that chooses to show either the real world or the simulated world, at the same location on screen (rescaled to undo the reduced resolution); doing so any bugs become noticeable right away. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        M Offline
        M Offline
        MasterSharp
        wrote on last edited by
        #3

        Does GDI and such apply fo XNA? And thank you for your reply, even if I have some trouble with it. Another Possible Way?: Is there a way to check collisions with all of the StaticBlocks with a for loop with good performance results? I tried something like this, but the framerate dipped to around 10...

        - I love D-flat! - Need. More. Code.

        L 1 Reply Last reply
        0
        • M MasterSharp

          Does GDI and such apply fo XNA? And thank you for your reply, even if I have some trouble with it. Another Possible Way?: Is there a way to check collisions with all of the StaticBlocks with a for loop with good performance results? I tried something like this, but the framerate dipped to around 10...

          - I love D-flat! - Need. More. Code.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          MasterSharp wrote:

          Does GDI and such apply fo XNA?

          I don't know. Anyway, I told you you don't really need GDI+, a 2D array and some simple loops is sufficient. Furthermore if XNA does not support GDI+, it must have another way to create and operate on bitmaps without showing them all the time. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          M 1 Reply Last reply
          0
          • L Luc Pattyn

            MasterSharp wrote:

            Does GDI and such apply fo XNA?

            I don't know. Anyway, I told you you don't really need GDI+, a 2D array and some simple loops is sufficient. Furthermore if XNA does not support GDI+, it must have another way to create and operate on bitmaps without showing them all the time. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


            M Offline
            M Offline
            MasterSharp
            wrote on last edited by
            #5

            OK, thanks, Luc!

            - I love D-flat! - Need. More. Code.

            1 Reply Last reply
            0
            • M MasterSharp

              #region Using using System; using System.Collections.Generic; 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.Net; using Microsoft.Xna.Framework.Storage; #endregion namespace Bouncer { #region Bouncer Game : Main public class BouncerGame : Microsoft.Xna.Framework.Game { #region Variables GraphicsDeviceManager Graphics; SpriteBatch SpriteBatch; SpriteFont SpriteFont; Player Player; StaticBlock[] StaticBlock = new StaticBlock[200]; int Stage = 1; enum GameState { Title, Game, StageSelect, Help } GameState State = new GameState(); #endregion #region Performance Variables int FPS, FrameCounter; TimeSpan ElapsedTime = TimeSpan.Zero; #endregion #region Initialization public BouncerGame() { Graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { IsFixedTimeStep = false; InitializePlayer(); InitializeStaticBlock(); base.Initialize(); } private void InitializePlayer() { Player = new Player(); } private void InitializeStaticBlock() { for (int numberOfStaticBlocks = 0; numberOfStaticBlocks < 200; ++numberOfStaticBlocks) { StaticBlock[numberOfStaticBlocks] = new StaticBlock(); } } #endregion #region Load protected override void LoadContent() { SpriteBatch = new SpriteBatch (GraphicsDevice); SpriteFont = Content.Load<SpriteFont> ("Trebuchet MS"); Player.Texture_Player = Content.Load<Texture2D> ("BouncerSprite_Bouncer"); for (int numberOfStaticBlocks = 0; numberOfStaticBlocks < 200; ++numberOfStaticBlocks) { StaticBlock[numberOfStaticBlocks].Texture_StaticBlock = Content.Load<Texture2D> ("BouncerSprite_StaticBlock");

              M Offline
              M Offline
              Mark Churchill
              wrote on last edited by
              #6

              Typically you'd store your scene data in a quadtree setup which would let you pick out all the StaticBlocks that are relevant to your player (in the same region). Then its fairly trivial to check if two squares will intersect...

              Mark Churchill Director Dunn & Churchill Free Download:
              Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups