Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with intersections  (Read 689 times)

0 Members and 1 Guest are viewing this topic.

muhdsalm

  • Newbie
  • *
  • Posts: 1
    • View Profile
Problem with intersections
« 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.
« Last Edit: February 17, 2022, 04:27:59 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10232
    • View Profile
    • development blog
    • Email
Re: Problem with intersections
« Reply #1 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]
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

 

anything