![]() |
|
Containment detection - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: Containment detection (/thread-7275.html) |
Containment detection - JeremyL - Feb 26, 2003 09:54 AM I want my mouseDragged method to continuously drag a circle, while the circle remains within the bounds of a larger circe. When the smaller circle hits the circumference of the containing circle (i.e., the mouse moves outside the containing circle), the smaller circle can only be dragged around the containing circle's circumference. Drag and drop methods are implemented in an NSView subclass. Circle properties are stored in a custom object currChar. currChar's draw method draws both dragging circle and containing circle. Circles are drawn as NSBezierPath ovals within rects. Problem is, I don't have enough knowledge of math to implement the containment algorithm. Can anyone help? Code: // center of containing circle is set in mouseDown methodCode: // center of circle to dragContainment detection - w_reade - Feb 26, 2003 11:06 AM The test you need is: Is the distance between the centers of the circles greater than the difference of their radii? If not, the small circle is inside the big one. Otherwise, you'll need to move the small circle back inside. Here's a vaguely relevant snippet, but bear in mind that it doesn't do exactly the same thing as you want to: [SOURCECODE] - (void)constrainMouseToCircleWithRadius: (float)radius centredOnX: (float)x Y: (float)y { mouseX -= x; mouseY += y; // Different co-ordinate ststems. float distSquared = mouseX*mouseX + mouseY*mouseY; float radSquared = radius*radius; if (distSquared > radSquared) { float scale = sqrt(radSquared / distSquared); mouseX *= scale; mouseY *= scale; } mouseX += x; mouseY -= y; } [/SOURCECODE] Ask again if I've been unclear. |