Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

repair station work, collision reaction

File size: 6.1 KB
RevLine 
[8200]1/*
2   orxonox - the future of 3D-vertical-scrollers
[8894]3
[8200]4   Copyright (C) 2004 orx
[8894]5
[8200]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.
[8894]10
[8200]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"
[8907]25#include "collision_reaction.h"
[8200]26
[8288]27#include <vector>
28
[8894]29#include "debug.h"
30
[8796]31#include "aabb.h"
32
[8894]33#include "cr_defs.h"
34
[8200]35using namespace std;
36
37
38/**
39 *  standard constructor
40 */
41CRPhysicsGroundWalk::CRPhysicsGroundWalk ()
[8724]42    : CollisionReaction()
[8200]43{
[8203]44  this->setClassID(CL_CR_PHYSICS_GROUND_WALK, "CRPhysicsGroundWalk");
[8200]45}
46
47
48/**
49 *  standard deconstructor
50 */
51CRPhysicsGroundWalk::~CRPhysicsGroundWalk ()
[8724]52{}
[8200]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{
[8288]61
[8796]62  AABB* box = collision->getEntityB()->getModelAABB();
[8894]63  WorldEntity* entity = collision->getEntityB();
64
[8907]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  }
[8922]70
71
[8894]72  float CR_MAX_WALK_HEIGHT = 2.0f;
73  float CR_THRESHOLD = 0.2f;
74
[8928]75  float height = 0;
76  float front = 0;
77  float side = 0;
[8894]78
[8925]79  PRINTF(0)("collision raction======================================\n");
[8922]80
[8924]81  const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents());
82  std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin();
83  for(; it != collisionEvents->end(); it++)
84  {
[8922]85
[8924]86    CollisionEvent* ce = (*it);
87    Vector normal = ce->getGroundNormal();
[8894]88
[8927]89    // calculate the collision position
[8924]90    Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
[8922]91
[8894]92
[8924]93    // test the 3 axis differently
94    switch( ce->getType())
95    {
96        // collision in the x-axis
97      case COLLISION_TYPE_AXIS_X:
98        front = collPos.x - box->halfLength[0];
[8922]99
[8927]100//         PRINTF(0)("front: %f\n", front);
101//         PRINTF(0)("in wall %i\n", ce->isInWall());
[8922]102
[8924]103        // object is beneath the plane (ground)
104        if( front <= 0.0f )
105        {
106//           entity->shiftCoor(Vector(front, 0.0f, 0.0f));
107        }
108        // object is already in the wall
109        else if( ce->isInWall())
110        {
111//           entity->setAbsCoor(entity->getLastAbsCoor());
112        }
113        break;
[8922]114
[8894]115
[8924]116        // collision in the y-axis
117      case COLLISION_TYPE_AXIS_Y:
118        // calulate the height above ground
119        height = collPos.y - box->halfLength[1];
[8894]120
[8927]121//         PRINTF(0)("height: %f\n", height);
122//         PRINTF(0)("in wall %i\n", ce->isInWall());
[8894]123
[8924]124        // object is beneath the plane (ground)
125        if( height <= 0.0f )
126        {
127          entity->shiftCoor(Vector(0.0f, -height, 0.0f));
128        }
129        // object is already in the wall
130        else if( ce->isInWall())
131        {
[8930]132         // entity->setAbsCoor(entity->getLastAbsCoor());
[8924]133        }
134        break;
[8894]135
136
[8924]137        // collision in the z-axis
138      case COLLISION_TYPE_AXIS_Z:
[8925]139
140        side = collPos.z - box->halfLength[2];
141
[8927]142//         PRINTF(0)("side: %f\n", side);
143//         PRINTF(0)("in wall %i\n", ce->isInWall());
[8925]144
145        // object is beneath the plane (ground)
146        if( side <= 0.0f )
147        {
148//           entity->shiftCoor(Vector(front, 0.0f, 0.0f));
149        }
150        // object is already in the wall
151        else if( ce->isInWall())
152        {
153//           entity->setAbsCoor(entity->getLastAbsCoor());
154        }
[8924]155        break;
156    }
157  }
[8931]158  PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
[8894]159
160
161
162
[8922]163
[8924]164
[8894]165#if 0
166  if( box != NULL)
[8796]167    height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ;
168  else
169    height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ;
[8724]170
171
[8922]172  if( box != NULL)
173  {
[8724]174
[8894]175
[8922]176    if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
177    {
[8796]178      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
179      return;
180    }
[8922]181    if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f)
182    {
[8796]183      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
184      return;
185    }
[8724]186
[8922]187    if(ce->getGroundNormal().len() <= 0.1f)
188    {
[8796]189      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
190      return;
191    }
[8894]192
193
[8922]194    if(ce->getGroundNormal().len() >= 1.4f)
195    {
[8724]196      downspeed++;
197      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
[8796]198      return;
[8724]199    }
200
[8796]201
202    if(height.y > box->halfLength[1] + 0.0f ) // Above ground
[8724]203    {
[8894]204      if(height.y < box->halfLength[1] + 2.3f) // Snap in
[8796]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
[8724]214    }
[8922]215    else
216    {
[8796]217      if(height.y <  box->halfLength[1] + 0.0f   /* && height.y  >  - 55.0f*/) // below ground
218      {
219        //if(downspeed <= 0) downspeed =1;
[8894]220        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y  +  box->halfLength[1] + 2.0f,0.0));
[8796]221        //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0));
222        downspeed = 0;
223      }
[8724]224
[8796]225    }
[8724]226
[8796]227  }// if(box!= NULL)
[8894]228#endif
[8724]229  /*
[8341]230  PRINTF(0)("Collision with Ground: \n");
231  collision->getEntityB()->getAbsCoor().debug();
[8724]232  collision->getEntityB()->setVelocity(Vector());
233  collision->getEntityB()->setAbsCoor(this->lastPositions[1]);
[8341]234
[8724]235  */
[8894]236
[8256]237}
[8200]238
[8256]239
240
[8724]241
[8256]242/**
243 * use this to do some collision offline calculations, only called for bContinuousPoll == true
244 */
245void CRPhysicsGroundWalk::update(WorldEntity* owner)
[8924]246{}
[8200]247
[8256]248
Note: See TracBrowser for help on using the repository browser.