Collision Help (XNA) : Please Help!
-
#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");
-
#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");
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.
-
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.
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.
-
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.
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.
-
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.
OK, thanks, Luc!
- I love D-flat! - Need. More. Code.
-
#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");
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.