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; }
Plane::Plane( const cc::Vec3f& a, const cc::Vec3f& b, const cc::Vec3f& c ) { const cc::Vec3f ab = b - a; const cc::Vec3f ac = c - a; const cc::Vec3f cross = ab.cross(ac); _normal = cross.normalized(); _d = -_normal.dot(a); }
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; } } }