Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Moving circle - line intersection?

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Jan 2016 06:19 Edited at: 6th Jan 2016 06:40
Something I started researching years ago, but looking at my old code I didn't quite get it yet. So I'm tackling it again.

If a circle moves from A to B, and what time does it intersect a line segment CD? I can't just do a simple line-line intersection test, because the tangent would've already made contact. Hopefully this illustration explains it better. I feel I've come close a few times, but it always falls apart.



"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Attachments

Login to view attachments
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 6th Jan 2016 08:04
I'm sure there's a method that returns the 0.0-1.0 percentage of where the line intersects - maybe that could be adjusted for the circle radius, will have to look through some old code snippets to find it.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 6th Jan 2016 10:16 Edited at: 6th Jan 2016 10:21


you have three points that form a triangle. you know;
1) the length of one side (the radius)
2) one corner point (x,y where the lines intersect) and it's angle
3) this is just an eyeball guess but i think the angle between the center of the circle and where it meets the line is a right angle (not 100% sure though)

i think that's enough info to complete. i didn't pay much attention in algebra so i don't know the equation but i think there's enough there to flesh out the triangle

http://mathworld.wolfram.com/Circle-LineIntersection.html
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 6th Jan 2016 11:37
Yes, if we are just testing for a collision then we can work that out using pythagoras or trig (once centre of circle distance from line = radius, collision detected) ... however if you want a function that returns the time at which the circle collides with the line given the start position, line vertices, velocity etc. then it becomes a little trickier.
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Jan 2016 13:05
if you calculate in time steps, the time step from a to b is 100%, from a to intersect its ?%
so if the intersect from a to b is in the middle its half time step. means percentage calculation.
AGK (Steam) V2.0.16 : Windows 10 Pro 64 Bit : AMD (15.30.1025) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Jan 2016 20:18 Edited at: 6th Jan 2016 20:19
Quote: "I'm sure there's a method that returns the 0.0-1.0 percentage of where the line intersects "

That's the easy part.

Quote: "this is just an eyeball guess but i think the angle between the center of the circle and where it meets the line is a right angle"

Correct, the normal of the line to the circle would make the shortest distance between the two.

I think I just needed some sleep. Dot product kept yelling in my head last night but I wasn't sure how/where to use it. I think I got it now.


When the circle hits the line, it forms a right triangle, one leg being a normal from the line and equal to the radius of the circle. Create two vectors, one being the direction of travel for the circle and the other the line normal, call these A and B. Take the dot product of the two vectors and divide by the product of the vector lengths to find the angle between them.

Now we know two parts of the triangle that's formed in this problem. The angle and the length of the adjacent leg (radius). Using good old SOHCAHTOA, you can then calculate the length of the hypotenuse. The equation can be simplified so we don't even need to use cosine. Now, starting at the intersection point of the two lines, traverse back along the direction vector of the circle by a distance equal to the hypotenuse. This new point is where the circle will hit the line along it's path.
To find the actual point of contact, take this new point and subtract (or add, depending on direction of movement and normal) the line normal times the radius of the circle. You now have all points along the line you need to then easily calculate a time index of intersection, if you still need it at this point.





Here's some dirty code for an example. (There's plenty of stuff in it that's not even used)

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Attachments

Login to view attachments
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 6th Jan 2016 20:39
Have you tried simplifying the problem?
Make another invisible line parallel to the current line. Make it the same distance away as the radius of the circle.
Now you can trace the centre of the circle relative to the invisible line.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Jan 2016 20:41
That's a pretty good idea!

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 6th Jan 2016 22:29
College flashbacks! WWWaaaaaaargh!!!!
make -C ../NagGaCreMo/2016 -f aGame.mk
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Jan 2016 06:31
Using Batvink's idea makes much less code! You got a simple solution for check if the circle collides with an endpoint? That'll be the next thing I need to figure out.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-11-25 13:19:09
Your offset time is: 2024-11-25 13:19:09