How to stop a ball from going outside a circle?

Hello,

I am trying to stop a ball from going outside a circle inside a canvas.

Blocks :

My problem is that the origin of the canvas is at the top left and the test of the if statement only works if the origin of the canvas is at the middle.

Is there a way to change the origin point of a canvas ?

If not, maybe there is a way to change the equation of the if statement ?

This is how the app looks like :

Thanks

1 Like

You need three global variables:

  • diskRadius (in pixels, you chose 247 but it might be a proportion (half?) of min(Canvas1.Height,Canvas1.Width) if you switch Screen1 to responsive)
  • centerDiskX (in pixels, the x value of the center of the big disk)
  • centerDiskY (in pixels, the y value of the center of the big disk)

The equation for being in the circle then becomes
if sqrt((currentX - global centerDiskX)^2 + (currentY - global centerDiskY)^2)) < global diskRadius

If this runs slow, you can speed it up by squaring both sides of the inequality to get the equivalent but faster check:
if (currentX - global centerDiskX)^2 + (currentY - global centerDiskY)^2 < global diskRadiusSquared
where diskRadiusSquared is initialized as the square of the big disk radius, a constant.

2 Likes

Thank you for you answer !

This works perfectly.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.