2 * Copyright (c) 2007 Walaber
3 * Modified by Ketmar // Invisible Vector
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 #include "PressureBody.h"
26 namespace JellyPhysics
{
28 PressureBody::~PressureBody () {
30 //delete[] mEdgeLengthList;
34 void PressureBody::accumulateInternalForces () {
35 SpringBody::accumulateInternalForces();
36 // internal forces based on pressure equations. we need 2 loops to do this. one to find the overall volume of the
37 // body, and 1 to apply forces. we will need the normals for the edges in both loops, so we will cache them and remember them.
40 for (int i
= 0; i
< c
; ++i
) {
41 int prev
= (i
> 0 ? i
-1 : mPointCount
-1);
42 int next
= (i
< mPointCount
-1 ? i
+1 : 0);
43 PointMass
& pmP
= mPointMasses
[prev
];
44 PointMass
& pmI
= mPointMasses
[i
];
45 PointMass
& pmN
= mPointMasses
[next
];
46 // currently we are talking about the edge from i --> j
47 // first calculate the volume of the body, and cache normals as we go
48 Vector2 edge1N
= pmI
.Position
-pmP
.Position
;
49 edge1N
.makePerpendicular();
50 Vector2 edge2N
= pmN
.Position
-pmI
.Position
;
51 edge2N
.makePerpendicular();
52 Vector2 norm
= edge1N
+edge2N
;
54 float edgeL
= mEdgeInfo
[i
].length
; //edge2N.length();
55 // cache normal and edge length
56 mNormalList
[i
] = norm
;
57 //mEdgeLengthList[i] = edgeL;
58 float xdist
= fabsf(pmI
.Position
.X
-pmN
.Position
.X
);
59 float normX
= fabsf(norm
.X
);
60 float volumeProduct
= xdist
*normX
*edgeL
;
62 mVolume
+= 0.5f
*volumeProduct
;
64 // now loop through, adding forces!
65 float invVolume
= 1.0f
/mVolume
;
66 for (int i
= 0; i
< c
; ++i
) {
67 int j
= (i
< mPointCount
-1 ? i
+1 : 0);
68 float pressureV
= invVolume
*mEdgeInfo
[i
].length
*mGasAmount
;
69 mPointMasses
[i
].Force
+= mNormalList
[i
]*pressureV
;
70 mPointMasses
[j
].Force
+= mNormalList
[j
]*pressureV
;