| 1 |  | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 5 |  | 
|---|
| 6 |    Copyright (C) 2004 orx | 
|---|
| 7 |  | 
|---|
| 8 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 9 |    it under the terms of the GNU General Public License as published by | 
|---|
| 10 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 11 |    any later version. | 
|---|
| 12 |  | 
|---|
| 13 |    ### File Specific: | 
|---|
| 14 |    main-programmer: Patrick Boenzli | 
|---|
| 15 |    co-programmer: Benjamin Grauer | 
|---|
| 16 |  | 
|---|
| 17 |    bensch: renamed the file | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PHYSICS | 
|---|
| 21 |  | 
|---|
| 22 | #include "physics_interface.h" | 
|---|
| 23 | #include "physics_engine.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "fields/field.h" | 
|---|
| 26 | #include "p_node.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "string.h" | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | ObjectListDefinition(PhysicsInterface); | 
|---|
| 33 | /** | 
|---|
| 34 |  * @brief standard constructor | 
|---|
| 35 |  */ | 
|---|
| 36 | PhysicsInterface::PhysicsInterface () | 
|---|
| 37 | { | 
|---|
| 38 |   this->registerObject(this, PhysicsInterface::_objectList); | 
|---|
| 39 |  | 
|---|
| 40 |   this->mass = 1; | 
|---|
| 41 |   this->massChildren = 0; | 
|---|
| 42 |   this->forceSum = Vector(0, 0, 0); | 
|---|
| 43 |   this->bForceApplied = false; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 |  *  standard deconstructor | 
|---|
| 48 | */ | 
|---|
| 49 | PhysicsInterface::~PhysicsInterface () | 
|---|
| 50 | { | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | /** | 
|---|
| 54 |  *  recalculates the total mass of all the children of this node | 
|---|
| 55 |  | 
|---|
| 56 |    (only availiable for PNodes) | 
|---|
| 57 | */ | 
|---|
| 58 | void PhysicsInterface::recalcMass() | 
|---|
| 59 | { | 
|---|
| 60 |   /* | 
|---|
| 61 |     PNode* massCalcPNode = dynamic_cast<PNode*>(this);  //! @todo not sure if this will work .... | 
|---|
| 62 |     float massSum = 0; | 
|---|
| 63 |  | 
|---|
| 64 |     tIterator<PNode>* iterator = massCalcPNode->children->getIterator(); | 
|---|
| 65 |     PNode* pn = iterator->firstElement(); | 
|---|
| 66 |     while( pn != NULL) | 
|---|
| 67 |     { | 
|---|
| 68 |     // todo: find out if children are PhysicsInterface in an efficient way | 
|---|
| 69 |     if (strcmp( pn->getClassCName(), "PhysicsInterface")) { | 
|---|
| 70 |     massSum += ((PhysicsInterface*)pn)->getTotalMass(); | 
|---|
| 71 |     } | 
|---|
| 72 |     pn = iterator->nextElement(); | 
|---|
| 73 |     } | 
|---|
| 74 |     delete iterator; | 
|---|
| 75 |  | 
|---|
| 76 |     if (massSum != this->massChildren ) { | 
|---|
| 77 |     this->massChildren = massSum; | 
|---|
| 78 |     if (strcmp( massCalcPNode->parent->getClassCName(), "PhysicsInterface")) | 
|---|
| 79 |     ((PhysicsInterface*)massCalcPNode->parent)->recalcMass(); | 
|---|
| 80 |     } else { | 
|---|
| 81 |     this->massChildren = massSum; | 
|---|
| 82 |     } | 
|---|
| 83 |   */ | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | /** | 
|---|
| 87 |  *  applyes a field to this Object | 
|---|
| 88 |  * @param field the field to apply | 
|---|
| 89 |  * | 
|---|
| 90 |  * this function is virtual, and !must be reimplemented if the Member is !NOT! a PNode | 
|---|
| 91 | */ | 
|---|
| 92 | void PhysicsInterface::applyField(Field* field) | 
|---|
| 93 | { | 
|---|
| 94 |   PNode* tmp = dynamic_cast<PNode*>((BaseObject*)this); | 
|---|
| 95 |   this->forceSum += field->calcForce(tmp->getAbsCoor()); | 
|---|
| 96 |   this->bForceApplied = true; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void PhysicsInterface::applyForce(const Vector& force) | 
|---|
| 100 | { | 
|---|
| 101 |   this->forceSum += force; | 
|---|
| 102 |   this->bForceApplied = true; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | /** | 
|---|
| 106 |  *  ticks the PhysicsEffect | 
|---|
| 107 |  * @param dt: the value about which to tick | 
|---|
| 108 | */ | 
|---|
| 109 | void PhysicsInterface::tickPhys( float dt ) | 
|---|
| 110 | { | 
|---|
| 111 |   //  Vector acc = this->forceSum / ( this->massChildren + this->mass ); | 
|---|
| 112 |   PNode* coorTick =  dynamic_cast<PNode*>((BaseObject*)this); | 
|---|
| 113 |   if (coorTick != NULL ) | 
|---|
| 114 |     coorTick->shiftCoor((coorTick->getVelocity()+ this->forceSum/this->mass*dt)*dt); | 
|---|
| 115 |  | 
|---|
| 116 |   if (this->bForceApplied) | 
|---|
| 117 |   { | 
|---|
| 118 |     this->bForceApplied = false; | 
|---|
| 119 |     this->forceSum = Vector(0,0,0); | 
|---|
| 120 |   } | 
|---|
| 121 | } | 
|---|