Cogs.Core
LinePlaneIntersection.h
1#pragma once
2
3#include <glm/geometric.hpp>
4#include <glm/vec3.hpp>
5
6namespace Cogs
7{
8 namespace Geometry
9 {
10 static bool intersect(const glm::vec3 normal, const float distance, const glm::vec3 & origin, const glm::vec3 & direction, glm::vec3 & intersection)
11 {
12 // Check if the line is parallel to the plane.
13 if (glm::dot(direction, normal) == 0.0f) return false;
14
15 // From the discussion on SbDPLine::getClosestPoint() we know that
16 // any point on the line can be expressed as:
17 // Q = P + t*D (1)
18 //
19 // We can also easily see that a point must satisfy this equation to lie
20 // in the plane:
21 // N·(Q - d*N) = 0, where N is the normal vector,
22 // Q is the point and d the offset
23 // from the origin.
24 //
25 // Combining these two equations and simplifying we get:
26 //
27 // d*|N|² - N·P
28 // t = ----------------, |N| == 1
29 // N·D
30 //
31 // Substituting t back in (1), we've solved the problem.
32 // 19980816 mortene.
33
34 float t = (distance - glm::dot(normal, origin)) / glm::dot(normal, direction);
35
36 intersection = origin + t * direction;
37
38 return true;
39 }
40 }
41}
@ Geometry
Store entity vector fields (vector<vec3>, vector<vec2>, vector<int>, vector<float>).
Contains all Cogs related functionality.
Definition: FieldSetter.h:23