Richard Garfield
Richard Garfield

Reputation: 455

Finding the intersection of a ray and a plane programmatically?

I understand the math here: http://lousodrome.net/blog/light/2020/07/03/intersection-of-a-ray-and-a-plane/

This is my implementation of his equation:

    Vector3 difference = plane_point - ray_origin;
    double product_1 = difference.dot(plane_normal);
    double product_2 = ray_direction.dot(plane_normal);
    double distance_from_origin_to_plane = product_1 / product_2;
    Vector3 intersection = ray_origin - ray_direction * distance_from_origin_to_plane;

However, my implementation doesnt work unless I change the first line to:

Vector3 difference = ray_origin - plane_point;

But I cant figure out why that works. His math makes perfect sense. The working code makes no sense.

Can anyone explain whats going on?

Upvotes: 1

Views: 921

Answers (1)

Richard Garfield
Richard Garfield

Reputation: 455

apple apple answered it in the comments. The last line should have had a + not a -.

The working code that makes sense now is:

    Vector3 difference = plane_point - ray_origin;
    double product_1 = difference.dot(plane_normal);
    double product_2 = ray_direction.dot(plane_normal);
    double distance_from_origin_to_plane = product_1 / product_2;
    Vector3 intersection = ray_origin + ray_direction * distance_from_origin_to_plane;

Upvotes: 2

Related Questions