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

Author Topic: Mouse click in circle  (Read 4803 times)

0 Members and 1 Guest are viewing this topic.

led

  • Newbie
  • *
  • Posts: 28
    • View Profile
Mouse click in circle
« on: January 12, 2010, 12:20:15 am »
Hey,
I'm trying to click inside a image that has a circle in it (its centered) with the mouse so i tried doing a circle using the sf::Shape and checking if the X,Y of the mouse is inside the X,Y of the circle but ofcourse it doesnt work (silly idea). How can I know if the Mouse is been clicked inside the circle?
while (!idle)
{
   DoSomething();
}

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Mouse click in circle
« Reply #1 on: January 12, 2010, 12:39:06 am »
Create a vector that contains the difference of your mouse click and the object location and check if the length of this vector is lower than your radius.
Listen to my band: pencilcase.bandcamp.com

led

  • Newbie
  • *
  • Posts: 28
    • View Profile
Mouse click in circle
« Reply #2 on: January 12, 2010, 11:45:04 am »
How am I able to do that? X1-X2 and Y1-Y2 <=24?

EDIT: Got it working following this example http://www.bigresource.com/Tracker/Track-vb-AjRXbampf6/
Code: [Select]

int dx= Window.GetInput().GetMouseX() - sprite1.GetPosition().x;
int dy= Window.GetInput().GetMouseY() - sprite1.GetPosition().y;
if (dx < 0) { dx = dx - (dx * 2); }
if (dy < 0) { dy = dy - (dy * 2); }
float d1= dx * dx;
float d2= dy * dy;
float d3= d1+d2;
float d = sqrt(d3);

if (d <= 24)
sprite1.SetColor(Color(0,0,0,0));

Thanks anyway :wink:
while (!idle)
{
   DoSomething();
}

Dravere

  • Newbie
  • *
  • Posts: 37
    • View Profile
Mouse click in circle
« Reply #3 on: January 12, 2010, 03:09:16 pm »
What the? The following lines are just nonsense:
Code: [Select]

if (dx < 0) { dx = dx - (dx * 2); }
if (dy < 0) { dy = dy - (dy * 2); }

1. You don't need them. If a value is negative, then the value multiplied with itself will be positive anyway.
2. It would have been easier to write: dx = -dx;

Also as an recommendation:
Don't take the square root. Instead compare against the squared radius.

And perhaps you should read this:
http://en.wikipedia.org/wiki/Euclidean_vector

Dravere

 

anything