Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1975 in orxonox.OLD for orxonox/branches/chris/core/collision.cc


Ignore:
Timestamp:
Jun 18, 2004, 7:56:18 AM (21 years ago)
Author:
chris
Message:

orxonox/branches/chris: Implemented traceing (line-sphere collision)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/chris/core/collision.cc

    r1966 r1975  
    7474}
    7575
    76 bool check_trace (const Placement& pa, const CollisionCluster& a, unsigned long* ahitflags, const Line& trace, Vector* impactpoint)
    77 {
    78   return false;
     76bool check_trace (const Placement* pa, const CollisionCluster* a, unsigned long* ahitflags, const Line* trace, Vector* impactpoint)
     77{
     78        CC_Tree* t;
     79        if( (t = a->root) == NULL) return false;
     80       
     81  return cctree_trace( pa, t, ahitflags, trace, impactpoint);
    7982}
    8083
     
    9497}
    9598
    96 bool trace_sphere_collision( Vector m, float r, Line& l, Vector* impactpoint)
    97 {
    98   return false;
     99bool trace_sphere_collision( Vector m, float r, const Line* l, Vector* impactpoint)
     100{
     101  float A, B, C, D, t[2];
     102  Vector d = l->r - m;
     103  int i;
     104 
     105  A = l->a * l->a;
     106  B = 2 * (l->a * d);
     107  C = (d*d) - r*r;
     108  D = B*B - 4*A*C;
     109 
     110  if (D < 0) return false;
     111 
     112  t[0] = (-B+sqrt(D))/(2*A);
     113  t[1] = (-B-sqrt(D))/(2*A);
     114 
     115  if( (t[0] > 1 || t[0] < 0) && (t[1] < 0 || t[1] > 1)) return false;
     116  if( t[0] > t[1]) i = 0;
     117  else i = 1;
     118
     119  impactpoint->x = (l->r + (l->a * t[i])).x;
     120  impactpoint->y = (l->r + (l->a * t[i])).y;
     121  impactpoint->z = (l->r + (l->a * t[i])).z;
     122 
     123  return true;
    99124}
    100125
     
    237262  return 0;
    238263}
     264
     265bool cctree_trace( const Placement* p, CC_Tree* t, unsigned long* hitflags, const Line* trace, Vector* impactpoint)
     266{
     267  bool r = false;
     268  int i;
     269  Vector mr = p->r + rotate_vector( t->m, p->w);
     270  CC_Tree* use_t;
     271  Vector* ips;
     272  unsigned long* hfs;
     273 
     274  if( trace_sphere_collision (mr, t->r, trace, impactpoint))
     275  {
     276        if( t->n == 0)
     277        {
     278                setflag (hitflags, t->data.ID);
     279                return true;
     280        }
     281        else
     282        {
     283                ips = new Vector[t->n];
     284                hfs = new unsigned long[t->n];
     285                for (i = 0; i < t->n; i++) hfs[i] = 0;
     286                for (i = 0; i < t->n; i++)
     287                {
     288                        r = r || cctree_trace (p, t->data.b[i], &(hfs[i]), trace, &(ips[i]));
     289                }
     290                if( r)
     291                {
     292                        float kl = 0.0;
     293                        float l = 0.0;
     294                        int k = 0;
     295                        for (i = 0; i < t->n; i++)
     296                        {
     297                                if( (kl = (trace->r - ips[i]).len()) > l)
     298                                {
     299                                        l = kl;
     300                                        k = i;
     301                                }
     302                        }
     303                        impactpoint->x = ips[k].x;
     304                        impactpoint->y = ips[k].y;
     305                        impactpoint->z = ips[k].z;
     306                        *hitflags = hfs[k];
     307                }
     308                delete ips;
     309                delete hfs;
     310        }
     311        return r;
     312  }
     313 
     314  return false;
     315}
Note: See TracChangeset for help on using the changeset viewer.