MCPcopy Create free account
hub / github.com/carbonengine/trinity / ApplyConstraint

Method ApplyConstraint

trinity/Particle/Tr2PlaneConstraint.cpp:114–187  ·  view source on GitHub ↗

-------------------------------------------------------------------------------------- Description: Implements ITr2GenericParticleConstraint interface. Checks for collision between a particle and a half-space defined by m_plane. If collision is detected optionally apply physical response to particle position and/or velocity and run generators to re-generate particle data components. This method ca

Source from the content-addressed store, hash-verified

112// dt - (unused) Frame time.
113// --------------------------------------------------------------------------------------
114void Tr2PlaneConstraint::ApplyConstraint( const ITr2GenericEmitter::UpdateArguments& arguments, float** particles, unsigned* strides, unsigned count, float /* dt */ )
115{
116 if( !m_isValid )
117 {
118 return;
119 }
120
121 Tr2ParticleStreamIterator<Vector3> position( particles, strides, m_positionElement );
122 Tr2ParticleStreamIterator<Vector4> radiusStream( particles, strides, m_radiusElement );
123 Tr2ParticleStreamIterator<Vector3> velocity( particles, strides, m_velocityElement );
124
125 for( unsigned i = 0; i < count; ++i, ++position, ++radiusStream, ++velocity )
126 {
127 float radius = 0;
128 if( m_radiusElement.m_offset != -1 )
129 {
130 radius = Dot( *radiusStream, m_particleRadiusCoefficient );
131 }
132
133 float dot = DotCoord( m_normalizedPlane, *position ) - radius;
134 const Vector3& planeNormal = reinterpret_cast<const Vector3&>( m_normalizedPlane );
135 float velocityDot = -1.f;
136 if( m_velocityElement.m_offset != -1 )
137 {
138 velocityDot = Dot( *velocity, planeNormal );
139 }
140 if( dot <= 0 && velocityDot < 0 )
141 {
142 if( m_affectPosition )
143 {
144 Vector3 shift = planeNormal * dot;
145 *position -= shift;
146 }
147 if( m_affectVelocity && m_velocityElement.m_offset != -1 )
148 {
149 Vector3 bounce = planeNormal * velocityDot;
150 Vector3 slide = *velocity - bounce;
151 bounce *= -m_elasticity;
152 slide *= m_friction;
153 *velocity = bounce + slide;
154 if( m_reflectionNoise > 0 )
155 {
156 Vector3 reflectionNoise(
157 Tr2ParticleSystem::RandFloat() * 2 - 1,
158 Tr2ParticleSystem::RandFloat() * 2 - 1,
159 Tr2ParticleSystem::RandFloat() * 2 - 1 );
160 reflectionNoise *= m_reflectionNoise;
161 reflectionNoise -= planeNormal * Dot( reflectionNoise, planeNormal );
162 float speed = Length( *velocity );
163 reflectionNoise *= speed;
164 *velocity += reflectionNoise;
165 }
166 }
167 if( !m_generators.empty() )
168 {
169 float* particle[Tr2ParticleElementData::COUNT];
170 for( unsigned j = 0; j < Tr2ParticleElementData::COUNT; ++j )
171 {

Callers 1

UpdateSimulationMethod · 0.45

Calls 8

RandFloatFunction · 0.85
LengthFunction · 0.85
emptyMethod · 0.80
DotFunction · 0.50
beginMethod · 0.45
endMethod · 0.45
GenerateMethod · 0.45
SpawnParticlesMethod · 0.45

Tested by

no test coverage detected