Emily A.
Emily A.

Reputation: 31

Within a circles radius in processing

Is there a way to detect if a co-ordinate is within the radius of a circle in processing?

For example, in my program, if player 2's sprite overlaps and "captures" player 1's sprite, then I want the game to end (so if player1's sprite is within the hitbox circle of player 2). *Note: my player 1 sprite is rather small, and the co-ordinate defining its position should be enough for this overlap detection

Thanks!

Upvotes: 2

Views: 591

Answers (2)

Psi
Psi

Reputation: 6783

Although I am not familiar with processing, it is a mathematical problem and could be solved using pythagoras:

float cx; //center x of circle
float cy; //center y of circle
float cr; //radius of circle

float x; //tested x coordinate
float y; //tested y coordinate

(sqrt(pow(x-cx, 2) + pow(y-cy, 2)) < cr) // must evaluate to true for a hit-test

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42176

You can just use the dist() function.

Get the distance between the point and the center of the circle. If that distance is less than the radius of the circle, then the point is inside the circle.

I'd recommend drawing out some examples to see why this makes sense.

More info is available in the reference.

Upvotes: 2

Related Questions