Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/lib/collision_reaction/cr_physics_ground_walk.cc @ 9096

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

collision reaction works again

File size: 5.4 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 = 10.0f;
73  float CR_THRESHOLD = 0.2f;
74
75  float height = 0;
76  float front = 0;
77  float side = 0;
78
79
80  const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents());
81  std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin();
82  for(; it != collisionEvents->end(); it++)
83  {
84
85    CollisionEvent* ce = (*it);
86    Vector normal = ce->getGroundNormal();
87
88    // calculate the collision position
89    Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
90
91    // test the 3 axis differently
92    switch( ce->getType())
93    {
94        // collision in the x-axis
95      case COLLISION_TYPE_AXIS_X:
96        front = collPos.len() - box->halfLength[0]; // should be [0]
97
98        // object is beneath the plane (ground)
99        if( front <= 0.0f )
100        {
101          Vector dirX = entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize();
102          Vector backoff = dirX * front;
103
104          entity->setAbsCoor(entity->getLastAbsCoor());
105         // entity->shiftCoor(backoff);
106        }
107        // object is already in the wall
108        else if( ce->isInWall())
109        {
110              entity->setAbsCoor(entity->getLastAbsCoor());
111        }
112        break;
113
114      case COLLISION_TYPE_AXIS_X_NEG:
115        front = collPos.len() - box->halfLength[0]; // should be [0]
116
117        // object is beneath the plane (ground)
118        if( front <= 0.0f)
119        {
120          Vector dirX = entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize();
121          Vector backoff = dirX * front;
122
123          entity->setAbsCoor(entity->getLastAbsCoor());
124         // entity->shiftCoor(backoff);
125        }
126        // object is already in the wall
127        else if( ce->isInWall())
128        {
129          entity->setAbsCoor(entity->getLastAbsCoor());
130        }
131        break;
132
133
134        // collision in the y-axis
135      case COLLISION_TYPE_AXIS_Y_NEG:
136        // calulate the height above ground
137        height = collPos.y - box->halfLength[1];
138
139
140        // object is beneath the plane (ground)
141//         if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing
142        if( height < 0.0f /*&& height < -CR_MAX_WALK_HEIGHT*/)
143        {
144          entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f));
145          entity->setOnGround(true);
146        }
147        // object is already in the wall
148        else if( ce->isInWall())
149        {
150          entity->setAbsCoor(entity->getLastAbsCoor());
151          PRINTF(0)("ground collision: reset pos\n");
152        }
153        else
154        {
155          // entity is not on ground
156          entity->setOnGround(false);
157        }
158        break;
159
160
161        // collision in the z-axis
162      case COLLISION_TYPE_AXIS_Z:
163
164        side = collPos.len()  - box->halfLength[2]; // should be [2]
165
166        // object is beneath the plane (ground)
167        if( side <= 0.0f )
168        {
169          entity->setAbsCoor(entity->getAbsCoor());
170          Vector dirZ = entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize();
171          Vector backoff = dirZ * side;
172          entity->shiftCoor(backoff);
173        }
174        // object is already in the wall
175        else if( ce->isInWall())
176        {
177          entity->setAbsCoor(entity->getLastAbsCoor());
178        }
179        break;
180
181
182         // collision in the z-axis
183      case COLLISION_TYPE_AXIS_Z_NEG:
184
185        side = collPos.len()  - box->halfLength[2]; // should be [2]
186
187        // object is beneath the plane (ground)
188        if( side <= 0.0f )
189        {
190
191          Vector dirZ = entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize();
192          Vector backoff = dirZ * side*-1.0f;
193          entity->shiftCoor(backoff);
194        }
195        // object is already in the wall
196        else if( ce->isInWall())
197        {
198          entity->setAbsCoor(entity->getLastAbsCoor());
199        }
200        break;
201    }
202  }
203  //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
204
205
206
207
208
209
210
211}
212
213
214
215
216/**
217 * use this to do some collision offline calculations, only called for bContinuousPoll == true
218 */
219void CRPhysicsGroundWalk::update(WorldEntity* owner)
220{}
221
222
Note: See TracBrowser for help on using the repository browser.