How can I know when the mouse hits a circle?
Hello!
I''m making a game using direct input, and everything is going well.
I have the variables containing the mouse x & y, so how can I check, with a simple function, if the mouse''s x & y are in a circle?
if the distance between the 2 points
(mouse point and mid point of circle)
is smaller than the radius then
the point is in the circle
(mouse point and mid point of circle)
is smaller than the radius then
the point is in the circle
The Rock
I wrote a tutorial about that (and other collision detection stuff).
Go to:
http://workspot.net/~kondor/tutors/dos/colldet.html
Go to:
http://workspot.net/~kondor/tutors/dos/colldet.html
This is basically as was said above but spelled out a little more plainly...
If your mouse position is stored in ''mousex'' and ''mousey'', and the circle is described by ''circlex'' and ''circley'' (co-ordinates of the centre of the circle), and the radius is ''circle_rad''...
You have to work out the distance from the (mousex,mousey) to (circlex,circley):
dx = mousex - circlex
dy = mousey - circley
distance = sqrt(dx * dx + dy * dy)
Then, a check to see if that distance is equal to or smaller than the radius of the circle:
if(distance <= circle_rad)
collision = true
If your mouse position is stored in ''mousex'' and ''mousey'', and the circle is described by ''circlex'' and ''circley'' (co-ordinates of the centre of the circle), and the radius is ''circle_rad''...
You have to work out the distance from the (mousex,mousey) to (circlex,circley):
dx = mousex - circlex
dy = mousey - circley
distance = sqrt(dx * dx + dy * dy)
Then, a check to see if that distance is equal to or smaller than the radius of the circle:
if(distance <= circle_rad)
collision = true
But sqrt is extremly slow, you can do the same thing w/o it...
I think this is faster, im not sure though...
========================
Game project(s):
www.fiend.cjb.net
dx = mousex - circlex;dy = mousey - circley;distance=(dx*dx)+(dy*dy);if(distance <= (circle_rad*circle_rad)) collision = true;
I think this is faster, im not sure though...
========================
Game project(s):
www.fiend.cjb.net
=======================Game project(s):www.fiend.cjb.net
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement