source: golgotha/src/golg/objs/model_collide.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.7 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "objs/model_collide.hh"
10#include "math/transform.hh"
11#include "g1_object.hh"
12#include "objs/model_id.hh"
13#include "math/pi.hh"
14#include "math/angle.hh"
15
16i4_bool g1_model_collide_polygonal(g1_object_class *_this,
17                                   g1_model_draw_parameters &params,
18                                   const i4_3d_vector &start,
19                                   i4_3d_vector &ray,
20                                   i4_3d_vector &hit_normal,
21                                   i4_float *minimum_t)
22
23  i4_transform_class transform, mini_local;
24  i4_3d_vector istart, iray, normal;
25  i4_float t, min_t=1.0;
26  int poly;
27 
28  g1_quad_object_class *model = params.model;
29  if (!model) return i4_F;
30
31  _this->calc_world_transform(1.0, &transform);
32
33  transform.inverse_transform(start, istart);
34  transform.inverse_transform_3x3(ray, iray);
35
36  if (model->intersect(istart, iray, params.animation, params.frame, &t, &poly, &normal))
37    if (t<min_t)
38    {
39      min_t = t;
40      transform.transform_3x3(normal, hit_normal);
41    }
42
43  i4_3d_vector mstart, mray;
44  for (int i=0; i<_this->num_mini_objects; i++)
45  {
46    g1_mini_object *mini = &_this->mini_objects[i];
47
48    if (mini->defmodeltype!=0xffff)
49    {
50      mini->calc_transform(1.0, &mini_local);
51      mini_local.inverse_transform(istart, mstart);
52      mini_local.inverse_transform_3x3(iray, mray);
53      model = g1_model_list_man.get_model(mini->defmodeltype);
54      if (model->intersect(mstart, mray, mini->animation, mini->frame, &t, &poly, &normal))
55      {
56        if (t<min_t)
57        {
58          min_t = t;
59          i4_3d_vector local_norm;
60          mini_local.transform_3x3(normal, local_norm);
61          transform.transform_3x3(local_norm, hit_normal);
62        }
63      }
64    }
65  }
66
67  if (minimum_t)
68    *minimum_t = min_t;
69
70  if (min_t<1.0)
71  {
72    ray *= min_t;
73    return i4_T;
74  }
75  return i4_F;
76}
77
78i4_bool g1_model_collide_radial(g1_object_class *_this,
79                                g1_model_draw_parameters &params,
80                                const i4_3d_vector &start,
81                                i4_3d_vector &ray)
82{
83     
84  // intersection of a ray with a sphere from graphics gems 1  p. 388
85  i4_3d_vector EO, V, O;
86  float r, disc, EO_length, d, v;
87 
88  V=ray;
89  V.normalize();
90
91  r=_this->occupancy_radius(); 
92  float z_dist=_this->h - start.z;
93//   if (z_dist*2<r)        // fudge the z because some vehicles are too short
94//     z_dist=0;
95
96  EO=i4_3d_vector(_this->x - start.x, _this->y - start.y, z_dist);
97  v=EO.dot(V);
98
99  EO_length=EO.length();
100
101  disc=r*r - (EO_length*EO_length - v*v);
102 
103  if (disc<0)
104    return i4_F;
105  else
106  {
107    d=sqrt(disc);
108    V*=(v-d);
109    ray=V;
110    return i4_T;
111  }
112
113 
114//   i4_float a,b,c,d,r, gx,gy;
115
116//   gx = _this->x - start.x;
117//   gy = _this->y - start.y;
118//   d  = gx*gx + gy*gy;
119//   r  = _this->occupancy_radius();     
120//   r = r*r;
121
122//   //first bounding radius check
123//   if (d < r)
124//     return i4_T;
125     
126//   //then ray/circle intersection check
127//   b  = ray.x*ray.x + ray.y*ray.y;
128//   a  = ray.x*gx + ray.y*gy;
129//   c  = (a*a)/b;
130     
131//   return (r>d-c);
132}
Note: See TracBrowser for help on using the repository browser.