diff --git a/src/3d_helpers.cc b/src/3d_helpers.cc index 0f98c11..9d17173 100644 --- a/src/3d_helpers.cc +++ b/src/3d_helpers.cc @@ -6,6 +6,9 @@ // third party includes #include +// local includes +#include "ems.h" + Matrix get_identity_matrix() { return Matrix{1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F}; @@ -166,6 +169,18 @@ std::optional ray_to_plane(const Ray &ray, const Ray &plane) { ray.position.z + ray.direction.z * amount}; } +Vector3 from_edge_to_sphere_random(Vector3 center, Vector3 point, + float radius) { + Vector3 to_center = center - point; + Vector3 perpendicular = Vector3Normalize(Vector3Perpendicular(to_center)); + + return Vector3Normalize( + to_center + Vector3RotateByAxisAngle(perpendicular, + Vector3Normalize(to_center), + call_js_get_random() * PI * 2.0F) * + (call_js_get_random() * radius)); +} + Vector3 operator+(const Vector3 &a, const Vector3 &b) { return Vector3{a.x + b.x, a.y + b.y, a.z + b.z}; } diff --git a/src/3d_helpers.h b/src/3d_helpers.h index a407c01..b4cfcd4 100644 --- a/src/3d_helpers.h +++ b/src/3d_helpers.h @@ -27,6 +27,9 @@ 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. extern std::optional ray_to_plane(const Ray &ray, const Ray &plane); +extern Vector3 from_edge_to_sphere_random(Vector3 center, Vector3 point, + float radius); + // Unimplemented as this function isn't really needed and it exposes some // weirdness regarding column-major matrices. // extern Vector4 operator*(const Matrix &m, const Vector4 &v); diff --git a/src/electricity_effect.cc b/src/electricity_effect.cc index 699b422..ea9435d 100644 --- a/src/electricity_effect.cc +++ b/src/electricity_effect.cc @@ -80,13 +80,8 @@ bool ElectricityEffect::update(float dt) { if (Vector3Distance(cylinder.point, center) > radius) { cylinder.point = cylinder.point - cylinder.mdir * (dt * CYLINDER_MOVE_RATE); - Vector3 to_center = center - cylinder.point; - Vector3 perpendicular = Vector3Normalize(Vector3Perpendicular(to_center)); - cylinder.mdir = Vector3Normalize( - to_center + Vector3RotateByAxisAngle(perpendicular, - Vector3Normalize(to_center), - call_js_get_random() * PI * 2) * - (call_js_get_random() * radius)); + cylinder.mdir = + from_edge_to_sphere_random(center, cylinder.point, radius); } }