SFML community forums

Help => General => Topic started by: muhdsalm on February 17, 2022, 03:04:29 pm

Title: Problem with intersections
Post by: muhdsalm on February 17, 2022, 03:04:29 pm
Hi.
I'm using SFML.Net, the c# bindings for sfml, and I'm getting a weird logic error.

Here's my code:
public static bool IsColliding(Entity enity1, Entity entity2)
        {
            if (enity1.GetSprite().GetGlobalBounds().Intersects(entity2.GetSprite().GetGlobalBounds()))
            {
                Console.WriteLine(enity1.GetSprite().GetGlobalBounds().Intersects(entity2.GetSprite().GetGlobalBounds()));
                Console.WriteLine(enity1.GetSprite().GetGlobalBounds().Left + ", " + entity2.GetSprite().GetGlobalBounds().Left);
                Console.WriteLine(enity1.GetSprite().GetGlobalBounds().Top + ", " + entity2.GetSprite().GetGlobalBounds().Top);
                return true;
            }
           
            return false;
           
        }
(how do I format code?)

As you can see, it's a simple function, which returns true if the sprites collide, and false if they don't. I also added some writeline() statements for debug.

Weirdly, this is the output I get:

True
342, 260
-40, 350

How? How is it returning true if the rectangles are obviously not intersecting?

(Also, this is my first time at the forum, so forgive me if this is not a correct type of problem in the correct type of section, etc.
Title: Re: Problem with intersections
Post by: eXpl0it3r on February 17, 2022, 04:31:57 pm
You need to check the full rect, just checking top left values doesn't specify how large the sprites actually are.

var bounds1 = enity1.GetSprite().GetGlobalBounds();
Consoel.WriteLine($"{bounds1.Left}, {bounds1.Top}, {bounds1.Width}, {bounds1.Height}");

var bounds2 = enity2.GetSprite().GetGlobalBounds();
Consoel.WriteLine($"{bounds2.Left}, {bounds2.Top}, {bounds2.Width}, {bounds2.Height}");

This should give you a clearer understanding.
Of course you can also always use your debugger to see the values returned.

To highlight code you can use [code=csharp]...[/code]