SFML community forums

Bindings - other languages => DotNet => Topic started by: zombiekiller222 on May 24, 2012, 10:24:21 am

Title: Why does my simple collision test with the screen not work?
Post by: zombiekiller222 on May 24, 2012, 10:24:21 am
                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).
Title: Re: Why does my simple collision test with the screen not work?
Post by: eXpl0it3r 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. ;)
Title: Re: Why does my simple collision test with the screen not work?
Post by: zombiekiller222 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?