Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/lib/collision_reaction/cr_physics_ground_walk.cc @ 8922

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

the ground walking algorithm is almost working again

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#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  CollisionEvent* ce = collision->getCollisionEvents().front();
62  Vector normal = ce->getGroundNormal();
63  // normal.normalize();
64
65
66  float height;
67  AABB* box = collision->getEntityB()->getModelAABB();
68  WorldEntity* entity = collision->getEntityB();
69
70  if( box == NULL)
71  {
72    PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n");
73    return;
74  }
75
76
77  // collision position maths
78  Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
79
80  float CR_MAX_WALK_HEIGHT = 2.0f;
81  float CR_THRESHOLD = 0.2f;
82
83  //height = collPos.y - box->halfLength[1];
84  //PRINTF(0)("height: %f          , model height: %f\n", height, box->halfLength[1]);
85  // PRINTF(0)(" ground normal: %f, %f, %f\n", normal.x, normal.y, normal.z);
86  //
87
88  switch( ce->getType())
89  {
90
91    case COLLISION_TYPE_AXIS_X:
92
93      break;
94
95    case COLLISION_TYPE_AXIS_Y:
96
97      height = collPos.y - box->halfLength[1];
98
99      PRINTF(0)("height: %f\n", height);
100      PRINTF(0)("in wall %i\n", ce->isInWall());
101
102      // object is beneath the plane (ground)
103      if( height <= 0.0f )
104      {
105        entity->shiftCoor(Vector(0, -height, 0));
106      }
107      // object is already in the wall
108      else if( ce->isInWall())
109      {
110        entity->setAbsCoor(entity->getLastAbsCoor());
111      }
112      break;
113      break;
114
115
116    case COLLISION_TYPE_AXIS_Z:
117
118      break;
119
120  }
121
122
123
124
125
126
127
128
129#if 0
130  if( box != NULL)
131    height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ;
132  else
133    height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ;
134
135
136  if( box != NULL)
137  {
138
139
140    if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
141    {
142      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
143      return;
144    }
145    if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
146    {
147      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
148      return;
149    }
150
151    if(ce->getGroundNormal().len() <= 0.1f)
152    {
153      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
154      return;
155    }
156
157
158    if(ce->getGroundNormal().len() >= 1.4f)
159    {
160      downspeed++;
161      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
162      return;
163    }
164
165
166    if(height.y > box->halfLength[1] + 0.0f ) // Above ground
167    {
168      if(height.y < box->halfLength[1] + 2.3f) // Snap in
169      {
170        downspeed = 0;
171        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y  - box->halfLength[1] - 0.0f,0.0));
172      } else
173      {
174        downspeed++;
175        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
176      }
177
178    }
179    else
180    {
181      if(height.y <  box->halfLength[1] + 0.0f   /* && height.y  >  - 55.0f*/) // below ground
182      {
183        //if(downspeed <= 0) downspeed =1;
184        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y  +  box->halfLength[1] + 2.0f,0.0));
185        //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0));
186        downspeed = 0;
187      }
188
189    }
190
191  }// if(box!= NULL)
192#endif
193  /*
194  PRINTF(0)("Collision with Ground: \n");
195  collision->getEntityB()->getAbsCoor().debug();
196  collision->getEntityB()->setVelocity(Vector());
197  collision->getEntityB()->setAbsCoor(this->lastPositions[1]);
198
199  */
200
201}
202
203
204
205
206/**
207 * use this to do some collision offline calculations, only called for bContinuousPoll == true
208 */
209void CRPhysicsGroundWalk::update(WorldEntity* owner)
210{
211}
212
213
Note: See TracBrowser for help on using the repository browser.