Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9048 was 9027, checked in by patrick, 19 years ago

npc does not yet move again. strange

File size: 6.1 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.x - box->halfLength[0]; // should be [0]
98
99        // object is beneath the plane (ground)
100        if( front <= 0.0f )
101        {
102          Vector backoff = entity->getAbsDirX() * front;
103//           entity->shiftCoor(backoff);
104        }
105        // object is already in the wall
106        else if( ce->isInWall())
107        {
108//           entity->setAbsCoor(entity->getLastAbsCoor());
109        }
110        break;
111
112
113        // collision in the y-axis
114      case COLLISION_TYPE_AXIS_Y:
115        // calulate the height above ground
116        height = collPos.y - box->halfLength[1];
117
118
119        // object is beneath the plane (ground)
120        if( height <= 0.0f )
121        {
122          entity->shiftCoor(Vector(0.0f, -height, 0.0f));
123          entity->setOnGround(true);
124        }
125        // object is already in the wall
126        else if( ce->isInWall())
127        {
128          entity->setAbsCoor(entity->getLastAbsCoor());
129          PRINTF(0)("ground collision: reset pos\n");
130        }
131        else
132        {
133          // entity is not on ground
134          entity->setOnGround(false);
135        }
136        break;
137
138
139        // collision in the z-axis
140      case COLLISION_TYPE_AXIS_Z:
141
142        side = collPos.z - box->halfLength[2]; // should be [2]
143
144        // object is beneath the plane (ground)
145        if( side <= 0.0f )
146        {
147          Vector backoff = entity->getAbsDirX() * side;
148//           entity->shiftCoor(backoff);
149        }
150        // object is already in the wall
151        else if( ce->isInWall())
152        {
153//           entity->setAbsCoor(entity->getLastAbsCoor());
154        }
155        break;
156    }
157  }
158  //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
159
160
161
162
163
164
165#if 0
166  if( box != NULL)
167    height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ;
168  else
169    height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ;
170
171
172  if( box != NULL)
173  {
174
175
176    if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
177    {
178      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
179      return;
180    }
181    if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
182    {
183      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
184      return;
185    }
186
187    if(ce->getGroundNormal().len() <= 0.1f)
188    {
189      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
190      return;
191    }
192
193
194    if(ce->getGroundNormal().len() >= 1.4f)
195    {
196      downspeed++;
197      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
198      return;
199    }
200
201
202    if(height.y > box->halfLength[1] + 0.0f ) // Above ground
203    {
204      if(height.y < box->halfLength[1] + 2.3f) // Snap in
205      {
206        downspeed = 0;
207        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y  - box->halfLength[1] - 0.0f,0.0));
208      } else
209      {
210        downspeed++;
211        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
212      }
213
214    }
215    else
216    {
217      if(height.y <  box->halfLength[1] + 0.0f   /* && height.y  >  - 55.0f*/) // below ground
218      {
219        //if(downspeed <= 0) downspeed =1;
220        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y  +  box->halfLength[1] + 2.0f,0.0));
221        //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0));
222        downspeed = 0;
223      }
224
225    }
226
227  }// if(box!= NULL)
228#endif
229  /*
230  PRINTF(0)("Collision with Ground: \n");
231  collision->getEntityB()->getAbsCoor().debug();
232  collision->getEntityB()->setVelocity(Vector());
233  collision->getEntityB()->setAbsCoor(this->lastPositions[1]);
234
235  */
236
237}
238
239
240
241
242/**
243 * use this to do some collision offline calculations, only called for bContinuousPoll == true
244 */
245void CRPhysicsGroundWalk::update(WorldEntity* owner)
246{}
247
248
Note: See TracBrowser for help on using the repository browser.