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

Author Topic: collisions with mouse  (Read 3626 times)

0 Members and 1 Guest are viewing this topic.

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
collisions with mouse
« 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?
« Last Edit: February 24, 2015, 06:55:49 pm by Mr_Blame »

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: collisions with mouse
« Reply #1 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 :) )

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: collisions with mouse
« Reply #2 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.
Laurent Gomila - SFML developer

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: collisions with mouse
« Reply #3 on: February 25, 2015, 07:45:42 am »
Guys I found a way with box2D library!

 

anything