Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/physics/physics_interface.cc @ 4397

Last change on this file since 4397 was 4397, checked in by bensch, 19 years ago

orxonox/trunk: ok… forces now apply to the right objects, but i don't really like the way it works…. we will see….

File size: 2.5 KB
Line 
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
32using namespace std;
33
34
35/**
36   \brief standard constructor
37*/
38PhysicsInterface::PhysicsInterface (void* objectPointer) 
39{
40  this->objectPointer = objectPointer;
41
42  //   this->setClassName ("PhysicsInterface");
43   this->mass = 1;
44   this->massChildren = 0;
45   this->forceSum = Vector(0, 0, 0);
46
47   PhysicsEngine::getInstance()->addPhysicsInterface(this);
48}
49
50
51/**
52   \brief standard deconstructor
53
54*/
55PhysicsInterface::~PhysicsInterface () 
56{
57   PhysicsEngine::getInstance()->removePhysicsInterface(this);
58}
59
60/**
61   \brief recalculates the total mass of all the children of this node
62
63   (only availiable for PNodes)
64*/
65void PhysicsInterface::recalcMass()
66{
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) 
73    { 
74      // todo: find out if children are PhysicsInterface in an efficient way
75      if (strcmp( pn->getClassName(), "PhysicsInterface")) {
76        massSum += ((PhysicsInterface*)pn)->getTotalMass();
77      }
78      pn = iterator->nextElement();
79    }
80  delete iterator;
81       
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  }
89}
90       
91       
92void PhysicsInterface::applyField(Field* field)
93{
94  PNode* tmp = (PNode*) objectPointer;
95  this->forceSum += field->calcForce(tmp->getAbsCoor()); 
96}
97
98void PhysicsInterface::tickPhys( float dt )
99{
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);
104  this->forceSum = Vector(0,0,0);
105  // todo: introduce kinematics
106}
Note: See TracBrowser for help on using the repository browser.