| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | #include <osgParticle/ModularProgram> |
|---|
| 17 | #include <osgParticle/BounceOperator> |
|---|
| 18 | |
|---|
| 19 | using namespace osgParticle; |
|---|
| 20 | |
|---|
| 21 | void BounceOperator::handleTriangle( const Domain& domain, Particle* P, double dt ) |
|---|
| 22 | { |
|---|
| 23 | osg::Vec3 nextpos = P->getPosition() + P->getVelocity() * dt; |
|---|
| 24 | float distance = domain.plane.distance( P->getPosition() ); |
|---|
| 25 | if ( distance*domain.plane.distance(nextpos)>=0 ) return; |
|---|
| 26 | |
|---|
| 27 | osg::Vec3 normal = domain.plane.getNormal(); |
|---|
| 28 | float nv = normal * P->getVelocity(); |
|---|
| 29 | osg::Vec3 hitPoint = P->getPosition() - P->getVelocity() * (distance / nv); |
|---|
| 30 | |
|---|
| 31 | float upos = (hitPoint - domain.v1) * domain.s1; |
|---|
| 32 | float vpos = (hitPoint - domain.v1) * domain.s2; |
|---|
| 33 | if ( upos<0.0f || vpos<0.0f || (upos + vpos)>1.0f ) return; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | osg::Vec3 vn = normal * nv; |
|---|
| 37 | osg::Vec3 vt = P->getVelocity() - vn; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | if ( vt.length2()<=_cutoff ) P->setVelocity( vt - vn*_resilience ); |
|---|
| 41 | else P->setVelocity( vt*(1.0f-_friction) - vn*_resilience ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void BounceOperator::handleRectangle( const Domain& domain, Particle* P, double dt ) |
|---|
| 45 | { |
|---|
| 46 | osg::Vec3 nextpos = P->getPosition() + P->getVelocity() * dt; |
|---|
| 47 | float distance = domain.plane.distance( P->getPosition() ); |
|---|
| 48 | if ( distance*domain.plane.distance(nextpos)>=0 ) return; |
|---|
| 49 | |
|---|
| 50 | osg::Vec3 normal = domain.plane.getNormal(); |
|---|
| 51 | float nv = normal * P->getVelocity(); |
|---|
| 52 | osg::Vec3 hitPoint = P->getPosition() - P->getVelocity() * (distance / nv); |
|---|
| 53 | |
|---|
| 54 | float upos = (hitPoint - domain.v1) * domain.s1; |
|---|
| 55 | float vpos = (hitPoint - domain.v1) * domain.s2; |
|---|
| 56 | if ( upos<0.0f || upos>1.0f || vpos<0.0f || vpos>1.0f ) return; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | osg::Vec3 vn = normal * nv; |
|---|
| 60 | osg::Vec3 vt = P->getVelocity() - vn; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | if ( vt.length2()<=_cutoff ) P->setVelocity( vt - vn*_resilience ); |
|---|
| 64 | else P->setVelocity( vt*(1.0f-_friction) - vn*_resilience ); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void BounceOperator::handlePlane( const Domain& domain, Particle* P, double dt ) |
|---|
| 68 | { |
|---|
| 69 | osg::Vec3 nextpos = P->getPosition() + P->getVelocity() * dt; |
|---|
| 70 | float distance = domain.plane.distance( P->getPosition() ); |
|---|
| 71 | if ( distance*domain.plane.distance(nextpos)>=0 ) return; |
|---|
| 72 | |
|---|
| 73 | osg::Vec3 normal = domain.plane.getNormal(); |
|---|
| 74 | float nv = normal * P->getVelocity(); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | osg::Vec3 vn = normal * nv; |
|---|
| 78 | osg::Vec3 vt = P->getVelocity() - vn; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | if ( vt.length2()<=_cutoff ) P->setVelocity( vt - vn*_resilience ); |
|---|
| 82 | else P->setVelocity( vt*(1.0f-_friction) - vn*_resilience ); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void BounceOperator::handleSphere( const Domain& domain, Particle* P, double dt ) |
|---|
| 86 | { |
|---|
| 87 | osg::Vec3 nextpos = P->getPosition() + P->getVelocity() * dt; |
|---|
| 88 | float distance1 = (P->getPosition() - domain.v1).length(); |
|---|
| 89 | if ( distance1<=domain.r1 ) |
|---|
| 90 | { |
|---|
| 91 | float distance2 = (nextpos - domain.v1).length(); |
|---|
| 92 | if ( distance2<=domain.r1 ) return; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | osg::Vec3 normal = domain.v1 - P->getPosition(); normal.normalize(); |
|---|
| 96 | float nmag = P->getVelocity() * normal; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | osg::Vec3 vn = normal * nmag; |
|---|
| 100 | osg::Vec3 vt = P->getVelocity() - vn; |
|---|
| 101 | if ( nmag<0 ) vn = -vn; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | float tanscale = (vt.length2()<=_cutoff) ? 1.0f : (1.0f - _friction); |
|---|
| 105 | P->setVelocity( vt * tanscale + vn * _resilience ); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | nextpos = P->getPosition() + P->getVelocity() * dt; |
|---|
| 109 | distance2 = (nextpos - domain.v1).length(); |
|---|
| 110 | if ( distance2>domain.r1 ) |
|---|
| 111 | { |
|---|
| 112 | normal = domain.v1 - nextpos; normal.normalize(); |
|---|
| 113 | |
|---|
| 114 | osg::Vec3 wishPoint = domain.v1 - normal * (0.999f * domain.r1); |
|---|
| 115 | P->setVelocity( (wishPoint - P->getPosition()) / dt ); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | else |
|---|
| 119 | { |
|---|
| 120 | float distance2 = (nextpos - domain.v1).length(); |
|---|
| 121 | if ( distance2>domain.r1 ) return; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | osg::Vec3 normal = P->getPosition() - domain.v1; normal.normalize(); |
|---|
| 125 | float nmag = P->getVelocity() * normal; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | osg::Vec3 vn = normal * nmag; |
|---|
| 129 | osg::Vec3 vt = P->getVelocity() - vn; |
|---|
| 130 | if ( nmag<0 ) vn = -vn; |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | float tanscale = (vt.length2()<=_cutoff) ? 1.0f : (1.0f - _friction); |
|---|
| 134 | P->setVelocity( vt * tanscale + vn * _resilience ); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void BounceOperator::handleDisk( const Domain& domain, Particle* P, double dt ) |
|---|
| 139 | { |
|---|
| 140 | osg::Vec3 nextpos = P->getPosition() + P->getVelocity() * dt; |
|---|
| 141 | float distance = domain.plane.distance( P->getPosition() ); |
|---|
| 142 | if ( distance*domain.plane.distance(nextpos)>=0 ) return; |
|---|
| 143 | |
|---|
| 144 | osg::Vec3 normal = domain.plane.getNormal(); |
|---|
| 145 | float nv = normal * P->getVelocity(); |
|---|
| 146 | osg::Vec3 hitPoint = P->getPosition() - P->getVelocity() * (distance / nv); |
|---|
| 147 | |
|---|
| 148 | float radius = (hitPoint - domain.v1).length(); |
|---|
| 149 | if ( radius>domain.r1 || radius<domain.r2 ) return; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | osg::Vec3 vn = normal * nv; |
|---|
| 153 | osg::Vec3 vt = P->getVelocity() - vn; |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | if ( vt.length2()<=_cutoff ) P->setVelocity( vt - vn*_resilience ); |
|---|
| 157 | else P->setVelocity( vt*(1.0f-_friction) - vn*_resilience ); |
|---|
| 158 | } |
|---|