Minor fixes to 3d_helpers.cc
All checks were successful
Build and Publish WASM version of demo / Build-And-Deploy (push) Successful in 17s

This commit is contained in:
Stephen Seo 2023-08-18 13:27:49 +09:00
parent ff03f30809
commit d4850f99bb

View file

@ -127,7 +127,7 @@ Matrix operator*(const Matrix &a, const Matrix &b) {
}
bool ray_to_xz_plane(const Ray &ray, float &x_out, float &z_out) {
if (ray.direction.y == 0.0F) {
if (FloatEquals(ray.direction.y, 0.0F)) {
return false;
}
// y = 0 -> amount to set ray.dir to set ray.pos to zero
@ -147,7 +147,7 @@ bool ray_to_xz_plane(const Ray &ray, float &x_out, float &z_out) {
std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane) {
// Ray dir and plane normal.
float rd_pn = Vector3DotProduct(ray.direction, plane.direction);
if (rd_pn == 0.0F) {
if (FloatEquals(rd_pn, 0.0F)) {
return std::nullopt;
}
@ -155,6 +155,11 @@ std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane) {
Vector3DotProduct(ray.position, plane.direction)) /
rd_pn;
// Negative amount means collision in opposite direction of ray.
if (amount < 0.0F) {
return std::nullopt;
}
// Amount * ray_dir + ray_pos == plane intersection.
return Vector3{ray.position.x + ray.direction.x * amount,
ray.position.y + ray.direction.y * amount,