Raytracing problem - Nearest point on ray

Started by
15 comments, last by Wyrframe 20 years, 1 month ago
quote:Original post by Wyrframe
All would be fine and good if it weren''t for the fact that computers cannot "set equal to zero and solve". And nobody has yet taught me how to re-write that into "add this, multiply this, and there you go".


ummm...do the solving yourself on paper then code it.

quote:Original post by Wyrframe
Computers can do nothing but straight arithmetic, and so that''s the realm that one must work in when working with them. And besides which, you''re looking at an absolute value; the point we''re looking for on the derivative of s() has no unique solution at the sharp point.


|x| where x is a vector is generally taken as the magnitude of x. Sorry for the confusion.

|v| = sqrt(v dot v)
|v| = sqrt(v.x*v.x + v.y*v.y + v.z*v.z)

So s(t) is in fact well behavied, its derivative with respect to t is defined for all t; however, i don''t suggest going that route as it gets messy fast.

Look at my second post and solve it yourself. You''ll be glad you did.
Advertisement
alvaro is going about it right, except his code is a little buggy. If you have a math book around that talks about vectors look up "scalar projection" and "vector projection."

the point on the ray closest to x is given by...

p1 + ((x-p1) dot d) * d

d: unit vector, direction of ray
p1: vector, start location of ray
x: vector, point of interest
Alright! Just tested out that last posts solution... works in all the cases that I needed it to. Many thanks, nonoptimalrobot, for your posts (including that final one).

Thanks all, for your help.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
quote:Original post by nonoptimalrobot
alvaro is going about it right, except his code is a little buggy.


What bugs does my code have?
quote:Original post by alvaro
What bugs does my code have?


It''s your ''Norm()'' function. I''m not quite sure what you are meaning this function to do. As it stands it simply returns the passed vector dotted with itself, which is a scalar value equal to the length of the vector squared. Maybe you meant ''Norm()'' to be a magnitude function, or a magnitude squared function? Either way this...

Point closest(Ray const &r, Point const &p)
{
return r.p+(p-r.p)*r.v*r.v/Norm(r.v);
}

should be this...

Point closest(Ray const &r, Point const &p)
{
return r.p + (p-r.p)*r.v * r.v/sqrtf(r.v*r.v);
}

or if ''r.v'' is a unit vector then you can simply do this...

Point closest(Ray const &r, Point const &p)
{
return r.p + (p-r.p)*r.v * r.v;
}

The middle part part:
(p-r.p)*r.v
is known as the scalar projection of (p-r.v) onto r.v.

The middle part plus the last part:
(p-r.p)*r.v * r.v/sqrtf(r.v*r.v)
is known as the vector projection of (p-r.v) onto r.v.

Anyway...nice save with the projecting strategy. I''m not sure why I was making this so complicated.
Granted, the function Norm() should be called NormSquared() or something like that. http://planetmath.org/encyclopedia/InnerProduct.html

But you have to divide by that, and not by the square root. Do a simple example by hand if you want to convince yourself.

quote:Original post by alvaro
Granted, the function Norm() should be called NormSquared() or something like that. http://planetmath.org/encyclopedia/InnerProduct.html

But you have to divide by that, and not by the square root. Do a simple example by hand if you want to convince yourself.


Goodness, your right. The term ''norm'' really threw me off. I guess i''m a little rustier in the math department than I thought. Time to review.

This topic is closed to new replies.

Advertisement