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 | \brief 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 | PhysicsEngine::getInstance()->addPhysicsInterface(this); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | \brief standard deconstructor |
---|
52 | */ |
---|
53 | PhysicsInterface::~PhysicsInterface () |
---|
54 | { |
---|
55 | PhysicsEngine::getInstance()->removePhysicsInterface(this); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | \brief recalculates the total mass of all the children of this node |
---|
60 | |
---|
61 | (only availiable for PNodes) |
---|
62 | */ |
---|
63 | void PhysicsInterface::recalcMass() |
---|
64 | { |
---|
65 | /* |
---|
66 | PNode* massCalcPNode = dynamic_cast<PNode*>(this); //! \todo not sure if this will work .... |
---|
67 | float massSum = 0; |
---|
68 | |
---|
69 | tIterator<PNode>* iterator = massCalcPNode->children->getIterator(); |
---|
70 | PNode* pn = iterator->nextElement(); |
---|
71 | while( pn != NULL) |
---|
72 | { |
---|
73 | // todo: find out if children are PhysicsInterface in an efficient way |
---|
74 | if (strcmp( pn->getClassName(), "PhysicsInterface")) { |
---|
75 | massSum += ((PhysicsInterface*)pn)->getTotalMass(); |
---|
76 | } |
---|
77 | pn = iterator->nextElement(); |
---|
78 | } |
---|
79 | delete iterator; |
---|
80 | |
---|
81 | if (massSum != this->massChildren ) { |
---|
82 | this->massChildren = massSum; |
---|
83 | if (strcmp( massCalcPNode->parent->getClassName(), "PhysicsInterface")) |
---|
84 | ((PhysicsInterface*)massCalcPNode->parent)->recalcMass(); |
---|
85 | } else { |
---|
86 | this->massChildren = massSum; |
---|
87 | } |
---|
88 | */ |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | \brief applyes a field to this Object |
---|
93 | \param field the field to apply |
---|
94 | */ |
---|
95 | void PhysicsInterface::applyField(Field* field) |
---|
96 | { |
---|
97 | PNode* tmp = dynamic_cast<PNode*>((BaseObject*)this); |
---|
98 | this->forceSum += field->calcForce(tmp->getAbsCoor()); |
---|
99 | this->bForceApplied = true; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | \brief ticks the PhysicsEffect |
---|
104 | \param dt: the value about which to tick |
---|
105 | */ |
---|
106 | void PhysicsInterface::tickPhys( float dt ) |
---|
107 | { |
---|
108 | // Vector acc = this->forceSum / ( this->massChildren + this->mass ); |
---|
109 | PNode* coorTick = dynamic_cast<PNode*>((BaseObject*)this); |
---|
110 | if (this->bForceApplied && coorTick) |
---|
111 | { |
---|
112 | coorTick->shiftCoor((coorTick->getVelocity()+ this->forceSum/this->mass * dt)*dt); |
---|
113 | |
---|
114 | this->bForceApplied = false; |
---|
115 | } |
---|
116 | this->forceSum = Vector(0,0,0); |
---|
117 | } |
---|