[Bf-blender-cvs] [05027c4d41b] functions: cleanup bounce code a little
Jacques Lucke
noreply at git.blender.org
Mon Jul 1 17:47:16 CEST 2019
Commit: 05027c4d41bb623c5c6b81ef19fea878dcab9b00
Author: Jacques Lucke
Date: Mon Jul 1 16:57:43 2019 +0200
Branches: functions
https://developer.blender.org/rB05027c4d41bb623c5c6b81ef19fea878dcab9b00
cleanup bounce code a little
===================================================================
M source/blender/blenlib/BLI_math.hpp
M source/blender/simulations/bparticles/events.cpp
===================================================================
diff --git a/source/blender/blenlib/BLI_math.hpp b/source/blender/blenlib/BLI_math.hpp
index 9ba7ea815ca..47e0d88cd03 100644
--- a/source/blender/blenlib/BLI_math.hpp
+++ b/source/blender/blenlib/BLI_math.hpp
@@ -46,10 +46,22 @@ struct float3 {
}
void reflect(float3 normal)
+ {
+ *this = this->reflected(normal);
+ }
+
+ float3 reflected(float3 normal)
{
float3 result;
reflect_v3_v3v3(result, *this, normal);
- *this = result;
+ return result;
+ }
+
+ void invert()
+ {
+ x = -x;
+ y = -y;
+ z = -z;
}
friend float3 operator+(float3 a, float3 b)
@@ -64,6 +76,18 @@ struct float3 {
this->z += b.z;
}
+ friend float3 operator-(float3 a, float3 b)
+ {
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
+ }
+
+ void operator-=(float3 b)
+ {
+ this->x -= b.x;
+ this->y -= b.y;
+ this->z -= b.z;
+ }
+
void operator*=(float scalar)
{
this->x *= scalar;
@@ -97,6 +121,11 @@ struct float3 {
stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return stream;
}
+
+ static float dot(float3 a, float3 b)
+ {
+ return a.x * b.x + a.y * b.y + a.z * b.z;
+ }
};
struct float4x4 {
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index 456387fbeea..a8639e4f51f 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -88,7 +88,12 @@ class MeshBounceEvent : public Event {
if (hit.index != -1) {
float time_factor = hit.dist / length;
auto &data = interface.trigger_particle<CollisionData>(i, time_factor);
- data.normal = m_normal_transform.transform_direction(hit.no).normalized();
+
+ float3 normal = hit.no;
+ if (float3::dot(hit.no, direction) > 0) {
+ normal.invert();
+ }
+ data.normal = m_normal_transform.transform_direction(normal).normalized();
}
}
}
@@ -104,13 +109,24 @@ class MeshBounceEvent : public Event {
for (uint pindex : particles.indices()) {
auto &data = interface.get_storage<CollisionData>(pindex);
- velocities[pindex].reflect(data.normal);
- position_offsets[pindex].reflect(data.normal);
+ /* Move particle back a little bit to avoid double collision. */
+ positions[pindex] += data.normal * 0.001f;
- /* Temporary solution to avoid double collision. */
- positions[pindex] += velocities[pindex] * 0.0001f;
+ velocities[pindex] = this->bounce_direction(velocities[pindex], data.normal);
+ position_offsets[pindex] = this->bounce_direction(position_offsets[pindex], data.normal);
}
}
+
+ float3 bounce_direction(float3 direction, float3 normal)
+ {
+ direction = direction.reflected(normal);
+
+ float normal_part = float3::dot(direction, normal);
+ float3 direction_normal = normal * normal_part;
+ float3 direction_tangent = direction - direction_normal;
+
+ return direction_normal * 0.5 + direction_tangent * 0.99;
+ }
};
EventFilter *EVENT_age_reached(float age)
More information about the Bf-blender-cvs
mailing list