Use Raylib's Ray/Triangle collision fn
All checks were successful
Build and Publish WASM version of demo / Build-And-Deploy (push) Successful in 16s

This commit is contained in:
Stephen Seo 2023-08-09 14:00:54 +09:00
parent 7f73388943
commit ba241417a2
3 changed files with 7 additions and 41 deletions

View file

@ -160,30 +160,3 @@ std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane) {
ray.position.y + ray.direction.y * amount, ray.position.y + ray.direction.y * amount,
ray.position.z + ray.direction.z * amount}; ray.position.z + ray.direction.z * amount};
} }
std::optional<Vector3> ray_collision_triangle(const Ray &ray, const Vector3 &a,
const Vector3 &b,
const Vector3 &c) {
const Vector3 ab = Vector3Subtract(a, b);
const Vector3 cb = Vector3Subtract(c, b);
Vector3 ortho = Vector3CrossProduct(ab, cb);
ortho = Vector3Normalize(ortho);
Ray plane{.position = b, .direction = ortho};
auto result = ray_to_plane(ray, plane);
if (!result.has_value()) {
return std::nullopt;
}
Vector3 bary = Vector3Barycenter(result.value(), a, b, c);
if (bary.x >= 0.0F && bary.x <= 1.0F && bary.y >= 0.0F && bary.y <= 1.0F &&
bary.z >= 0.0F && bary.z <= 1.0F) {
float sum = bary.x + bary.y + bary.z;
if (sum > 0.999F && sum < 1.001) {
return result;
}
}
return std::nullopt;
}

View file

@ -27,11 +27,6 @@ extern bool ray_to_xz_plane(const Ray &ray, float &x_out, float &z_out);
/// plane.direction is plane normal, plane.position is position on plane. /// plane.direction is plane normal, plane.position is position on plane.
extern std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane); extern std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane);
extern std::optional<Vector3> ray_collision_triangle(const Ray &ray,
const Vector3 &a,
const Vector3 &b,
const Vector3 &c);
// Unimplemented as this function isn't really needed and it exposes some // Unimplemented as this function isn't really needed and it exposes some
// weirdness regarding column-major matrices. // weirdness regarding column-major matrices.
// extern Vector4 operator*(const Matrix &m, const Vector4 &v); // extern Vector4 operator*(const Matrix &m, const Vector4 &v);

View file

@ -239,7 +239,7 @@ bool TRunnerScreen::update(float dt) {
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "idx_hit set to " << idx_hit << std::endl; std::cout << "idx_hit set to " << idx_hit << std::endl;
#endif #endif
this->mouse_hit = collision.value(); this->mouse_hit = collision;
this->camera_target.x = xf; this->camera_target.x = xf;
this->camera_target.y = this->camera_target.y =
@ -260,14 +260,12 @@ bool TRunnerScreen::update(float dt) {
if (auto bb_collision = GetRayCollisionBox(ray, surface_bbs[idx]); if (auto bb_collision = GetRayCollisionBox(ray, surface_bbs[idx]);
bb_collision.hit) { bb_collision.hit) {
if (auto collision = ray_collision_triangle(ray, nw, sw, ne); if (auto collision = GetRayCollisionTriangle(ray, nw, sw, ne);
collision.has_value()) { collision.hit) {
on_collide_fn(collision); on_collide_fn(collision.point);
break; } else if (auto collision = GetRayCollisionTriangle(ray, ne, sw, se);
} else if (auto collision = ray_collision_triangle(ray, ne, sw, se); collision.hit) {
collision.has_value()) { on_collide_fn(collision.point);
on_collide_fn(collision);
break;
} }
} }
} }