I need help figuring out how to make it so the ball is good when you tap the screen and it is in the circle, but when you tap the screen and it is a little out of the circle then you lose.
I have provided a sketch:
I need help figuring out how to make it so the ball is good when you tap the screen and it is in the circle, but when you tap the screen and it is a little out of the circle then you lose.
I have provided a sketch:
You want to check the line connecting the centers of the two circles.
When they’re just touching, the length, math.sqrt((center1.x - center2.x)^2 + (center1.y - center2.y)^2), will be the sum of radius #1 and radius #2.
If the distance is greater than that sum, they’re still separated, as in your top image.
Otherwise, it’s one of the bottom two cases, and I think you’d want to proceed by rejecting the third case where you can. I assume the ball will be smaller than the circle? Then the ball would just touch the circle from the inside when that length above, plus its radius, equaled the radius of the circle. So you’d check whether it’s less than (or equal?) to that. Does that make sense?
So something like:
local BallRadius, CircleRadius = ... -- whatever function CheckBall () local dist = math.sqrt((ball.x - circle.x)^2 + (ball.y - circle.y)^2) if dist \<= BallRadius + CircleRadius then -- touching or inside if dist + BallRadius \< CircleRadius then -- or \<= if boundary is okay too return "win" else return "lose" end end return "other" -- top image end
Sorry, didn’t see this until today. (And probably won’t see any response until tomorrow, at best.)
It would be better if I met you halfway. Get something basic up and running, with a ball and circle moving the way you’d like. If you’re still having troubles with the rest, post the code and I can probably suggest how to finish it.
Don’t worry too much about following my CheckBall() function. I wrote that as if you’d be checking for a return value, thus the “other” case, but of course you could fire off your win or lose logic directly. Also, if your objects are actual circles, you could get those radius values as say ball.path.radius directly, too, rather than have them declared earlier.
Alright. Well, we can always talk in private and I can show you what I have so far.
You want to check the line connecting the centers of the two circles.
When they’re just touching, the length, math.sqrt((center1.x - center2.x)^2 + (center1.y - center2.y)^2), will be the sum of radius #1 and radius #2.
If the distance is greater than that sum, they’re still separated, as in your top image.
Otherwise, it’s one of the bottom two cases, and I think you’d want to proceed by rejecting the third case where you can. I assume the ball will be smaller than the circle? Then the ball would just touch the circle from the inside when that length above, plus its radius, equaled the radius of the circle. So you’d check whether it’s less than (or equal?) to that. Does that make sense?
So something like:
local BallRadius, CircleRadius = ... -- whatever function CheckBall () local dist = math.sqrt((ball.x - circle.x)^2 + (ball.y - circle.y)^2) if dist \<= BallRadius + CircleRadius then -- touching or inside if dist + BallRadius \< CircleRadius then -- or \<= if boundary is okay too return "win" else return "lose" end end return "other" -- top image end
Sorry, didn’t see this until today. (And probably won’t see any response until tomorrow, at best.)
It would be better if I met you halfway. Get something basic up and running, with a ball and circle moving the way you’d like. If you’re still having troubles with the rest, post the code and I can probably suggest how to finish it.
Don’t worry too much about following my CheckBall() function. I wrote that as if you’d be checking for a return value, thus the “other” case, but of course you could fire off your win or lose logic directly. Also, if your objects are actual circles, you could get those radius values as say ball.path.radius directly, too, rather than have them declared earlier.
Alright. Well, we can always talk in private and I can show you what I have so far.