Advertisement

How can I know when the mouse hits a circle?

Started by July 04, 2000 12:07 PM
4 comments, last by BenB 24 years, 5 months ago
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
The Rock
Advertisement
I wrote a tutorial about that (and other collision detection stuff).

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

But sqrt is extremly slow, you can do the same thing w/o it...

    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
JonatanHedborg is right, thats the fastest way without using a sqrt at run-time

=======================================
A man with no head is still a man.
A head with no man is plain freaky.

This topic is closed to new replies.

Advertisement