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