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

Author Topic: Why does my simple collision test with the screen not work?  (Read 2861 times)

0 Members and 1 Guest are viewing this topic.

zombiekiller222

  • Guest
                public virtual bool IsOnScreen()
                {
                        FloatRect rect1 = new FloatRect(Sprite.Position.X - Sprite.Origin.X, Sprite.Position.Y - Sprite.Origin.Y, Sprite.Width, Sprite.Height);
                        FloatRect rect2 = new FloatRect(Program.Camera.Center.X - Program.Camera.Viewport.Width / 2, Program.Camera.Center.Y - Program.Camera.Viewport.Height / 2, Program.Camera.Viewport.Width, Program.Camera.Viewport.Height);
                       
                        if (rect1.Intersects (rect2))
                        {
                                return true;
                        }
                       
                        else
                        {
                                return false;
                        }
                       
                }

I get that this is not the best or simplest way to do it, I've tried
return Sprite.SubRect.Intersects(Program.Camera.Viewport);
Yet that does not work (even if I convert the Sprite IntRect to a FloatRect).
« Last Edit: May 24, 2012, 10:27:43 am by zombiekiller222 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Why does my simple collision test with the screen not work?
« Reply #1 on: May 24, 2012, 12:53:47 pm »
I'm not exactly sure what your Program.Camera is.
You just need to make sure that both rectangles have absolute coordinates.

Keep in mind that the SubRect() of a sprite has nothing to do with it's position. It defines the show rectangle on the sf::Image which it holds. So you could make animations by first showing only a part of a image and then just move the subrect on the image to get another result.

Also I'd advise you to switch to SFML 2rc. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zombiekiller222

  • Guest
Re: Why does my simple collision test with the screen not work?
« Reply #2 on: May 24, 2012, 01:23:23 pm »
Switching to the RC now (how could you tell I wasn't using it?). Anyhow, Program is the main class, and Camera is a global variabe pointing to a Camera, basically my extension of a View.

EDIT: Wow, using this code I get 0 FPS.
return Program.Camera.Viewport.Intersects (Sprite.GetGlobalBounds ());
What is wrong?
« Last Edit: May 24, 2012, 01:43:54 pm by zombiekiller222 »