Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

spl: collision reaction similar as it was but doesn't stick to the ground yet

File size: 4.5 KB
RevLine 
[8200]1/*
2   orxonox - the future of 3D-vertical-scrollers
[8812]3
[8200]4   Copyright (C) 2004 orx
[8812]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.
[8812]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"
25
[8288]26#include <vector>
27
[8824]28#include "debug.h"
29
[8796]30#include "aabb.h"
31
[8200]32using namespace std;
33
34
35/**
36 *  standard constructor
37 */
38CRPhysicsGroundWalk::CRPhysicsGroundWalk ()
[8724]39    : CollisionReaction()
[8200]40{
[8203]41  this->setClassID(CL_CR_PHYSICS_GROUND_WALK, "CRPhysicsGroundWalk");
[8200]42}
43
44
45/**
46 *  standard deconstructor
47 */
48CRPhysicsGroundWalk::~CRPhysicsGroundWalk ()
[8724]49{}
[8200]50
51
52/**
53 * caluculates and applys the reaction to a specific collision
54 *  @param collision the collision
55 */
56void CRPhysicsGroundWalk::reactToCollision(Collision* collision)
57{
[8334]58  CollisionEvent* ce = collision->getCollisionEvents().front();
59  Vector normal = ce->getGroundNormal();
[8724]60  // normal.normalize();
[8288]61
62  // put it back
[8724]63  //   PRINTF(0)("putting it back to lastPos: \n");
64  //   this->lastPositions[0].debug();
65  //   PRINTF(0)("current pos:\n");
66  //   collision->getEntityB()->getAbsCoor().debug();
[8323]67
[8836]68  float height;
[8796]69  AABB* box = collision->getEntityB()->getModelAABB();
[8836]70  WorldEntity* entity = collision->getEntityB();
[8812]71
[8836]72  float CR_MAX_WALK_HEIGHT = 2.0f;
73
[8834]74  if( box != NULL)
75  {
[8812]76
[8834]77    Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
[8836]78    height = collPos.y - box->halfLength[1];
[8834]79
[8836]80    PRINTF(0)("height: %f          , model height: %f\n", height, box->halfLength[1]);
81    PRINTF(0)(" ground normal: %f, %f, %f\n", normal.x, normal.y, normal.z);
82
83    // object is beneath the plane (ground)
84    if( height < 0.0f)
85    {
86      entity->shiftCoor(Vector(0,-height,0));
87    }
88
89
[8834]90  }
91
[8836]92
93
94
95
[8834]96#if 0
[8824]97  if( box != NULL)
[8796]98    height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ;
99  else
100    height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ;
[8724]101
102
[8824]103  if( box != NULL) {
[8724]104
[8824]105
[8796]106    if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) {
107      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
108      return;
109    }
110    if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) {
111      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
112      return;
113    }
[8724]114
[8796]115    if(ce->getGroundNormal().len() <= 0.1f) {
116      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor());
117      return;
118    }
[8812]119
120
[8796]121    if(ce->getGroundNormal().len() >= 1.4f) {
[8724]122      downspeed++;
123      collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
[8796]124      return;
[8724]125    }
126
[8796]127
128    if(height.y > box->halfLength[1] + 0.0f ) // Above ground
[8724]129    {
[8812]130      if(height.y < box->halfLength[1] + 2.3f) // Snap in
[8796]131      {
132        downspeed = 0;
133        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y  - box->halfLength[1] - 0.0f,0.0));
134      } else
135      {
136        downspeed++;
137        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0));
138      }
139
[8724]140    }
[8796]141    else {
142      if(height.y <  box->halfLength[1] + 0.0f   /* && height.y  >  - 55.0f*/) // below ground
143      {
144        //if(downspeed <= 0) downspeed =1;
[8836]145        collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y  +  box->halfLength[1] + 2.0f,0.0));
[8796]146        //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0));
147        downspeed = 0;
148      }
[8724]149
[8796]150    }
[8724]151
[8796]152  }// if(box!= NULL)
[8834]153#endif
[8724]154  /*
[8341]155  PRINTF(0)("Collision with Ground: \n");
156  collision->getEntityB()->getAbsCoor().debug();
[8724]157  collision->getEntityB()->setVelocity(Vector());
158  collision->getEntityB()->setAbsCoor(this->lastPositions[1]);
[8341]159
[8724]160  */
[8812]161
[8256]162}
[8200]163
[8256]164
165
[8724]166
[8256]167/**
168 * use this to do some collision offline calculations, only called for bContinuousPoll == true
169 */
170void CRPhysicsGroundWalk::update(WorldEntity* owner)
171{
[8337]172  for( int i = 9; i > 0; i--) {
[8336]173    this->lastPositions[i] = this->lastPositions[i-1];
[8724]174    //     PRINTF(0)("lastPosition[%i]: %f, %f, %f\n", i, lastPositions[i].x, lastPositions[i].y, lastPositions[i].z);
[8337]175  }
[8336]176  this->lastPositions[0] = owner->getAbsCoor();
[8200]177}
178
[8256]179
Note: See TracBrowser for help on using the repository browser.