Hi.I recently learned about collision detection between circles. However, How can I detect a collision between the edge of the circle and a circle inside that circle? In my case I am using 2 ellipses to create a circular track. However, I want to know when a ellipse between those circles collides with one of the edges. Here is what my situation looks like:
(Note the ball can be dragged with your finger)
-- Use this function to perform your initial setup
lastx = 0
lasty = 0
initx = WIDTH/2+3
inity = 690
ellipsepositionx = 0
ellipsepositiony = 0
-- This function gets called once every frame
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
lastx = CurrentTouch.x
lasty = CurrentTouch.y
ellipsepositionx = CurrentTouch.x
ellipsepositiony = CurrentTouch.y
ellipsepositionx = initx
ellipsepositiony = inity
ellipsepositionx = lastx
ellipsepositiony = lasty
if(start==0 and CurrentTouch.x>initx-20 and CurrentTouch.x<initx+20 and CurrentTouch.y<inity+20 and CurrentTouch.y>inity-20)then
if(CurrentTouch.state==ENDED and start==1)then
fill(0, 196, 255, 255)
-- Do your drawing here
fill(30, 255, 0, 255)
Touch between the 2 circles and drag the smaller circle. If you touch the inside or outside circle a message will appear in the center of the screen. You don’t need to use physics for this.
background(40, 40, 50)
if small or large then
if (x-xc)^2/a^2+(y-yc)^2/a^2 <= 1 then
if (x-xc)^2/a^2+(y-yc)^2/a^2 >= 1 then
@dave1707 - it doesn’t matter for this simple example, but power functions can really hurt your speed when you’re using them so many time a second. Also, it is neater to use a single function when the two of them are so similar. So how about this (untested)?
-- in draw....
small = IsInside(x,y,w,h,(WIDTH-240)/2)
large = not IsInside(x,y,w,h,(WIDTH-60)/2)
--using this function
local xx,yy=x-xc, y-yc
return (xx*xx+yy*yy) / (a*a) <=1
@Ignatz I agree with you about the power functions and you’re right that a simple program like this won’t be affected that much. I was also thinking about combining the 2 functions, but I was just pulling code from existing programs I have and didn’t feel like changing anything.
Thank you for responding. I will test this code.
This works great! Physics would slow down the game anyways. Thanks!
@austinmccoy - something you should look at is using the vec2 functions (they are just pairs of numbers used to store x,y positions). There are several special functions for them, such as distance.
So you can test for collisions by measuring the distance between the centre and the centre of the moving ellipse, and if it is less than (the radius of the inner circle plus its own radius) or greater than (the radius of the outer circle less its own radius), then you have collided.
I know this is pretty much what you are doing, but here is how it looks with vec2’s (untested). You can see the rest of the special vec2 functions in the vectors section of the Codea reference. The advantage is partly simplicity, also they are faster and more efficient than writing your own code.
--using dave's code
--now the test
vecCentre=vec2(w,h) --put this in setup if it's not going to change
local d = vecCentre:dist(vec2(x,y)) --calculate distance between ellipse centres
if d < (WIDTH-240)/2 or d > (WIDTH-60)/2) then
I’ve been busy lately and just got around to reading your post, but this looks interesting! I’ll definitely test out the use of vec2 functions to determine collisions between objects.