00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "PressureForce.h"
00013
#include <OpenCAL/BalloonMesh.h>
00014
#include <OpenCAL/Vertex.h>
00015
#include <OpenCAL/PointMass.h>
00016
#include <OpenCAL/Triangle.h>
00017
using namespace OpenCAL;
00018
00019
using namespace std;
00020
00021
00022
00023
00024
00025
00026 PressureForce::PressureForce(
BalloonMesh *parent)
00027 :
Force(parent), m_idealGasConstant(8.314570f), m_numMoles(0.1f), m_temperature(293.0f)
00028 {
00029 m_numMoles = 0.0001f;
00030 }
00031
00032 PressureForce::~PressureForce()
00033 {
00034 }
00035
00036
00037
00038
00039
00040
00041
void PressureForce::execute(
float seconds)
00042 {
00043
00044 Force::execute(seconds);
00045
00046
BalloonMesh *balloon = (
BalloonMesh *) m_parent;
00047
TriangleMesh *mesh = balloon->
getSurfaceP();
00048
00049 mesh->
invalidateTriangleNormals();
00050 mesh->
calculateVertexNormals();
00051
00052
float volume = calculateVolume();
00053
if(volume == 0.0f)
00054 volume = 1.0f;
00055
float pressure =
m_numMoles * m_idealGasConstant *
m_temperature / volume;
00056
00057
float blend = pressure * 25;
00058
00059
Color color(blend, 1.0f - blend, 0.0f);
00060 mesh->
getMaterial(0)->
setAmbient(color);
00061 mesh->
getMaterial(0)->
setDiffuse(color);
00062
00063
Triangle *triangle;
00064
float force;
00065 Vector3 direction;
00066
PointMass *point;
00067
for(
unsigned int i = 0; i < mesh->
numTriangles(); ++i)
00068 {
00069 triangle = mesh->
getTriangle(i);
00070 force = triangle->
getArea() * pressure;
00071
00072
00073 direction = triangle->
getVertex(0)->
getNormal();
00074 point = balloon->
getPointFromVertex(triangle->
getVertex(0));
00075 point->
addForce(force * direction);
00076
00077
00078 direction = triangle->
getVertex(1)->
getNormal();
00079 point = balloon->
getPointFromVertex(triangle->
getVertex(1));
00080 point->
addForce(force * direction);
00081
00082
00083 direction = triangle->
getVertex(2)->
getNormal();
00084 point = balloon->
getPointFromVertex(triangle->
getVertex(2));
00085 point->
addForce(force * direction);
00086 }
00087 }
00088
00089
00090
00091
00092
00093
00094
float PressureForce::calculateVolume()
00095 {
00096
BalloonMesh *balloon = (
BalloonMesh *) m_parent;
00097
TriangleMesh *mesh = balloon->
getSurfaceP();
00098
00099
float volume = 0.0f;
00100
00101
Triangle *triangle;
00102
for(
unsigned int i = 0; i < mesh->
numTriangles(); ++i)
00103 {
00104 triangle = mesh->
getTriangle(i);
00105
const Vector3 &p1 = triangle->
getVertex(0)->
getPosition();
00106
const Vector3 &p2 = triangle->
getVertex(1)->
getPosition();
00107
const Vector3 &p3 = triangle->
getVertex(2)->
getPosition();
00108
00109 volume += p1.dotProduct(p2.crossProduct(p3));
00110 }
00111 volume /= 6.0f;
00112
00113
return volume;
00114 }
00115