More Collision (XNA)
-
I have this code so far on checking collision. It doesn't quite work.
if (Velocity.Y > 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X + Sprite.Width > grassTopBlock[n].Position.X && Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.Y = grassTopBlock[n].Position.Y - Sprite.Height; } } } } /// FIX ME! ( i wrote his for myself before I got off last night) if (Velocity.X < 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width && Position.X + Sprite.Width > grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.X = grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width; } } } }
What the problem is, I can never tell if the second statement acts, because every time I try, the sprite moves to the top of the grassTopBlock. Can anyone post (or help?) with some collision algorithm for 2D sprites? thanks- I love D-flat! - Need. More. Code.
-
I have this code so far on checking collision. It doesn't quite work.
if (Velocity.Y > 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X + Sprite.Width > grassTopBlock[n].Position.X && Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.Y = grassTopBlock[n].Position.Y - Sprite.Height; } } } } /// FIX ME! ( i wrote his for myself before I got off last night) if (Velocity.X < 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width && Position.X + Sprite.Width > grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.X = grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width; } } } }
What the problem is, I can never tell if the second statement acts, because every time I try, the sprite moves to the top of the grassTopBlock. Can anyone post (or help?) with some collision algorithm for 2D sprites? thanks- I love D-flat! - Need. More. Code.
So, comment out the code that changes the y position and see if the x position code works alone.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
So, comment out the code that changes the y position and see if the x position code works alone.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
OK: never mind; I figured it out, fianlly. Thanks for helping, though. The problem was that the Y value kept checking after a certain limit was supposed to be. I added 1 line and it was fixed.
- I love D-flat! - Need. More. Code.
modified on Monday, March 17, 2008 7:07 PM