[3954] | 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: |
---|
[3961] | 14 | main-programmer: Patrick Boenzli |
---|
[4375] | 15 | co-programmer: Benjamin Grauer |
---|
| 16 | |
---|
| 17 | bensch: renamed the file |
---|
[3954] | 18 | */ |
---|
| 19 | |
---|
[3961] | 20 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PHYSICS |
---|
[3954] | 21 | |
---|
[4375] | 22 | #include "physics_interface.h" |
---|
[4392] | 23 | #include "physics_engine.h" |
---|
[4376] | 24 | |
---|
[4377] | 25 | #include "field.h" |
---|
[4376] | 26 | #include "p_node.h" |
---|
| 27 | |
---|
[4121] | 28 | #include "list.h" |
---|
| 29 | #include "string.h" |
---|
[3961] | 30 | #include "stdincl.h" |
---|
[3954] | 31 | |
---|
| 32 | using namespace std; |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | \brief standard constructor |
---|
| 37 | */ |
---|
[4375] | 38 | PhysicsInterface::PhysicsInterface () |
---|
[3954] | 39 | { |
---|
[4376] | 40 | // this->setClassName ("PhysicsInterface"); |
---|
[4396] | 41 | this->mass = 1; |
---|
[4121] | 42 | this->massChildren = 0; |
---|
[4221] | 43 | this->forceSum = Vector(0, 0, 0); |
---|
[4392] | 44 | |
---|
| 45 | PhysicsEngine::getInstance()->addPhysicsInterface(this); |
---|
[3954] | 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | \brief standard deconstructor |
---|
| 51 | |
---|
| 52 | */ |
---|
[4375] | 53 | PhysicsInterface::~PhysicsInterface () |
---|
[3954] | 54 | { |
---|
[4392] | 55 | PhysicsEngine::getInstance()->removePhysicsInterface(this); |
---|
[3954] | 56 | } |
---|
[4121] | 57 | |
---|
[4376] | 58 | /** |
---|
| 59 | \brief recalculates the total mass of all the children of this node |
---|
| 60 | |
---|
| 61 | (only availiable for PNodes) |
---|
| 62 | */ |
---|
[4375] | 63 | void PhysicsInterface::recalcMass() |
---|
[4121] | 64 | { |
---|
[4376] | 65 | PNode* massCalcPNode = dynamic_cast<PNode*>(this); //! \todo not sure if this will work .... |
---|
| 66 | float massSum = 0; |
---|
| 67 | |
---|
| 68 | tIterator<PNode>* iterator = massCalcPNode->children->getIterator(); |
---|
| 69 | PNode* pn = iterator->nextElement(); |
---|
| 70 | while( pn != NULL) |
---|
[4121] | 71 | { |
---|
[4376] | 72 | // todo: find out if children are PhysicsInterface in an efficient way |
---|
| 73 | if (strcmp( pn->getClassName(), "PhysicsInterface")) { |
---|
| 74 | massSum += ((PhysicsInterface*)pn)->getTotalMass(); |
---|
| 75 | } |
---|
[4121] | 76 | pn = iterator->nextElement(); |
---|
| 77 | } |
---|
[4376] | 78 | delete iterator; |
---|
[4121] | 79 | |
---|
[4376] | 80 | if (massSum != this->massChildren ) { |
---|
| 81 | this->massChildren = massSum; |
---|
| 82 | if (strcmp( massCalcPNode->parent->getClassName(), "PhysicsInterface")) |
---|
| 83 | ((PhysicsInterface*)massCalcPNode->parent)->recalcMass(); |
---|
| 84 | } else { |
---|
| 85 | this->massChildren = massSum; |
---|
| 86 | } |
---|
[4121] | 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
[4395] | 90 | void PhysicsInterface::applyField(Field* field) |
---|
[4221] | 91 | { |
---|
[4396] | 92 | this->forceSum += field->calcForce(dynamic_cast<PNode*>(this)->getAbsCoor()); |
---|
[4221] | 93 | } |
---|
| 94 | |
---|
[4376] | 95 | void PhysicsInterface::tickPhys( float dt ) |
---|
[4221] | 96 | { |
---|
[4376] | 97 | Vector acc = this->forceSum / ( this->massChildren + this->mass ); |
---|
[4396] | 98 | PNode* coorTick = (PNode*) this; |
---|
| 99 | coorTick->setRelCoor(coorTick->getRelCoor() + (this->forceSum / this->mass * dt)); |
---|
| 100 | |
---|
| 101 | this->forceSum = Vector(0,0,0); |
---|
[4376] | 102 | // todo: introduce kinematics |
---|
[4221] | 103 | } |
---|