SFML community forums

Help => Graphics => Topic started by: Mr_Blame on February 24, 2015, 06:46:43 pm

Title: collisions with mouse
Post by: Mr_Blame on February 24, 2015, 06:46:43 pm
Hellow everyone !  :) I have some problems with sfml  :'( the thing is that  i am checking if mouse is over the sprite and everything seems fine(first time a made this)

bool isMouseOverObjectBox(sf::Sprite &object, sf::Texture &tex, sf::RenderWindow &target){
               
                sf::Vector2i Mpos(sf::Mouse::getPosition(target));
                sf::Vector2f pos(object.getPosition());
                sf::Vector2u size(tex.getSize());
                if(Mpos.y >= pos.y && Mpos.x >= pos.x && Mpos.y <= pos.y+size.y && Mpos.x <= pos.x+size.x){
                return true;
                }
                return false;
        }


but this isn't all i want. i want to make it work with rotated, scaled sprite and unfortinatly i don't know how to do it can anyone help me?
Title: Re: collisions with mouse
Post by: 2ant on February 24, 2015, 08:07:30 pm
I think i found what you need :

http://www.sfml-dev.org/tutorials/2.2/graphics-transform.php

In the bouding box section there is this :

// get the bounding box of the entity
sf::FloatRect boundingBox = entity.getGlobalBounds();

// check collision with a point
sf::Vector2f point = ...;
if (boundingBox.contains(point))
{
    // collision!
}

Make the "point" contain your mouse coordinates and i will work ( i think :) )
Title: Re: collisions with mouse
Post by: Laurent on February 24, 2015, 08:30:49 pm
This will not be accurate in case of a rotation, because the global bounds is an axis-aligned rectangle (see http://www.sfml-dev.org/tutorials/2.2/graphics-transform.php#bounding-boxes).

Google should help you for "point in rotated rectangle" algorithms.
Title: Re: collisions with mouse
Post by: Mr_Blame on February 25, 2015, 07:45:42 am
Guys I found a way with box2D library!