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