I'm making a game in Java w/ slick and lwjgl and right now I want to do the following: draw a circle, that is drawn around an object with a radius of e.g. 75. I want the object to be IN THE MIDDLE of that circle. That is easy but I also want the circle to become smaller when the player enters it. Basically this is an example:

Players x is 100

The OBJECT'S x is 300

The circle's x is (300-75 == 225),(object's x - radius) and its width is radius*2, so 150

When the player is inside of the circle (players x > circles x; e.g. 230 > 225) then I want the RADIUS of the circle to shrink to 70 (75 - (230 - 225), or, radius - (palyers x - circles x)), and after the circle shrank it should still be perfectly around the object from before. (AND BTW I didn't mention the y variable because it's self-explanatory after I figure out the x) Can anyone explain me the maths behind it please. Thank you

  • \$\begingroup\$ I don't understand. Does the circle have a fixed size until the player enters it? Does the circle shrink proportionally with the player's distance from the middle? What happens if the player gets to the middle—does it disappear? A picture would be helpful. \$\endgroup\$ – Anko Nov 7 '15 at 21:28
  • \$\begingroup\$ Yes, the circle stays the same unti the player enters it, and then it shrinks proportionally by how far the player has enterd it. When the player is at the middle it should just be very small. I am using this circle to see if the player is close to a 'spike' and the closer he is to it the faster your health will drop. \$\endgroup\$ – Ivan Jerza Nov 8 '15 at 8:42

Say, your circle is at 100, 120 and has radius 25.

Say, your object is at x, y and has radius 9.

So, the distance between the centers of the two is:

sqrt((100 - x)^2 + (120 - y)^2)

which means that the distance between their boundaries is

d = sqrt((100 - x)^2 + (120 - y)^2) - 25 - 9

Now, if this distance is less than zero, we know that objects are intersecting and we know that we should start shrinking the circle (or by how much you need to decrease your player's health). By how much you shrink the circle is your choice, but you obviously make it a function of d. Hope this was somewhat useful.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.