| 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 "field.h" | 
|---|
| 26 | #include "p_node.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "list.h" | 
|---|
| 29 | #include "string.h" | 
|---|
| 30 | #include "stdincl.h" | 
|---|
| 31 |  | 
|---|
| 32 | using namespace std; | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 |  *  standard constructor | 
|---|
| 37 |  */ | 
|---|
| 38 | PhysicsInterface::PhysicsInterface () | 
|---|
| 39 | { | 
|---|
| 40 |   this->setClassID(CL_PHYSICS_INTERFACE, "PhysicsInterface"); | 
|---|
| 41 |  | 
|---|
| 42 |   this->mass = 1; | 
|---|
| 43 |   this->massChildren = 0; | 
|---|
| 44 |   this->forceSum = Vector(0, 0, 0); | 
|---|
| 45 |   this->bForceApplied = false; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /** | 
|---|
| 49 |  *  standard deconstructor | 
|---|
| 50 | */ | 
|---|
| 51 | PhysicsInterface::~PhysicsInterface () | 
|---|
| 52 | { | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | /** | 
|---|
| 56 |  *  recalculates the total mass of all the children of this node | 
|---|
| 57 |  | 
|---|
| 58 |    (only availiable for PNodes) | 
|---|
| 59 | */ | 
|---|
| 60 | void PhysicsInterface::recalcMass() | 
|---|
| 61 | { | 
|---|
| 62 |   /* | 
|---|
| 63 |     PNode* massCalcPNode = dynamic_cast<PNode*>(this);  //! @todo not sure if this will work .... | 
|---|
| 64 |     float massSum = 0; | 
|---|
| 65 |  | 
|---|
| 66 |     tIterator<PNode>* iterator = massCalcPNode->children->getIterator(); | 
|---|
| 67 |     PNode* pn = iterator->firstElement(); | 
|---|
| 68 |     while( pn != NULL) | 
|---|
| 69 |     { | 
|---|
| 70 |     // todo: find out if children are PhysicsInterface in an efficient way | 
|---|
| 71 |     if (strcmp( pn->getClassName(), "PhysicsInterface")) { | 
|---|
| 72 |     massSum += ((PhysicsInterface*)pn)->getTotalMass(); | 
|---|
| 73 |     } | 
|---|
| 74 |     pn = iterator->nextElement(); | 
|---|
| 75 |     } | 
|---|
| 76 |     delete iterator; | 
|---|
| 77 |  | 
|---|
| 78 |     if (massSum != this->massChildren ) { | 
|---|
| 79 |     this->massChildren = massSum; | 
|---|
| 80 |     if (strcmp( massCalcPNode->parent->getClassName(), "PhysicsInterface")) | 
|---|
| 81 |     ((PhysicsInterface*)massCalcPNode->parent)->recalcMass(); | 
|---|
| 82 |     } else { | 
|---|
| 83 |     this->massChildren = massSum; | 
|---|
| 84 |     } | 
|---|
| 85 |   */ | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | /** | 
|---|
| 89 |  *  applyes a field to this Object | 
|---|
| 90 |  * @param field the field to apply | 
|---|
| 91 |  * | 
|---|
| 92 |  * this function is virtual, and !must be reimplemented if the Member is !NOT! a PNode | 
|---|
| 93 | */ | 
|---|
| 94 | void PhysicsInterface::applyField(Field* field) | 
|---|
| 95 | { | 
|---|
| 96 |   PNode* tmp = dynamic_cast<PNode*>((BaseObject*)this); | 
|---|
| 97 |   this->forceSum += field->calcForce(tmp->getAbsCoor()); | 
|---|
| 98 |   this->bForceApplied = true; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void PhysicsInterface::applyForce(const Vector& force) | 
|---|
| 102 | { | 
|---|
| 103 |   PNode* tmp = dynamic_cast<PNode*>((BaseObject*)this); | 
|---|
| 104 |   this->forceSum += force; | 
|---|
| 105 |   this->bForceApplied = true; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | /** | 
|---|
| 109 |  *  ticks the PhysicsEffect | 
|---|
| 110 |  * @param dt: the value about which to tick | 
|---|
| 111 | */ | 
|---|
| 112 | void PhysicsInterface::tickPhys( float dt ) | 
|---|
| 113 | { | 
|---|
| 114 |   //  Vector acc = this->forceSum / ( this->massChildren + this->mass ); | 
|---|
| 115 |   PNode* coorTick =  dynamic_cast<PNode*>((BaseObject*)this); | 
|---|
| 116 |   if (coorTick != NULL ) | 
|---|
| 117 |     coorTick->shiftCoor((coorTick->getVelocity()+ this->forceSum/this->mass*dt)*dt); | 
|---|
| 118 |  | 
|---|
| 119 |   if (this->bForceApplied) | 
|---|
| 120 |   { | 
|---|
| 121 |     this->bForceApplied = false; | 
|---|
| 122 |     this->forceSum = Vector(0,0,0); | 
|---|
| 123 |   } | 
|---|
| 124 | } | 
|---|