Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/collision_reaction/cr_physics_ground_walk.cc @ 9068

Last change on this file since 9068 was 9061, checked in by patrick, 18 years ago

merged the single_player branche to trunk

File size: 7.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION
17
18#include "collision.h"
19#include "collision_event.h"
20
21#include "physics_interface.h"
22
23#include "world_entity.h"
24#include "cr_physics_ground_walk.h"
25#include "collision_reaction.h"
26
27#include <vector>
28
29#include "debug.h"
30
31#include "aabb.h"
32
33#include "cr_defs.h"
34
35using namespace std;
36
37
38/**
39 *  standard constructor
40 */
41CRPhysicsGroundWalk::CRPhysicsGroundWalk ()
42    : CollisionReaction()
43{
44  this->setClassID(CL_CR_PHYSICS_GROUND_WALK, "CRPhysicsGroundWalk");
45}
46
47
48/**
49 *  standard deconstructor
50 */
51CRPhysicsGroundWalk::~CRPhysicsGroundWalk ()
52{}
53
54
55/**
56 * caluculates and applys the reaction to a specific collision
57 *  @param collision the collision
58 */
59void CRPhysicsGroundWalk::reactToCollision(Collision* collision)
60{
61
62  AABB* box = collision->getEntityB()->getModelAABB();
63  WorldEntity* entity = collision->getEntityB();
64
65  if( box == NULL)
66  {
67    PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n");
68    return;
69  }
70
71
72  float CR_MAX_WALK_HEIGHT = 2.0f;
73  float CR_THRESHOLD = 0.2f;
74
75  float height = 0;
76  float front = 0;
77  float side = 0;
78
79  //PRINTF(0)("collision raction======================================\n");
80
81  const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents());
82  std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin();
83  for(; it != collisionEvents->end(); it++)
84  {
85
86    CollisionEvent* ce = (*it);
87    Vector normal = ce->getGroundNormal();
88
89    // calculate the collision position
90    Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
91
92    // test the 3 axis differently
93    switch( ce->getType())
94    {
95        // collision in the x-axis
96      case COLLISION_TYPE_AXIS_X:
97        front = collPos.len() - box->halfLength[0]; // should be [0]
98
99        // object is beneath the plane (ground)
100        if( front <= 0.0f )
101        {
102          Vector dirX = entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize();
103          Vector backoff = dirX * front;
104         
105          entity->setAbsCoor(entity->getLastAbsCoor());
106         // entity->shiftCoor(backoff);
107        }
108        // object is already in the wall
109        else if( ce->isInWall())
110        {
111              entity->setAbsCoor(entity->getLastAbsCoor());
112        }
113        break;
114       
115      case COLLISION_TYPE_AXIS_X_NEG:
116        front = collPos.len() - box->halfLength[0]; // should be [0]
117
118        // object is beneath the plane (ground)
119        if( front <= 0.0f )
120        {
121          Vector dirX = entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize();
122          Vector backoff = dirX * front * -1.0f;
123         
124          entity->setAbsCoor(entity->getLastAbsCoor());
125         // entity->shiftCoor(backoff);
126        }
127        // object is already in the wall
128        else if( ce->isInWall())
129        {
130          entity->setAbsCoor(entity->getLastAbsCoor());
131        }
132        break;
133
134
135        // collision in the y-axis
136      case COLLISION_TYPE_AXIS_Y_NEG:
137        // calulate the height above ground
138        height = collPos.y - box->halfLength[1];
139
140
141        // object is beneath the plane (ground)
142        if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing
143        else if( height < 0.0f )
144        {
145          entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f));
146          entity->setOnGround(true);
147        }
148        // object is already in the wall
149        else if( ce->isInWall())
150        {
151          entity->setAbsCoor(entity->getLastAbsCoor());
152          PRINTF(0)("ground collision: reset pos\n");
153        }
154        else
155        {
156          // entity is not on ground
157          entity->setOnGround(false);
158        }
159        break;
160
161
162        // collision in the z-axis
163      case COLLISION_TYPE_AXIS_Z:
164
165        side = collPos.len()  - box->halfLength[2]; // should be [2]
166
167        // object is beneath the plane (ground)
168        if( side <= 0.0f )
169        {
170          entity->setAbsCoor(entity->getAbsCoor());
171          Vector dirZ = entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize();
172          Vector backoff = dirZ * side;
173          entity->shiftCoor(backoff);
174        }
175        // object is already in the wall
176        else if( ce->isInWall())
177        {
178          entity->setAbsCoor(entity->getLastAbsCoor());
179        }
180        break;
181       
182       
183         // collision in the z-axis
184      case COLLISION_TYPE_AXIS_Z_NEG:
185
186        side = collPos.len()  - box->halfLength[2]; // should be [2]
187
188        // object is beneath the plane (ground)
189        if( side <= 0.0f )
190        {
191         
192          Vector dirZ = entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize();
193          Vector backoff = dirZ * side*-1.0f;
194          entity->shiftCoor(backoff);
195        }
196        // object is already in the wall
197        else if( ce->isInWall())
198        {
199          entity->setAbsCoor(entity->getLastAbsCoor());
200        }
201        break;
202    }
203  }
204  //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
205
206
207
208
209
210
211#if 0
212  if( box != NULL)
213    height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ;
214  else
215    height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ;
216
217
218  if( box != NULL)
219  {
220
221
222    if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
223    {
224      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
225      return;
226    }
227    if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
228    {
229      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
230      return;
231    }
232
233    if(ce->getGroundNormal().len() <= 0.1f)
234    {
235      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
236      return;
237    }
238
239
240    if(ce->getGroundNormal().len() >= 1.4f)
241    {
242      downspeed++;
243      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
244      return;
245    }
246
247
248    if(height.y > box->halfLength[1] + 0.0f ) // Above ground
249    {
250      if(height.y < box->halfLength[1] + 2.3f) // Snap in
251      {
252        downspeed = 0;
253        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y  - box->halfLength[1] - 0.0f,0.0));
254      } else
255      {
256        downspeed++;
257        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
258      }
259
260    }
261    else
262    {
263      if(height.y <  box->halfLength[1] + 0.0f   /* && height.y  >  - 55.0f*/) // below ground
264      {
265        //if(downspeed <= 0) downspeed =1;
266        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y  +  box->halfLength[1] + 2.0f,0.0));
267        //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0));
268        downspeed = 0;
269      }
270
271    }
272
273  }// if(box!= NULL)
274#endif
275  /*
276  PRINTF(0)("Collision with Ground: \n");
277  collision->getEntityB()->getAbsCoor().debug();
278  collision->getEntityB()->setVelocity(Vector());
279  collision->getEntityB()->setAbsCoor(this->lastPositions[1]);
280
281  */
282
283}
284
285
286
287
288/**
289 * use this to do some collision offline calculations, only called for bContinuousPoll == true
290 */
291void CRPhysicsGroundWalk::update(WorldEntity* owner)
292{}
293
294
Note: See TracBrowser for help on using the repository browser.