# Odd Behavior with Simple Collision Detection Between Moving Objects, Rectangle Method

Solution Update

After tinkering around a bit, I noticed that my bounding boxes were not updating in accordance with the movement of their respective objects, so it struck me that I should put the collision Rectangle construct into the same part of the code that handled movement, to ensure that this was happening. After cleaning up a geometry issue with the PC sprite width, the system now works properly. In short, it seemed to be a problem with making sure that the bounding boxes properly updated accordingly with the movement code within the sprite classes,

(Second Edit)

TLDR version of the question: why is the Intersects method (with rectangles) working in the case of a collision between a moving and stationary object, but failing to do so in the case of collisions between moving objects?

(Beginning of Original Post)

For background, I'm more or less a novice with C# and XNA, along with programming more generally, and happen to be taking a class with particular assignment requirements, such as making a basic collision detection system.

To the problem: after perusing various articles, checking tutorials, copy-pasting and reformatting code, etc., I have found that I have been able to get a basic intersecting rectangle collision detection system between a stationary object and a moving object working. However, in spite of apparently valid logic, and code that seems to make sense, my project simply refuses to detect collisions between the bounding boxes of any of the moving objects: it fails either between the player object and any animated sprite object, or between the two instances of the animated sprite objects that I have. This is being detected from both the visual evidence and the lack of any output messages that I have written in for testing. I would think that there's something wrong with the boxes, but everything collides properly with the stationary object, so it would seem that the boxes do exist.

I am suspicious that there's some intricacy regarding geometric update cycles that is resulting in the inability of the moving objects' boxes to detect collisions with one another, or perhaps the vector system, as the boxes do clearly exist, but have found specific answers to be elusive.

I am happy to provide sample code but I do not know what could be helpful at present.

Any and all suggestions are appreciated.

P.S. - I should add that, interestingly, one animated sprite is able to "push" the other one through the stationary object upon collision with it, in spite of there being no code to do so. The animated objects are all derived from their own respective classes, if that has any bearing on the problem: there are a handful of pertinent public values in the two classes which the game class calls upon to attempt collision detection and repositioning, as well.

(end of original post)

(Second Edit)

At request, here are some snippets of the pertinent code.

The basic collision checking method I am using is Intersects with bounding boxes, presently written in the Game class's standard Update section.

            if (PCSprite.PCBounds.Intersects(TreeBounds) && PCSprite.spriteSpeed < 0)
{
PCSprite.Position = new Vector2(PCSprite.PCposition.X - PCSprite.spriteSpeedN, PCSprite.PCposition.Y - PCSprite.spriteSpeedN);
Console.WriteLine("Negative");
}


The player sprite's position is determined by keyboard-controlled movement in accordance to two given speed values, from which a Rectangle that fits the sprite frame is drawn (32x48 pixels). This particular part of the code works fine, as running into the stationary "Tree" object produces both the proper reaction and the text message in the output window when moving in a negative direction.

However, the Intersects check procedure fails to produce any result when it is performed with an animated sprite against the player sprite, or vice versa, which for now is exactly the same size as that of the player (it has essentially the same code in a different class, without the player input component). The animated sprite is simply traveling across the screen in one direction, to the right.

            if (ASprite1.ASBounds.Intersects(PCSprite.PCBounds))
{
Console.WriteLine("AS1-Player Collision");
ASprite1.Position = new Vector2(ASprite1.Position.X - ASprite1.spriteSpeed, ASprite1.position.Y);
}


Both the animated sprites and player sprite register collisions with the tree object, but not each other. As an experiment, I also added code to check for collisions between the animated sprites and set them to do just that, but they do not register a collision either.

As clearly the bounding boxes exist and work to detect collisions with the stationary object, I am at a bit of a loss as to why there are no registered collisions between the mobile objects. The mobile objects are derived from their own classes, while the stationary (tree) object is simply coded into the game class.

• If you've solved your problem, post your solution as an Answer folks can upvote, not embedded in the question itself. – DMGregory Jul 25 at 4:32