[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 | */ |
---|
[4397] | 38 | PhysicsInterface::PhysicsInterface (void* objectPointer) |
---|
[3954] | 39 | { |
---|
[4397] | 40 | this->objectPointer = objectPointer; |
---|
| 41 | |
---|
[4376] | 42 | // this->setClassName ("PhysicsInterface"); |
---|
[4396] | 43 | this->mass = 1; |
---|
[4121] | 44 | this->massChildren = 0; |
---|
[4221] | 45 | this->forceSum = Vector(0, 0, 0); |
---|
[4392] | 46 | |
---|
| 47 | PhysicsEngine::getInstance()->addPhysicsInterface(this); |
---|
[3954] | 48 | } |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | \brief standard deconstructor |
---|
| 53 | |
---|
| 54 | */ |
---|
[4375] | 55 | PhysicsInterface::~PhysicsInterface () |
---|
[3954] | 56 | { |
---|
[4392] | 57 | PhysicsEngine::getInstance()->removePhysicsInterface(this); |
---|
[3954] | 58 | } |
---|
[4121] | 59 | |
---|
[4376] | 60 | /** |
---|
| 61 | \brief recalculates the total mass of all the children of this node |
---|
| 62 | |
---|
| 63 | (only availiable for PNodes) |
---|
| 64 | */ |
---|
[4375] | 65 | void PhysicsInterface::recalcMass() |
---|
[4121] | 66 | { |
---|
[4376] | 67 | PNode* massCalcPNode = dynamic_cast<PNode*>(this); //! \todo not sure if this will work .... |
---|
| 68 | float massSum = 0; |
---|
| 69 | |
---|
| 70 | tIterator<PNode>* iterator = massCalcPNode->children->getIterator(); |
---|
| 71 | PNode* pn = iterator->nextElement(); |
---|
| 72 | while( pn != NULL) |
---|
[4121] | 73 | { |
---|
[4376] | 74 | // todo: find out if children are PhysicsInterface in an efficient way |
---|
| 75 | if (strcmp( pn->getClassName(), "PhysicsInterface")) { |
---|
| 76 | massSum += ((PhysicsInterface*)pn)->getTotalMass(); |
---|
| 77 | } |
---|
[4121] | 78 | pn = iterator->nextElement(); |
---|
| 79 | } |
---|
[4376] | 80 | delete iterator; |
---|
[4121] | 81 | |
---|
[4376] | 82 | if (massSum != this->massChildren ) { |
---|
| 83 | this->massChildren = massSum; |
---|
| 84 | if (strcmp( massCalcPNode->parent->getClassName(), "PhysicsInterface")) |
---|
| 85 | ((PhysicsInterface*)massCalcPNode->parent)->recalcMass(); |
---|
| 86 | } else { |
---|
| 87 | this->massChildren = massSum; |
---|
| 88 | } |
---|
[4121] | 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
[4395] | 92 | void PhysicsInterface::applyField(Field* field) |
---|
[4221] | 93 | { |
---|
[4397] | 94 | PNode* tmp = (PNode*) objectPointer; |
---|
| 95 | this->forceSum += field->calcForce(tmp->getAbsCoor()); |
---|
[4221] | 96 | } |
---|
| 97 | |
---|
[4376] | 98 | void PhysicsInterface::tickPhys( float dt ) |
---|
[4221] | 99 | { |
---|
[4397] | 100 | // Vector acc = this->forceSum / ( this->massChildren + this->mass ); |
---|
| 101 | PNode* coorTick = (PNode*)(this->objectPointer); |
---|
| 102 | if (coorTick) |
---|
| 103 | coorTick->setRelCoor(coorTick->getRelCoor() + this->forceSum/this->mass * dt); |
---|
[4396] | 104 | this->forceSum = Vector(0,0,0); |
---|
[4376] | 105 | // todo: introduce kinematics |
---|
[4221] | 106 | } |
---|