Function to detect a point in a circle?

BlitzMax Forums/BlitzMax Beginners Area/Function to detect a point in a circle?

pappavis(Posted 2006) [#1]
i know it should be dead simple but can any1 tell me:
1. How can i detect if a object is inside a circle?
2. How can i move (shoot) a missile starting at XY with its destination to another (known) XY.

For example, a enemy object detects my playership, and shoots a bullet to the XY-position where my playership was detected.

Any1 have some example code?? TIA!


sswift(Posted 2006) [#2]
Calculate the distance of the point from the center of the circle.

Distance# = Sqr((Px#-Cx#)^2 + (Py#-Cy#)^2)
If Distance# < Radius# Then InCircle()

Radius# is half the width of the circle.

If on the other hand you want to know if two circles collide, then do this:

Calculate distance between circle centers:

Distance# = Sqr((C2x#-C1x#)^2 + (C2y#-C1y#)^2)
If Distance# < (Radius1#+Radius2#) Then Colliding()


FlameDuck(Posted 2006) [#3]
Or to turn Mr. Swifts example into (optimized) sourceocde:
Function cCollide:Int( pointX:Float , pointY:Float , circleX:Float , circleY:Float , radius:Float )
	radius:* radius
	Return radius < (pointX - circleX) * (pointX - circleX) + (pointY - circleY) * (pointY - circleY)
EndFunction



SSS(Posted 2006) [#4]
As per your second question, lets say you have vx and vy as the velocities of the rocket in the x and y directions. Then you have speed as the speed at which the rocket will travel. You can use the following code.

(x1,y1 and x2,y2 are the start/end points)
local dx = x1-x2
local dy = y1-y2
local mag# = sqr(dx^2+dy^2)
vx = dx*speed/mag
vy = dy*speed/mag



Shagwana(Posted 2006) [#5]
I did some work on this sort of thing in bb3d, there are faster ways to calculate point to circles. Check out the bb3d code here. Figure it might be of help to you.