Advertisement

Need Explanation With Distance Between Two Segments

Started by August 01, 2016 07:03 AM
11 comments, last by BBeck 8 years, 6 months ago

The code below is from book Real-Time Collection Detection ch5.cpp file

Some of them I don't know where it come from.

float denom = a*e-b*b;

and

(b*f - c*e) / denom

what does this mean?I know a is square length of d1,e is square length of d2,b is d1 projection on d2,

but what does a*e-b*b means?where this guy come from,for what reason?any rule or fomular related??

All of this is explained in section 5.1.9 of my book. The code uses the notation that the text uses, and the text contains both explanations and derivations of the math, that is then expressed in code. The expression (b*f - c*e) / denom is solving an equation, to determine where on the segments the closest points lie. In order to understand it, you need to invest time into understanding the math behind it (said math also being explained in the book).

I have no idea. I consider half that book "black magic". I consider it a major accomplishment any time I understand any of it. To say that it is "math intensive" is an understatement. He specifically says right before that code "Alternatively, Gaussian elimination can be used on the 2 x 2 system of linear equations to solve for the one unknown in terms of the other." Yep, over my head.

The Chapter 3 math primer is there to help provide some math background, but is fundamentally not a replacement for the study of a proper elementary linear algebra (ELA) textbook. I described Cramer's rule for solving systems of linear equations because it is -- relatively -- very easy to describe and understand. The Gaussian elimination algorithm I deemed would take too much space and time to go through, and people would be better off looking it up online or consulting an ELA textbook. It's really not that hard though, I recommend looking it up!

I don't recommend Christer's formula: (b*f - c*e) / denom

In practice I have recieved negative squared distance from very small line segments, which resulted in NaNs being passed around if the result is given to a sqrt function. I wrote an article about the details here: http://www.randygaul.net/2014/07/23/distance-point-to-line-segment/. Usually drawings of vectors and deriving equations piece by piece helps to make sense of formulas.

I caveat all of this on pages 5-6. E.g. "To turn the presented code into real production code, some additions may be necessary. For example, tests for division by zero are not always performed to avoid going into details that would hamper the understanding of the overall approach. Similarly, some code tests may require tolerance values to be added for full robustness."

The code samples aren't intended to be used as-is, but as a starting point for someone to write their own version, that matches their own needs.

Christer's code had no such safety mechanism or even an explanation of potential problems. Edit: Which is unfortunate since all the rest of his code I've ever used seems totally robust.

Again, see pages 5-6 that clarify this. Notably the reference to the reader to apply the practices of Chapter 11 to the code samples, if they want production-ready code.

I caveat all of this on pages 5-6. E.g. "To turn the presented code into real production code, some additions may be necessary. For example, tests for division by zero are not always performed to avoid going into details that would hamper the understanding of the overall approach. Similarly, some code tests may require tolerance values to be added for full robustness."

The code samples aren't intended to be used as-is, but as a starting point for someone to write their own version, that matches their own needs.


Oh I see, thank you for pointing this out Christer! A small portion of existential pain has been lifted from my personal daily life :) Unfortunately the code bases I've worked in tend to do have pieces of your code directly copy + pasted, so I appreciate you coming here to clarify.

Advertisement

I have no idea. I consider half that book "black magic". I consider it a major accomplishment any time I understand any of it. To say that it is "math intensive" is an understatement. He specifically says right before that code "Alternatively, Gaussian elimination can be used on the 2 x 2 system of linear equations to solve for the one unknown in terms of the other." Yep, over my head.

The Chapter 3 math primer is there to help provide some math background, but is fundamentally not a replacement for the study of a proper elementary linear algebra (ELA) textbook. I described Cramer's rule for solving systems of linear equations because it is -- relatively -- very easy to describe and understand. The Gaussian elimination algorithm I deemed would take too much space and time to go through, and people would be better off looking it up online or consulting an ELA textbook. It's really not that hard though, I recommend looking it up


lol I did not realize you were on this forum.

Thanks for the book! I'm sure you don't need me to tell you that this book is a major contribution to the game programming community. I wouldn't give up my copy for the world. It's one of several of my most cherished game programming books even if I don't understand half of it. That's largely just because I haven't had time to really dig into the book and go through it, and I might need to buff up on my Linear Algebra as well. But what I do know is that I would not know where to find half these algorithms if not for this book.

Thanks for taking the time to write it!

This topic is closed to new replies.

Advertisement