static Plane constructFromPointNormal( const cc::Vec3f& point, const cc::Vec3f& normal ) { Plane result; const cc::Vec3f normalizedNormal = normal.normalized(); result.normal = normalizedNormal; result.constant = -point.dot(normalizedNormal); return result; }
void OpenCloth::computeForces( float deltaTime ) { for( int i = 0; i < _totalPoints; ++i ) { _forces[i].x = _forces[i].y = _forces[i].z = 0.0f; const cc::Vec3f velocity = getVerletVelocity(_positions[i], _lastPositions[i], deltaTime); if( (i != 0) && (i != _divsX) ) { _forces[i] += _gravity * _mass; } _forces[i] += _damping * velocity; } for( int i = 0; i < _springCount; ++i ) { const cc::Vec3f& p1 = _positions[_springs[i].p1]; const cc::Vec3f& p1Last = _lastPositions[_springs[i].p1]; const cc::Vec3f& p2 = _positions[_springs[i].p2]; const cc::Vec3f& p2Last = _lastPositions[_springs[i].p2]; const cc::Vec3f v1 = getVerletVelocity(p1, p1Last, deltaTime); const cc::Vec3f v2 = getVerletVelocity(p2, p2Last, deltaTime); const cc::Vec3f deltaP = p1 - p2; const cc::Vec3f deltaV = v1 - v2; const float dist = deltaP.magnitude(); const float leftTerm = -_springs[i].ks * (dist - _springs[i].restLength); const float rightTerm = _springs[i].kd * (deltaV.dot(deltaP) / dist); const cc::Vec3f springForce = (leftTerm + rightTerm) * deltaP.normalized(); if( _springs[i].p1 != 0 && _springs[i].p1 != _divsX ) { _forces[_springs[i].p1] += springForce; } if( _springs[i].p2 != 0 && _springs[i].p2 != _divsX ) { _forces[_springs[i].p2] -= springForce; } } }
void OpenCloth::addSpring( int* idx, int a, int b, float ks, float kd ) { _springs[*idx].p1 = a; _springs[*idx].p2 = b; _springs[*idx].ks = ks; _springs[*idx].kd = kd; const cc::Vec3f deltaP = _positions[a] - _positions[b]; _springs[*idx].restLength = sqrtf(deltaP.dot(deltaP)); *idx += 1; }