Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the branche single_player_map with the trunk

File size: 4.8 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
26#include <vector>
27
28#include "debug.h"
29
30#include "aabb.h"
31
32#include "cr_defs.h"
33
34using namespace std;
35
36
37/**
38 *  standard constructor
39 */
40CRPhysicsGroundWalk::CRPhysicsGroundWalk ()
41    : CollisionReaction()
42{
43  this->setClassID(CL_CR_PHYSICS_GROUND_WALK, "CRPhysicsGroundWalk");
44}
45
46
47/**
48 *  standard deconstructor
49 */
50CRPhysicsGroundWalk::~CRPhysicsGroundWalk ()
51{}
52
53
54/**
55 * caluculates and applys the reaction to a specific collision
56 *  @param collision the collision
57 */
58void CRPhysicsGroundWalk::reactToCollision(Collision* collision)
59{
60  CollisionEvent* ce = collision->getCollisionEvents().front();
61  Vector normal = ce->getGroundNormal();
62  // normal.normalize();
63
64  // put it back
65  //   PRINTF(0)("putting it back to lastPos: \n");
66  //   this->lastPositions[0].debug();
67  //   PRINTF(0)("current pos:\n");
68  //   collision->getEntityB()->getAbsCoor().debug();
69
70  float height;
71  AABB* box = collision->getEntityB()->getModelAABB();
72  WorldEntity* entity = collision->getEntityB();
73
74  // collision position maths
75  Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
76
77  float CR_MAX_WALK_HEIGHT = 2.0f;
78  float CR_THRESHOLD = 0.2f;
79
80  if( box == NULL)
81  {
82    PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n");
83    return;
84  }
85
86
87  switch( ce->getType())
88  {
89    case COLLISION_TYPE_AXIS_Y:
90
91      height = collPos.y - box->halfLength[1];
92     // PRINTF(0)("height: %f          , model height: %f\n", height, box->halfLength[1]);
93     // PRINTF(0)(" ground normal: %f, %f, %f\n", normal.x, normal.y, normal.z);
94
95      // object is beneath the plane (ground)
96      if( height <= 0.0f )
97      {
98        entity->shiftCoor(Vector(0, -height, 0));
99      }
100      // object is already in the wall
101      else if( ce->isInWall())
102      {
103        entity->setAbsCoor(entity->getLastAbsCoor());
104      }
105      break;
106
107
108    case COLLISION_TYPE_AXIS_X:
109    case COLLISION_TYPE_AXIS_Z:
110      break;
111
112    }
113
114
115
116
117
118
119
120
121#if 0
122  if( box != NULL)
123    height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ;
124  else
125    height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ;
126
127
128  if( box != NULL) {
129
130
131    if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) {
132      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
133      return;
134    }
135    if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) {
136      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
137      return;
138    }
139
140    if(ce->getGroundNormal().len() <= 0.1f) {
141      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
142      return;
143    }
144
145
146    if(ce->getGroundNormal().len() >= 1.4f) {
147      downspeed++;
148      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
149      return;
150    }
151
152
153    if(height.y > box->halfLength[1] + 0.0f ) // Above ground
154    {
155      if(height.y < box->halfLength[1] + 2.3f) // Snap in
156      {
157        downspeed = 0;
158        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y  - box->halfLength[1] - 0.0f,0.0));
159      } else
160      {
161        downspeed++;
162        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
163      }
164
165    }
166    else {
167      if(height.y <  box->halfLength[1] + 0.0f   /* && height.y  >  - 55.0f*/) // below ground
168      {
169        //if(downspeed <= 0) downspeed =1;
170        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y  +  box->halfLength[1] + 2.0f,0.0));
171        //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0));
172        downspeed = 0;
173      }
174
175    }
176
177  }// if(box!= NULL)
178#endif
179  /*
180  PRINTF(0)("Collision with Ground: \n");
181  collision->getEntityB()->getAbsCoor().debug();
182  collision->getEntityB()->setVelocity(Vector());
183  collision->getEntityB()->setAbsCoor(this->lastPositions[1]);
184
185  */
186
187}
188
189
190
191
192/**
193 * use this to do some collision offline calculations, only called for bContinuousPoll == true
194 */
195void CRPhysicsGroundWalk::update(WorldEntity* owner)
196{
197
198}
199
200
Note: See TracBrowser for help on using the repository browser.