• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Pipo DeClown

(Finite) Line-Circle Intersection

7 posts in this topic

I'm writing a small collision detection library, but the math is taking longer than I planned. I'm currently doing circle with line, triangle and rectangle collision, but the Circle-Line page on MathWorld doesn't make much sense to me (plus, it's for infinite lines which is more work for me). I wonder how to implement a function that allows for the center of the circle to be other than (0, 0) - I can't think of anything with the MW explanation. Code prefered, but theory will do also.
0

Share this post


Link to post
Share on other sites
bool clIntersect(Circle c,Line l)
{
float x0 = c.x;
float y0 = c.y;
float x1 = l.s.x;
float y1 = l.s.y;
float x2 = l.e.x;
float y2 = l.e.y;
float n = fabs((x2-x1)*(y1-y0)-(x1-x0)*(y2-y1));
float d = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
float dist = n/d;
if(dist > c.r)return false;
float d1 = sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
if((d1-c.r) > d)return false;
float d2 = sqrt((x0-x2)*(x0-x2)+(y0-y2)*(y0-y2));
if((d2-c.r) > d)return false;
return true;
}



[Edited by - mike25025 on March 3, 2005 5:49:28 PM]
0

Share this post


Link to post
Share on other sites
mike25025's code may be correct, but I'm a little confused as to what's happening in the last few lines. Anyway, if you're having trouble with it still, here's an alternate implementation to try:


bool IntersectCircleSegment(
const Vector2& c, // center
float r, // radius
const Vector2& p1, // segment start
const Vector2& p2) // segment end
{
Vector2 dir = p2 - p1;
Vector2 diff = c - p1;
float t = diff.Dot(dir) / dir.Dot(dir);
if (t < 0.0f)
t = 0.0f;
if (t > 1.0f)
t = 1.0f;
Vector2 closest = p1 + t * dir;
Vector2 d = c - closest;
float distsqr = d.Dot(d);
return distsqr <= r * r;
}




Theory: Find the parametric projection of the circle center onto the line segment. Use this to find the point on the segment closest to the circle center. Find the squared distance between the closest point and the circle center. Compare to radius squared.

If you want a point of intersection rather than a simple boolean result, different (but equally straightforward) code will be required.

Note: the above code is untested and may not be error-free.
0

Share this post


Link to post
Share on other sites
Quote:
I wonder how to implement a function that allows for the center of the circle to be other than (0, 0)


If that's the only holdup then you can just translate all your points before you jump into the algorithm. e.g. if a point on the line is at (Px, Py) and the circle is at (Cx, Cy) then use (Px - Cx, Py - Cy) and (0, 0) instead.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
Quote:
I wonder how to implement a function that allows for the center of the circle to be other than (0, 0)


If that's the only holdup then you can just translate all your points before you jump into the algorithm. e.g. if a point on the line is at (Px, Py) and the circle is at (Cx, Cy) then use (Px - Cx, Py - Cy) and (0, 0) instead.


Yes, that's what I tried to do. But I don't know where to substitude the 0's since they are left out (?) on the MathWorld page.

@Jyk: Thanks, I'm going to look at the theory!

@mike25025: I'm going to replace that code immediately, you'll hear from me!
0

Share this post


Link to post
Share on other sites
MMkay.. The collision works partly now. When the circle is on the line, all is okay, but when the circle is around the starting- and endingpoints, it gets stuck sometimes, while not even touching the line.

I'm using mike25025's code.

[update edit whatever]
YAEAAAAAAH!! I implemented jyk's code and it works like a super charm! I'll work out the theory later when I have time, currently I'm just so happy that the code works!

Thanks for everyone's attempt on helping, all++!

[Edited by - Pipo DeClown on March 4, 2005 3:02:58 AM]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0