Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 20, 2008, 5:40:38 PM (16 years ago)
Author:
rgrieder
Message:

Downgraded Bullet to latest tagged version: 2.72
That should give us more stability.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/physics/src/bullet/BulletSoftBody/btSoftBodyInternals.h

    r1963 r1972  
    3232struct btSymMatrix
    3333{
    34         btSymMatrix() : dim(0)                                  {}
    35         btSymMatrix(int n,const T& init=T())    { resize(n,init); }
    36         void                                    resize(int n,const T& init=T())                 { dim=n;store.resize((n*(n+1))/2,init); }
    37         int                                             index(int c,int r) const                                { if(c>r) btSwap(c,r);btAssert(r<dim);return((r*(r+1))/2+c); }
    38         T&                                              operator()(int c,int r)                                 { return(store[index(c,r)]); }
    39         const T&                                operator()(int c,int r) const                   { return(store[index(c,r)]); }
    40         btAlignedObjectArray<T> store;
    41         int                                             dim;
     34                                                btSymMatrix() : dim(0)                                  {}
     35                                                btSymMatrix(int n,const T& init=T())    { resize(n,init); }
     36void                                    resize(int n,const T& init=T())                 { dim=n;store.resize((n*(n+1))/2,init); }
     37int                                             index(int c,int r) const                                { if(c>r) btSwap(c,r);btAssert(r<dim);return((r*(r+1))/2+c); }
     38T&                                              operator()(int c,int r)                                 { return(store[index(c,r)]); }
     39const T&                                operator()(int c,int r) const                   { return(store[index(c,r)]); }
     40btAlignedObjectArray<T> store;
     41int                                             dim;
    4242};     
    4343
     
    4949public:
    5050        btSoftBody*                                             m_body;
    51 
     51       
    5252        btSoftBodyCollisionShape(btSoftBody* backptr)
    5353        {
     
    7070        virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
    7171        {
    72                 /* t should be identity, but better be safe than...fast? */
    73                 const btVector3 mins=m_body->m_bounds[0];
    74                 const btVector3 maxs=m_body->m_bounds[1];
    75                 const btVector3 crns[]={t*btVector3(mins.x(),mins.y(),mins.z()),
    76                         t*btVector3(maxs.x(),mins.y(),mins.z()),
    77                         t*btVector3(maxs.x(),maxs.y(),mins.z()),
    78                         t*btVector3(mins.x(),maxs.y(),mins.z()),
    79                         t*btVector3(mins.x(),mins.y(),maxs.z()),
    80                         t*btVector3(maxs.x(),mins.y(),maxs.z()),
    81                         t*btVector3(maxs.x(),maxs.y(),maxs.z()),
    82                         t*btVector3(mins.x(),maxs.y(),maxs.z())};
    83                 aabbMin=aabbMax=crns[0];
    84                 for(int i=1;i<8;++i)
    85                 {
    86                         aabbMin.setMin(crns[i]);
    87                         aabbMax.setMax(crns[i]);
    88                 }
    89         }
    90 
    91 
     72        /* t should be identity, but better be safe than...fast? */
     73        const btVector3 mins=m_body->m_bounds[0];
     74        const btVector3 maxs=m_body->m_bounds[1];
     75        const btVector3 crns[]={t*btVector3(mins.x(),mins.y(),mins.z()),
     76                                                        t*btVector3(maxs.x(),mins.y(),mins.z()),
     77                                                        t*btVector3(maxs.x(),maxs.y(),mins.z()),
     78                                                        t*btVector3(mins.x(),maxs.y(),mins.z()),
     79                                                        t*btVector3(mins.x(),mins.y(),maxs.z()),
     80                                                        t*btVector3(maxs.x(),mins.y(),maxs.z()),
     81                                                        t*btVector3(maxs.x(),maxs.y(),maxs.z()),
     82                                                        t*btVector3(mins.x(),maxs.y(),maxs.z())};
     83        aabbMin=aabbMax=crns[0];
     84        for(int i=1;i<8;++i)
     85                {
     86                aabbMin.setMin(crns[i]);
     87                aabbMax.setMax(crns[i]);
     88                }
     89        }
     90
     91       
    9292        virtual void    setLocalScaling(const btVector3& /*scaling*/)
    9393        {               
     
    9696        virtual const btVector3& getLocalScaling() const
    9797        {
    98                 static const btVector3 dummy(1,1,1);
    99                 return dummy;
     98        static const btVector3 dummy(1,1,1);
     99        return dummy;
    100100        }
    101101        virtual void    calculateLocalInertia(btScalar /*mass*/,btVector3& /*inertia*/) const
     
    120120
    121121        btSoftClusterCollisionShape (const btSoftBody::Cluster* cluster) : m_cluster(cluster) { setMargin(0); }
    122 
    123 
     122       
     123       
    124124        virtual btVector3       localGetSupportingVertex(const btVector3& vec) const
    125         {
     125                {
    126126                btSoftBody::Node* const *                                               n=&m_cluster->m_nodes[0];
    127127                btScalar                                                                                d=dot(vec,n[0]->m_x);
    128128                int                                                                                             j=0;
    129129                for(int i=1,ni=m_cluster->m_nodes.size();i<ni;++i)
    130                 {
     130                        {
    131131                        const btScalar  k=dot(vec,n[i]->m_x);
    132132                        if(k>d) { d=k;j=i; }
    133                 }
     133                        }
    134134                return(n[j]->m_x);
    135         }
     135                }
    136136        virtual btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const
    137         {
     137                {
    138138                return(localGetSupportingVertex(vec));
    139         }
     139                }
    140140        //notice that the vectors should be unit length
    141141        virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
     
    150150
    151151        virtual int     getShapeType() const { return SOFTBODY_SHAPE_PROXYTYPE; }
    152 
     152       
    153153        //debugging
    154154        virtual const char*     getName()const {return "SOFTCLUSTER";}
     
    172172static inline void                      ZeroInitialize(T& value)
    173173{
    174         static const T  zerodummy;
    175         value=zerodummy;
     174static const T  zerodummy;
     175value=zerodummy;
    176176}
    177177//
     
    193193//
    194194static inline btMatrix3x3       Lerp(   const btMatrix3x3& a,
    195                                                                  const btMatrix3x3& b,
    196                                                                  btScalar t)
    197 {
    198         btMatrix3x3     r;
    199         r[0]=Lerp(a[0],b[0],t);
    200         r[1]=Lerp(a[1],b[1],t);
    201         r[2]=Lerp(a[2],b[2],t);
    202         return(r);
     195                                                                        const btMatrix3x3& b,
     196                                                                        btScalar t)
     197{
     198btMatrix3x3     r;
     199r[0]=Lerp(a[0],b[0],t);
     200r[1]=Lerp(a[1],b[1],t);
     201r[2]=Lerp(a[2],b[2],t);
     202return(r);
    203203}
    204204//
    205205static inline btVector3         Clamp(const btVector3& v,btScalar maxlength)
    206206{
    207         const btScalar sql=v.length2();
    208         if(sql>(maxlength*maxlength))
    209                 return((v*maxlength)/btSqrt(sql));
     207const btScalar sql=v.length2();
     208if(sql>(maxlength*maxlength))
     209        return((v*maxlength)/btSqrt(sql));
    210210        else
    211                 return(v);
     211        return(v);
    212212}
    213213//
     
    234234static inline btScalar          ClusterMetric(const btVector3& x,const btVector3& y)
    235235{
    236         const btVector3 d=x-y;
    237         return(btFabs(d[0])+btFabs(d[1])+btFabs(d[2]));
     236const btVector3 d=x-y;
     237return(btFabs(d[0])+btFabs(d[1])+btFabs(d[2]));
    238238}
    239239//
     
    272272//
    273273static inline btMatrix3x3       Add(const btMatrix3x3& a,
    274                                                                 const btMatrix3x3& b)
     274        const btMatrix3x3& b)
    275275{
    276276        btMatrix3x3     r;
     
    280280//
    281281static inline btMatrix3x3       Sub(const btMatrix3x3& a,
    282                                                                 const btMatrix3x3& b)
     282        const btMatrix3x3& b)
    283283{
    284284        btMatrix3x3     r;
     
    288288//
    289289static inline btMatrix3x3       Mul(const btMatrix3x3& a,
    290                                                                 btScalar b)
     290        btScalar b)
    291291{
    292292        btMatrix3x3     r;
     
    297297static inline void                      Orthogonalize(btMatrix3x3& m)
    298298{
    299         m[2]=cross(m[0],m[1]).normalized();
    300         m[1]=cross(m[2],m[0]).normalized();
    301         m[0]=cross(m[1],m[2]).normalized();
     299m[2]=cross(m[0],m[1]).normalized();
     300m[1]=cross(m[2],m[0]).normalized();
     301m[0]=cross(m[1],m[2]).normalized();
    302302}
    303303//
     
    310310//
    311311static inline btMatrix3x3       ImpulseMatrix(  btScalar dt,
    312                                                                                   btScalar ima,
    313                                                                                   btScalar imb,
    314                                                                                   const btMatrix3x3& iwi,
    315                                                                                   const btVector3& r)
     312        btScalar ima,
     313        btScalar imb,
     314        const btMatrix3x3& iwi,
     315        const btVector3& r)
    316316{
    317317        return(Diagonal(1/dt)*Add(Diagonal(ima),MassMatrix(imb,iwi,r)).inverse());
     
    320320//
    321321static inline btMatrix3x3       ImpulseMatrix(  btScalar ima,const btMatrix3x3& iia,const btVector3& ra,
    322                                                                                   btScalar imb,const btMatrix3x3& iib,const btVector3& rb)     
    323 {
    324         return(Add(MassMatrix(ima,iia,ra),MassMatrix(imb,iib,rb)).inverse());
     322                                                                                        btScalar imb,const btMatrix3x3& iib,const btVector3& rb)       
     323{
     324return(Add(MassMatrix(ima,iia,ra),MassMatrix(imb,iib,rb)).inverse());
    325325}
    326326
    327327//
    328328static inline btMatrix3x3       AngularImpulseMatrix(   const btMatrix3x3& iia,
    329                                                                                                  const btMatrix3x3& iib)
    330 {
    331         return(Add(iia,iib).inverse());
     329                                                                                                        const btMatrix3x3& iib)
     330{
     331return(Add(iia,iib).inverse());
    332332}
    333333
    334334//
    335335static inline btVector3         ProjectOnAxis(  const btVector3& v,
    336                                                                                   const btVector3& a)
     336        const btVector3& a)
    337337{
    338338        return(a*dot(v,a));
     
    340340//
    341341static inline btVector3         ProjectOnPlane( const btVector3& v,
    342                                                                                    const btVector3& a)
     342        const btVector3& a)
    343343{
    344344        return(v-ProjectOnAxis(v,a));
     
    347347//
    348348static inline void                      ProjectOrigin(  const btVector3& a,
    349                                                                                   const btVector3& b,
    350                                                                                   btVector3& prj,
    351                                                                                   btScalar& sqd)
    352 {
    353         const btVector3 d=b-a;
    354         const btScalar  m2=d.length2();
    355         if(m2>SIMD_EPSILON)
     349                                                                                        const btVector3& b,
     350                                                                                        btVector3& prj,
     351                                                                                        btScalar& sqd)
     352{
     353const btVector3 d=b-a;
     354const btScalar  m2=d.length2();
     355if(m2>SIMD_EPSILON)
    356356        {       
    357                 const btScalar  t=Clamp<btScalar>(-dot(a,d)/m2,0,1);
    358                 const btVector3 p=a+d*t;
    359                 const btScalar  l2=p.length2();
    360                 if(l2<sqd)
    361                 {
     357        const btScalar  t=Clamp<btScalar>(-dot(a,d)/m2,0,1);
     358        const btVector3 p=a+d*t;
     359        const btScalar  l2=p.length2();
     360        if(l2<sqd)
     361                {
     362                prj=p;
     363                sqd=l2;
     364                }
     365        }
     366}
     367//
     368static inline void                      ProjectOrigin(  const btVector3& a,
     369                                                                                        const btVector3& b,
     370                                                                                        const btVector3& c,
     371                                                                                        btVector3& prj,
     372                                                                                        btScalar& sqd)
     373{
     374const btVector3&        q=cross(b-a,c-a);
     375const btScalar          m2=q.length2();
     376if(m2>SIMD_EPSILON)
     377        {
     378        const btVector3 n=q/btSqrt(m2);
     379        const btScalar  k=dot(a,n);
     380        const btScalar  k2=k*k;
     381        if(k2<sqd)
     382                {
     383                const btVector3 p=n*k;
     384                if(     (dot(cross(a-p,b-p),q)>0)&&
     385                        (dot(cross(b-p,c-p),q)>0)&&
     386                        (dot(cross(c-p,a-p),q)>0))
     387                        {                       
    362388                        prj=p;
    363                         sqd=l2;
    364                 }
    365         }
    366 }
    367 //
    368 static inline void                      ProjectOrigin(  const btVector3& a,
    369                                                                                   const btVector3& b,
    370                                                                                   const btVector3& c,
    371                                                                                   btVector3& prj,
    372                                                                                   btScalar& sqd)
    373 {
    374         const btVector3&        q=cross(b-a,c-a);
    375         const btScalar          m2=q.length2();
    376         if(m2>SIMD_EPSILON)
    377         {
    378                 const btVector3 n=q/btSqrt(m2);
    379                 const btScalar  k=dot(a,n);
    380                 const btScalar  k2=k*k;
    381                 if(k2<sqd)
    382                 {
    383                         const btVector3 p=n*k;
    384                         if(     (dot(cross(a-p,b-p),q)>0)&&
    385                                 (dot(cross(b-p,c-p),q)>0)&&
    386                                 (dot(cross(c-p,a-p),q)>0))
    387                         {                       
    388                                 prj=p;
    389                                 sqd=k2;
     389                        sqd=k2;
    390390                        }
    391391                        else
    392392                        {
    393                                 ProjectOrigin(a,b,prj,sqd);
    394                                 ProjectOrigin(b,c,prj,sqd);
    395                                 ProjectOrigin(c,a,prj,sqd);
     393                        ProjectOrigin(a,b,prj,sqd);
     394                        ProjectOrigin(b,c,prj,sqd);
     395                        ProjectOrigin(c,a,prj,sqd);
    396396                        }
    397397                }
     
    402402template <typename T>
    403403static inline T                         BaryEval(               const T& a,
    404                                                                          const T& b,
    405                                                                          const T& c,
    406                                                                          const btVector3& coord)
     404                                                                                        const T& b,
     405                                                                                        const T& c,
     406                                                                                        const btVector3& coord)
    407407{
    408408        return(a*coord.x()+b*coord.y()+c*coord.z());
     
    410410//
    411411static inline btVector3         BaryCoord(      const btVector3& a,
    412                                                                           const btVector3& b,
    413                                                                           const btVector3& c,
    414                                                                           const btVector3& p)
    415 {
    416         const btScalar  w[]={   cross(a-p,b-p).length(),
    417                 cross(b-p,c-p).length(),
    418                 cross(c-p,a-p).length()};
    419         const btScalar  isum=1/(w[0]+w[1]+w[2]);
    420         return(btVector3(w[1]*isum,w[2]*isum,w[0]*isum));
     412                                                                                const btVector3& b,
     413                                                                                const btVector3& c,
     414                                                                                const btVector3& p)
     415{
     416const btScalar  w[]={   cross(a-p,b-p).length(),
     417                                                cross(b-p,c-p).length(),
     418                                                cross(c-p,a-p).length()};
     419const btScalar  isum=1/(w[0]+w[1]+w[2]);
     420return(btVector3(w[1]*isum,w[2]*isum,w[0]*isum));
    421421}
    422422
    423423//
    424424static btScalar                         ImplicitSolve(  btSoftBody::ImplicitFn* fn,
    425                                                                                   const btVector3& a,
    426                                                                                   const btVector3& b,
    427                                                                                   const btScalar accuracy,
    428                                                                                   const int maxiterations=256)
    429 {
    430         btScalar        span[2]={0,1};
    431         btScalar        values[2]={fn->Eval(a),fn->Eval(b)};
    432         if(values[0]>values[1])
    433         {
    434                 btSwap(span[0],span[1]);
    435                 btSwap(values[0],values[1]);
    436         }
    437         if(values[0]>-accuracy) return(-1);
    438         if(values[1]<+accuracy) return(-1);
    439         for(int i=0;i<maxiterations;++i)
    440         {
    441                 const btScalar  t=Lerp(span[0],span[1],values[0]/(values[0]-values[1]));
    442                 const btScalar  v=fn->Eval(Lerp(a,b,t));
    443                 if((t<=0)||(t>=1))              break;
    444                 if(btFabs(v)<accuracy)  return(t);
    445                 if(v<0)
     425                                                                                        const btVector3& a,
     426                                                                                        const btVector3& b,
     427                                                                                        const btScalar accuracy,
     428                                                                                        const int maxiterations=256)
     429{
     430btScalar        span[2]={0,1};
     431btScalar        values[2]={fn->Eval(a),fn->Eval(b)};
     432if(values[0]>values[1])
     433        {
     434        btSwap(span[0],span[1]);
     435        btSwap(values[0],values[1]);
     436        }
     437if(values[0]>-accuracy) return(-1);
     438if(values[1]<+accuracy) return(-1);
     439for(int i=0;i<maxiterations;++i)
     440        {
     441        const btScalar  t=Lerp(span[0],span[1],values[0]/(values[0]-values[1]));
     442        const btScalar  v=fn->Eval(Lerp(a,b,t));
     443        if((t<=0)||(t>=1))              break;
     444        if(btFabs(v)<accuracy)  return(t);
     445        if(v<0)
    446446                { span[0]=t;values[0]=v; }
    447447                else
    448448                { span[1]=t;values[1]=v; }
    449449        }
    450         return(-1);
     450return(-1);
    451451}
    452452
     
    463463//
    464464static inline btDbvtVolume      VolumeOf(       const btSoftBody::Face& f,
    465                                                                          btScalar margin)
    466 {
    467         const btVector3*        pts[]={ &f.m_n[0]->m_x,
    468                 &f.m_n[1]->m_x,
    469                 &f.m_n[2]->m_x};
    470         btDbvtVolume            vol=btDbvtVolume::FromPoints(pts,3);
    471         vol.Expand(btVector3(margin,margin,margin));
    472         return(vol);
     465                                                                                btScalar margin)
     466{
     467const btVector3*        pts[]={ &f.m_n[0]->m_x,
     468                                                        &f.m_n[1]->m_x,
     469                                                        &f.m_n[2]->m_x};
     470btDbvtVolume            vol=btDbvtVolume::FromPoints(pts,3);
     471vol.Expand(btVector3(margin,margin,margin));
     472return(vol);
    473473}
    474474
     
    476476static inline btVector3                 CenterOf(       const btSoftBody::Face& f)
    477477{
    478         return((f.m_n[0]->m_x+f.m_n[1]->m_x+f.m_n[2]->m_x)/3);
     478return((f.m_n[0]->m_x+f.m_n[1]->m_x+f.m_n[2]->m_x)/3);
    479479}
    480480
    481481//
    482482static inline btScalar                  AreaOf(         const btVector3& x0,
    483                                                                            const btVector3& x1,
    484                                                                            const btVector3& x2)
     483                                                                                        const btVector3& x1,
     484                                                                                        const btVector3& x2)
    485485{
    486486        const btVector3 a=x1-x0;
     
    493493//
    494494static inline btScalar          VolumeOf(       const btVector3& x0,
    495                                                                          const btVector3& x1,
    496                                                                          const btVector3& x2,
    497                                                                          const btVector3& x3)
     495                                                                                const btVector3& x1,
     496                                                                                const btVector3& x2,
     497                                                                                const btVector3& x3)
    498498{
    499499        const btVector3 a=x1-x0;
     
    505505//
    506506static void                                     EvaluateMedium( const btSoftBodyWorldInfo* wfi,
    507                                                                                    const btVector3& x,
    508                                                                                    btSoftBody::sMedium& medium)
     507                                                                                        const btVector3& x,
     508                                                                                        btSoftBody::sMedium& medium)
    509509{
    510510        medium.m_velocity       =       btVector3(0,0,0);
     
    524524//
    525525static inline void                      ApplyClampedForce(      btSoftBody::Node& n,
    526                                                                                           const btVector3& f,
    527                                                                                           btScalar dt)
     526                                                                                                const btVector3& f,
     527                                                                                                btScalar dt)
    528528{
    529529        const btScalar  dtim=dt*n.m_im;
     
    540540//
    541541static inline int               MatchEdge(      const btSoftBody::Node* a,
    542                                                                   const btSoftBody::Node* b,
    543                                                                   const btSoftBody::Node* ma,
    544                                                                   const btSoftBody::Node* mb)
    545 {
    546         if((a==ma)&&(b==mb)) return(0);
    547         if((a==mb)&&(b==ma)) return(1);
    548         return(-1);
     542                                                                        const btSoftBody::Node* b,
     543                                                                        const btSoftBody::Node* ma,
     544                                                                        const btSoftBody::Node* mb)
     545{
     546if((a==ma)&&(b==mb)) return(0);
     547if((a==mb)&&(b==ma)) return(1);
     548return(-1);
    549549}
    550550
     
    556556struct  btEigen
    557557{
    558         static int                      system(btMatrix3x3& a,btMatrix3x3* vectors,btVector3* values=0)
    559         {
    560                 static const int                maxiterations=16;
    561                 static const btScalar   accuracy=(btScalar)0.0001;
    562                 btMatrix3x3&                    v=*vectors;
    563                 int                                             iterations=0;
    564                 vectors->setIdentity();
    565                 do      {
    566                         int                             p=0,q=1;
    567                         if(btFabs(a[p][q])<btFabs(a[0][2])) { p=0;q=2; }
    568                         if(btFabs(a[p][q])<btFabs(a[1][2])) { p=1;q=2; }
    569                         if(btFabs(a[p][q])>accuracy)
     558static int                      system(btMatrix3x3& a,btMatrix3x3* vectors,btVector3* values=0)
     559        {
     560        static const int                maxiterations=16;
     561        static const btScalar   accuracy=(btScalar)0.0001;
     562        btMatrix3x3&                    v=*vectors;
     563        int                                             iterations=0;
     564        vectors->setIdentity();
     565        do      {
     566                int                             p=0,q=1;
     567                if(btFabs(a[p][q])<btFabs(a[0][2])) { p=0;q=2; }
     568                if(btFabs(a[p][q])<btFabs(a[1][2])) { p=1;q=2; }
     569                if(btFabs(a[p][q])>accuracy)
    570570                        {
    571                                 const btScalar  w=(a[q][q]-a[p][p])/(2*a[p][q]);
    572                                 const btScalar  z=btFabs(w);
    573                                 const btScalar  t=w/(z*(btSqrt(1+w*w)+z));
    574                                 if(t==t)/* [WARNING] let hope that one does not get thrown aways by some compilers... */
     571                        const btScalar  w=(a[q][q]-a[p][p])/(2*a[p][q]);
     572                        const btScalar  z=btFabs(w);
     573                        const btScalar  t=w/(z*(btSqrt(1+w*w)+z));
     574                        if(t==t)/* [WARNING] let hope that one does not get thrown aways by some compilers... */
    575575                                {
    576                                         const btScalar  c=1/btSqrt(t*t+1);
    577                                         const btScalar  s=c*t;
    578                                         mulPQ(a,c,s,p,q);
    579                                         mulTPQ(a,c,s,p,q);
    580                                         mulPQ(v,c,s,p,q);
     576                                const btScalar  c=1/btSqrt(t*t+1);
     577                                const btScalar  s=c*t;
     578                                mulPQ(a,c,s,p,q);
     579                                mulTPQ(a,c,s,p,q);
     580                                mulPQ(v,c,s,p,q);
    581581                                } else break;
    582582                        } else break;
    583583                } while((++iterations)<maxiterations);
    584                 if(values)
    585                 {
    586                         *values=btVector3(a[0][0],a[1][1],a[2][2]);
    587                 }
    588                 return(iterations);
     584        if(values)
     585                {
     586                *values=btVector3(a[0][0],a[1][1],a[2][2]);
     587                }
     588        return(iterations);
    589589        }
    590590private:
    591         static inline void      mulTPQ(btMatrix3x3& a,btScalar c,btScalar s,int p,int q)
    592         {
    593                 const btScalar  m[2][3]={       {a[p][0],a[p][1],a[p][2]},
    594                 {a[q][0],a[q][1],a[q][2]}};
    595                 int i;
    596 
    597                 for(i=0;i<3;++i) a[p][i]=c*m[0][i]-s*m[1][i];
    598                 for(i=0;i<3;++i) a[q][i]=c*m[1][i]+s*m[0][i];
    599         }
    600         static inline void      mulPQ(btMatrix3x3& a,btScalar c,btScalar s,int p,int q)
    601         {
    602                 const btScalar  m[2][3]={       {a[0][p],a[1][p],a[2][p]},
    603                 {a[0][q],a[1][q],a[2][q]}};
    604                 int i;
    605 
    606                 for(i=0;i<3;++i) a[i][p]=c*m[0][i]-s*m[1][i];
    607                 for(i=0;i<3;++i) a[i][q]=c*m[1][i]+s*m[0][i];
     591static inline void      mulTPQ(btMatrix3x3& a,btScalar c,btScalar s,int p,int q)
     592        {
     593        const btScalar  m[2][3]={       {a[p][0],a[p][1],a[p][2]},
     594                                                                {a[q][0],a[q][1],a[q][2]}};
     595        int i;
     596
     597        for(i=0;i<3;++i) a[p][i]=c*m[0][i]-s*m[1][i];
     598        for(i=0;i<3;++i) a[q][i]=c*m[1][i]+s*m[0][i];
     599        }
     600static inline void      mulPQ(btMatrix3x3& a,btScalar c,btScalar s,int p,int q)
     601        {
     602        const btScalar  m[2][3]={       {a[0][p],a[1][p],a[2][p]},
     603                                                                {a[0][q],a[1][q],a[2][q]}};
     604        int i;
     605
     606        for(i=0;i<3;++i) a[i][p]=c*m[0][i]-s*m[1][i];
     607        for(i=0;i<3;++i) a[i][q]=c*m[1][i]+s*m[0][i];
    608608        }
    609609};
     
    640640                s.setIdentity();
    641641        }
    642         return(i);
     642return(i);
    643643}
    644644
     
    653653        struct  ClusterBase : btDbvt::ICollide
    654654        {
    655                 btScalar                        erp;
    656                 btScalar                        idt;
    657                 btScalar                        margin;
    658                 btScalar                        friction;
    659                 btScalar                        threshold;
    660                 ClusterBase()
    661                 {
    662                         erp                     =(btScalar)1;
    663                         idt                     =0;
    664                         margin          =0;
    665                         friction        =0;
    666                         threshold       =(btScalar)0;
    667                 }
    668                 bool                            SolveContact(   const btGjkEpaSolver2::sResults& res,
    669                         btSoftBody::Body ba,btSoftBody::Body bb,
    670                         btSoftBody::CJoint& joint)
    671                 {
    672                         if(res.distance<margin)
     655        btScalar                        erp;
     656        btScalar                        idt;
     657        btScalar                        margin;
     658        btScalar                        friction;
     659        btScalar                        threshold;
     660                                        ClusterBase()
     661                {
     662                erp                     =(btScalar)1;
     663                idt                     =0;
     664                margin          =0;
     665                friction        =0;
     666                threshold       =(btScalar)0;
     667                }
     668        bool                            SolveContact(   const btGjkEpaSolver2::sResults& res,
     669                                                                                btSoftBody::Body ba,btSoftBody::Body bb,
     670                                                                                btSoftBody::CJoint& joint)
     671                {
     672                if(res.distance<margin)
    673673                        {
    674                                 const btVector3         ra=res.witnesses[0]-ba.xform().getOrigin();
    675                                 const btVector3         rb=res.witnesses[1]-bb.xform().getOrigin();
    676                                 const btVector3         va=ba.velocity(ra);
    677                                 const btVector3         vb=bb.velocity(rb);
    678                                 const btVector3         vrel=va-vb;
    679                                 const btScalar          rvac=dot(vrel,res.normal);
    680                                 const btScalar          depth=res.distance-margin;
    681                                 const btVector3         iv=res.normal*rvac;
    682                                 const btVector3         fv=vrel-iv;
    683                                 joint.m_bodies[0]       =       ba;
    684                                 joint.m_bodies[1]       =       bb;
    685                                 joint.m_refs[0]         =       ra*ba.xform().getBasis();
    686                                 joint.m_refs[1]         =       rb*bb.xform().getBasis();
    687                                 joint.m_rpos[0]         =       ra;
    688                                 joint.m_rpos[1]         =       rb;
    689                                 joint.m_cfm                     =       1;
    690                                 joint.m_erp                     =       1;
    691                                 joint.m_life            =       0;
    692                                 joint.m_maxlife         =       0;
    693                                 joint.m_split           =       1;
    694                                 joint.m_drift           =       depth*res.normal;
    695                                 joint.m_normal          =       res.normal;
    696                                 joint.m_delete          =       false;
    697                                 joint.m_friction        =       fv.length2()<(-rvac*friction)?1:friction;
    698                                 joint.m_massmatrix      =       ImpulseMatrix(  ba.invMass(),ba.invWorldInertia(),joint.m_rpos[0],
    699                                         bb.invMass(),bb.invWorldInertia(),joint.m_rpos[1]);
    700                                 return(true);
     674                        const btVector3         ra=res.witnesses[0]-ba.xform().getOrigin();
     675                        const btVector3         rb=res.witnesses[1]-bb.xform().getOrigin();
     676                        const btVector3         va=ba.velocity(ra);
     677                        const btVector3         vb=bb.velocity(rb);
     678                        const btVector3         vrel=va-vb;
     679                        const btScalar          rvac=dot(vrel,res.normal);
     680                        const btScalar          depth=res.distance-margin;
     681                        const btVector3         iv=res.normal*rvac;
     682                        const btVector3         fv=vrel-iv;
     683                        joint.m_bodies[0]       =       ba;
     684                        joint.m_bodies[1]       =       bb;
     685                        joint.m_refs[0]         =       ra*ba.xform().getBasis();
     686                        joint.m_refs[1]         =       rb*bb.xform().getBasis();
     687                        joint.m_rpos[0]         =       ra;
     688                        joint.m_rpos[1]         =       rb;
     689                        joint.m_cfm                     =       1;
     690                        joint.m_erp                     =       1;
     691                        joint.m_life            =       0;
     692                        joint.m_maxlife         =       0;
     693                        joint.m_split           =       1;
     694                        joint.m_drift           =       depth*res.normal;
     695                        joint.m_normal          =       res.normal;
     696                        joint.m_delete          =       false;
     697                        joint.m_friction        =       fv.length2()<(-rvac*friction)?1:friction;
     698                        joint.m_massmatrix      =       ImpulseMatrix(  ba.invMass(),ba.invWorldInertia(),joint.m_rpos[0],
     699                                                                                                        bb.invMass(),bb.invWorldInertia(),joint.m_rpos[1]);
     700                        return(true);
    701701                        }
    702                         return(false);
     702                return(false);
    703703                }
    704704        };
     
    708708        struct  CollideCL_RS : ClusterBase
    709709        {
    710                 btSoftBody*             psb;
    711                 btRigidBody*    prb;
    712                 void            Process(const btDbvtNode* leaf)
    713                 {
    714                         btSoftBody::Cluster*            cluster=(btSoftBody::Cluster*)leaf->data;
    715                         btSoftClusterCollisionShape     cshape(cluster);
    716                         const btConvexShape*            rshape=(const btConvexShape*)prb->getCollisionShape();
    717                         btGjkEpaSolver2::sResults       res;           
    718                         if(btGjkEpaSolver2::SignedDistance(     &cshape,btTransform::getIdentity(),
    719                                 rshape,prb->getInterpolationWorldTransform(),
    720                                 btVector3(1,0,0),res))
     710        btSoftBody*             psb;
     711        btRigidBody*    prb;
     712        void            Process(const btDbvtNode* leaf)
     713                {
     714                btSoftBody::Cluster*            cluster=(btSoftBody::Cluster*)leaf->data;
     715                btSoftClusterCollisionShape     cshape(cluster);
     716                const btConvexShape*            rshape=(const btConvexShape*)prb->getCollisionShape();
     717                btGjkEpaSolver2::sResults       res;           
     718                if(btGjkEpaSolver2::SignedDistance(     &cshape,btTransform::getIdentity(),
     719                                                                                        rshape,prb->getInterpolationWorldTransform(),
     720                                                                                        btVector3(1,0,0),res))
    721721                        {
    722                                 btSoftBody::CJoint      joint;
    723                                 if(SolveContact(res,cluster,prb,joint))
     722                        btSoftBody::CJoint      joint;
     723                        if(SolveContact(res,cluster,prb,joint))
    724724                                {
    725                                         btSoftBody::CJoint*     pj=new(btAlignedAlloc(sizeof(btSoftBody::CJoint),16)) btSoftBody::CJoint();
    726                                         *pj=joint;psb->m_joints.push_back(pj);
    727                                         if(prb->isStaticOrKinematicObject())
     725                                btSoftBody::CJoint*     pj=new(btAlignedAlloc(sizeof(btSoftBody::CJoint),16)) btSoftBody::CJoint();
     726                                *pj=joint;psb->m_joints.push_back(pj);
     727                                if(prb->isStaticOrKinematicObject())
    728728                                        {
    729                                                 pj->m_erp       *=      psb->m_cfg.kSKHR_CL;
    730                                                 pj->m_split     *=      psb->m_cfg.kSK_SPLT_CL;
     729                                        pj->m_erp       *=      psb->m_cfg.kSKHR_CL;
     730                                        pj->m_split     *=      psb->m_cfg.kSK_SPLT_CL;
    731731                                        }
    732732                                        else
    733733                                        {
    734                                                 pj->m_erp       *=      psb->m_cfg.kSRHR_CL;
    735                                                 pj->m_split     *=      psb->m_cfg.kSR_SPLT_CL;
     734                                        pj->m_erp       *=      psb->m_cfg.kSRHR_CL;
     735                                        pj->m_split     *=      psb->m_cfg.kSR_SPLT_CL;
    736736                                        }
    737737                                }
    738738                        }
    739739                }
    740                 void            Process(btSoftBody* ps,btRigidBody* pr)
    741                 {
    742                         psb                     =       ps;
    743                         prb                     =       pr;
    744                         idt                     =       ps->m_sst.isdt;
    745                         margin          =       ps->getCollisionShape()->getMargin()+
    746                                 pr->getCollisionShape()->getMargin();
    747                         friction        =       btMin(psb->m_cfg.kDF,prb->getFriction());
    748                         btVector3                       mins;
    749                         btVector3                       maxs;
    750 
    751                         ATTRIBUTE_ALIGNED16(btDbvtVolume)               volume;
    752                         pr->getCollisionShape()->getAabb(pr->getInterpolationWorldTransform(),mins,maxs);
    753                         volume=btDbvtVolume::FromMM(mins,maxs);
    754                         volume.Expand(btVector3(1,1,1)*margin);
    755                         btDbvt::collideTV(ps->m_cdbvt.m_root,volume,*this);
     740        void            Process(btSoftBody* ps,btRigidBody* pr)
     741                {
     742                psb                     =       ps;
     743                prb                     =       pr;
     744                idt                     =       ps->m_sst.isdt;
     745                margin          =       ps->getCollisionShape()->getMargin()+
     746                                                pr->getCollisionShape()->getMargin();
     747                friction        =       btMin(psb->m_cfg.kDF,prb->getFriction());
     748                btVector3                       mins;
     749                btVector3                       maxs;
     750               
     751                ATTRIBUTE_ALIGNED16(btDbvtVolume)               volume;
     752                pr->getCollisionShape()->getAabb(pr->getInterpolationWorldTransform(),mins,maxs);
     753                volume=btDbvtVolume::FromMM(mins,maxs);
     754                volume.Expand(btVector3(1,1,1)*margin);
     755                btDbvt::collideTV(ps->m_cdbvt.m_root,volume,*this);
    756756                }       
    757757        };
     
    761761        struct  CollideCL_SS : ClusterBase
    762762        {
    763                 btSoftBody*     bodies[2];
    764                 void            Process(const btDbvtNode* la,const btDbvtNode* lb)
    765                 {
    766                         btSoftBody::Cluster*            cla=(btSoftBody::Cluster*)la->data;
    767                         btSoftBody::Cluster*            clb=(btSoftBody::Cluster*)lb->data;
    768                         btSoftClusterCollisionShape     csa(cla);
    769                         btSoftClusterCollisionShape     csb(clb);
    770                         btGjkEpaSolver2::sResults       res;           
    771                         if(btGjkEpaSolver2::SignedDistance(     &csa,btTransform::getIdentity(),
    772                                 &csb,btTransform::getIdentity(),
    773                                 cla->m_com-clb->m_com,res))
     763        btSoftBody*     bodies[2];
     764        void            Process(const btDbvtNode* la,const btDbvtNode* lb)
     765                {
     766                btSoftBody::Cluster*            cla=(btSoftBody::Cluster*)la->data;
     767                btSoftBody::Cluster*            clb=(btSoftBody::Cluster*)lb->data;
     768                btSoftClusterCollisionShape     csa(cla);
     769                btSoftClusterCollisionShape     csb(clb);
     770                btGjkEpaSolver2::sResults       res;           
     771                if(btGjkEpaSolver2::SignedDistance(     &csa,btTransform::getIdentity(),
     772                                                                                        &csb,btTransform::getIdentity(),
     773                                                                                        cla->m_com-clb->m_com,res))
    774774                        {
    775                                 btSoftBody::CJoint      joint;
    776                                 if(SolveContact(res,cla,clb,joint))
     775                        btSoftBody::CJoint      joint;
     776                        if(SolveContact(res,cla,clb,joint))
    777777                                {
    778                                         btSoftBody::CJoint*     pj=new(btAlignedAlloc(sizeof(btSoftBody::CJoint),16)) btSoftBody::CJoint();
    779                                         *pj=joint;bodies[0]->m_joints.push_back(pj);
    780                                         pj->m_erp       *=      btMax(bodies[0]->m_cfg.kSSHR_CL,bodies[1]->m_cfg.kSSHR_CL);
    781                                         pj->m_split     *=      (bodies[0]->m_cfg.kSS_SPLT_CL+bodies[1]->m_cfg.kSS_SPLT_CL)/2;
     778                                btSoftBody::CJoint*     pj=new(btAlignedAlloc(sizeof(btSoftBody::CJoint),16)) btSoftBody::CJoint();
     779                                *pj=joint;bodies[0]->m_joints.push_back(pj);
     780                                pj->m_erp       *=      btMax(bodies[0]->m_cfg.kSSHR_CL,bodies[1]->m_cfg.kSSHR_CL);
     781                                pj->m_split     *=      (bodies[0]->m_cfg.kSS_SPLT_CL+bodies[1]->m_cfg.kSS_SPLT_CL)/2;
    782782                                }
    783783                        }
    784784                }
    785                 void            Process(btSoftBody* psa,btSoftBody* psb)
    786                 {
    787                         idt                     =       psa->m_sst.isdt;
    788                         margin          =       (psa->getCollisionShape()->getMargin()+psb->getCollisionShape()->getMargin())/2;
    789                         friction        =       btMin(psa->m_cfg.kDF,psb->m_cfg.kDF);
    790                         bodies[0]       =       psa;
    791                         bodies[1]       =       psb;
    792                         btDbvt::collideTT(psa->m_cdbvt.m_root,psb->m_cdbvt.m_root,*this);
     785        void            Process(btSoftBody* psa,btSoftBody* psb)
     786                {
     787                idt                     =       psa->m_sst.isdt;
     788                margin          =       (psa->getCollisionShape()->getMargin()+psb->getCollisionShape()->getMargin())/2;
     789                friction        =       btMin(psa->m_cfg.kDF,psb->m_cfg.kDF);
     790                bodies[0]       =       psa;
     791                bodies[1]       =       psb;
     792                btDbvt::collideTT(psa->m_cdbvt.m_root,psb->m_cdbvt.m_root,*this);
    793793                }       
    794794        };
     
    798798        struct  CollideSDF_RS : btDbvt::ICollide
    799799        {
    800                 void            Process(const btDbvtNode* leaf)
    801                 {
    802                         btSoftBody::Node*       node=(btSoftBody::Node*)leaf->data;
    803                         DoNode(*node);
    804                 }
    805                 void            DoNode(btSoftBody::Node& n) const
    806                 {
    807                         const btScalar                  m=n.m_im>0?dynmargin:stamargin;
    808                         btSoftBody::RContact    c;
    809                         if(     (!n.m_battach)&&
    810                                 psb->checkContact(prb,n.m_x,m,c.m_cti))
     800        void            Process(const btDbvtNode* leaf)
     801                {
     802                btSoftBody::Node*       node=(btSoftBody::Node*)leaf->data;
     803                DoNode(*node);
     804                }
     805        void            DoNode(btSoftBody::Node& n) const
     806                {
     807                const btScalar                  m=n.m_im>0?dynmargin:stamargin;
     808                btSoftBody::RContact    c;
     809                if(     (!n.m_battach)&&
     810                        psb->checkContact(prb,n.m_x,m,c.m_cti))
    811811                        {
    812                                 const btScalar  ima=n.m_im;
    813                                 const btScalar  imb=prb->getInvMass();
    814                                 const btScalar  ms=ima+imb;
    815                                 if(ms>0)
     812                        const btScalar  ima=n.m_im;
     813                        const btScalar  imb=prb->getInvMass();
     814                        const btScalar  ms=ima+imb;
     815                        if(ms>0)
    816816                                {
    817                                         const btTransform&      wtr=prb->getInterpolationWorldTransform();
    818                                         const btMatrix3x3&      iwi=prb->getInvInertiaTensorWorld();
    819                                         const btVector3         ra=n.m_x-wtr.getOrigin();
    820                                         const btVector3         va=prb->getVelocityInLocalPoint(ra)*psb->m_sst.sdt;
    821                                         const btVector3         vb=n.m_x-n.m_q;
    822                                         const btVector3         vr=vb-va;
    823                                         const btScalar          dn=dot(vr,c.m_cti.m_normal);
    824                                         const btVector3         fv=vr-c.m_cti.m_normal*dn;
    825                                         const btScalar          fc=psb->m_cfg.kDF*prb->getFriction();
    826                                         c.m_node        =       &n;
    827                                         c.m_c0          =       ImpulseMatrix(psb->m_sst.sdt,ima,imb,iwi,ra);
    828                                         c.m_c1          =       ra;
    829                                         c.m_c2          =       ima*psb->m_sst.sdt;
    830                                         c.m_c3          =       fv.length2()<(btFabs(dn)*fc)?0:1-fc;
    831                                         c.m_c4          =       prb->isStaticOrKinematicObject()?psb->m_cfg.kKHR:psb->m_cfg.kCHR;
    832                                         psb->m_rcontacts.push_back(c);
    833                                         prb->activate();
     817                                const btTransform&      wtr=prb->getInterpolationWorldTransform();
     818                                const btMatrix3x3&      iwi=prb->getInvInertiaTensorWorld();
     819                                const btVector3         ra=n.m_x-wtr.getOrigin();
     820                                const btVector3         va=prb->getVelocityInLocalPoint(ra)*psb->m_sst.sdt;
     821                                const btVector3         vb=n.m_x-n.m_q;
     822                                const btVector3         vr=vb-va;
     823                                const btScalar          dn=dot(vr,c.m_cti.m_normal);
     824                                const btVector3         fv=vr-c.m_cti.m_normal*dn;
     825                                const btScalar          fc=psb->m_cfg.kDF*prb->getFriction();
     826                                c.m_node        =       &n;
     827                                c.m_c0          =       ImpulseMatrix(psb->m_sst.sdt,ima,imb,iwi,ra);
     828                                c.m_c1          =       ra;
     829                                c.m_c2          =       ima*psb->m_sst.sdt;
     830                                c.m_c3          =       fv.length2()<(btFabs(dn)*fc)?0:1-fc;
     831                                c.m_c4          =       prb->isStaticOrKinematicObject()?psb->m_cfg.kKHR:psb->m_cfg.kCHR;
     832                                psb->m_rcontacts.push_back(c);
     833                                prb->activate();
    834834                                }
    835835                        }
    836836                }
    837                 btSoftBody*             psb;
    838                 btRigidBody*    prb;
    839                 btScalar                dynmargin;
    840                 btScalar                stamargin;
     837        btSoftBody*             psb;
     838        btRigidBody*    prb;
     839        btScalar                dynmargin;
     840        btScalar                stamargin;
    841841        };
    842842        //
     
    845845        struct  CollideVF_SS : btDbvt::ICollide
    846846        {
    847                 void            Process(const btDbvtNode* lnode,
    848                         const btDbvtNode* lface)
    849                 {
    850                         btSoftBody::Node*       node=(btSoftBody::Node*)lnode->data;
    851                         btSoftBody::Face*       face=(btSoftBody::Face*)lface->data;
    852                         btVector3                       o=node->m_x;
    853                         btVector3                       p;
    854                         btScalar                        d=SIMD_INFINITY;
    855                         ProjectOrigin(  face->m_n[0]->m_x-o,
    856                                 face->m_n[1]->m_x-o,
    857                                 face->m_n[2]->m_x-o,
    858                                 p,d);
    859                         const btScalar  m=mrg+(o-node->m_q).length()*2;
    860                         if(d<(m*m))
     847        void            Process(const btDbvtNode* lnode,
     848                                                const btDbvtNode* lface)
     849                {
     850                btSoftBody::Node*       node=(btSoftBody::Node*)lnode->data;
     851                btSoftBody::Face*       face=(btSoftBody::Face*)lface->data;
     852                btVector3                       o=node->m_x;
     853                btVector3                       p;
     854                btScalar                        d=SIMD_INFINITY;
     855                ProjectOrigin(  face->m_n[0]->m_x-o,
     856                                                face->m_n[1]->m_x-o,
     857                                                face->m_n[2]->m_x-o,
     858                                                p,d);
     859                const btScalar  m=mrg+(o-node->m_q).length()*2;
     860                if(d<(m*m))
    861861                        {
    862                                 const btSoftBody::Node* n[]={face->m_n[0],face->m_n[1],face->m_n[2]};
    863                                 const btVector3                 w=BaryCoord(n[0]->m_x,n[1]->m_x,n[2]->m_x,p+o);
    864                                 const btScalar                  ma=node->m_im;
    865                                 btScalar                                mb=BaryEval(n[0]->m_im,n[1]->m_im,n[2]->m_im,w);
    866                                 if(     (n[0]->m_im<=0)||
    867                                         (n[1]->m_im<=0)||
    868                                         (n[2]->m_im<=0))
     862                        const btSoftBody::Node* n[]={face->m_n[0],face->m_n[1],face->m_n[2]};
     863                        const btVector3                 w=BaryCoord(n[0]->m_x,n[1]->m_x,n[2]->m_x,p+o);
     864                        const btScalar                  ma=node->m_im;
     865                        btScalar                                mb=BaryEval(n[0]->m_im,n[1]->m_im,n[2]->m_im,w);
     866                        if(     (n[0]->m_im<=0)||
     867                                (n[1]->m_im<=0)||
     868                                (n[2]->m_im<=0))
    869869                                {
    870                                         mb=0;
     870                                mb=0;
    871871                                }
    872                                 const btScalar  ms=ma+mb;
    873                                 if(ms>0)
     872                        const btScalar  ms=ma+mb;
     873                        if(ms>0)
    874874                                {
    875                                         btSoftBody::SContact    c;
    876                                         c.m_normal              =       p/-btSqrt(d);
    877                                         c.m_margin              =       m;
    878                                         c.m_node                =       node;
    879                                         c.m_face                =       face;
    880                                         c.m_weights             =       w;
    881                                         c.m_friction    =       btMax(psb[0]->m_cfg.kDF,psb[1]->m_cfg.kDF);
    882                                         c.m_cfm[0]              =       ma/ms*psb[0]->m_cfg.kSHR;
    883                                         c.m_cfm[1]              =       mb/ms*psb[1]->m_cfg.kSHR;
    884                                         psb[0]->m_scontacts.push_back(c);
     875                                btSoftBody::SContact    c;
     876                                c.m_normal              =       p/-btSqrt(d);
     877                                c.m_margin              =       m;
     878                                c.m_node                =       node;
     879                                c.m_face                =       face;
     880                                c.m_weights             =       w;
     881                                c.m_friction    =       btMax(psb[0]->m_cfg.kDF,psb[1]->m_cfg.kDF);
     882                                c.m_cfm[0]              =       ma/ms*psb[0]->m_cfg.kSHR;
     883                                c.m_cfm[1]              =       mb/ms*psb[1]->m_cfg.kSHR;
     884                                psb[0]->m_scontacts.push_back(c);
    885885                                }
    886886                        }       
    887887                }
    888                 btSoftBody*             psb[2];
    889                 btScalar                mrg;
     888        btSoftBody*             psb[2];
     889        btScalar                mrg;
    890890        };
    891891};
Note: See TracChangeset for help on using the changeset viewer.