1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* Copyright (C) 2006-2010 - Frictional Games
*
* This file is part of HPL1 Engine.
*/
#include "hpl1/engine/impl/CollideShapeNewton.h"
#include "hpl1/engine/physics/PhysicsWorld.h"
#include "hpl1/engine/system/low_level_system.h"
namespace hpl {
//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
cCollideShapeNewton::cCollideShapeNewton(eCollideShapeType aType, const cVector3f &avSize,
cMatrixf *apOffsetMtx, NewtonWorld *apNewtonWorld,
iPhysicsWorld *apWorld)
: iCollideShape(apWorld) {
mpNewtonCollision = NULL;
mpNewtonWorld = apNewtonWorld;
mvSize = avSize;
mType = aType;
mfVolume = 0;
float *pMtx = NULL;
cMatrixf mtxTranspose;
if (apOffsetMtx) {
m_mtxOffset = *apOffsetMtx;
mtxTranspose = m_mtxOffset.GetTranspose();
pMtx = &(mtxTranspose.m[0][0]);
} else {
m_mtxOffset = cMatrixf::Identity;
}
////////////////////////////////////////////
// Create Newton collision
switch (aType) {
case eCollideShapeType_Null:
mpNewtonCollision = NewtonCreateNull(apNewtonWorld);
break;
case eCollideShapeType_Box:
mpNewtonCollision = NewtonCreateBox(apNewtonWorld,
mvSize.x, mvSize.y, mvSize.z,
0, pMtx);
break;
case eCollideShapeType_Sphere:
mpNewtonCollision = NewtonCreateSphere(apNewtonWorld,
mvSize.x, mvSize.y, mvSize.z,
0, pMtx);
break;
case eCollideShapeType_Cylinder:
mpNewtonCollision = NewtonCreateCylinder(apNewtonWorld,
mvSize.x, mvSize.y,
0, pMtx);
break;
case eCollideShapeType_Capsule:
mpNewtonCollision = NewtonCreateCapsule(apNewtonWorld,
mvSize.x, mvSize.y,
0, pMtx);
break;
default:
break;
}
////////////////////////////////////////////
// Calculate Bounding volume and volume.
if (mType == eCollideShapeType_Box) {
mBoundingVolume.SetSize(mvSize);
mfVolume = mvSize.x * mvSize.y * mvSize.z;
} else if (mType == eCollideShapeType_Sphere) {
mBoundingVolume.SetSize(mvSize * 2);
mfVolume = (4.0f / 3.0f) * kPif * (mvSize.x * mvSize.x * mvSize.x);
} else if (mType == eCollideShapeType_Cylinder ||
mType == eCollideShapeType_Capsule) {
mBoundingVolume.SetSize(cVector3f(mvSize.y, mvSize.x * 2, mvSize.x * 2));
// Not gonna be correct for capsule...
if (mType == eCollideShapeType_Cylinder)
mfVolume = kPif * (mvSize.x * mvSize.x) * mvSize.y;
else {
// Height of the cylinder part.
float fCylHeight = mvSize.y - (mvSize.x * 2);
mfVolume = 0;
// The volume of the cylinder part.
if (fCylHeight > 0)
mfVolume += kPif * (mvSize.x * mvSize.x) * fCylHeight;
// The volume of the sphere part.
mfVolume += (4.0f / 3.0f) * kPif * (mvSize.x * mvSize.x * mvSize.x);
}
}
mBoundingVolume.SetTransform(m_mtxOffset);
}
//-----------------------------------------------------------------------
cCollideShapeNewton::~cCollideShapeNewton() {
// Release Newton Collision
if (mpNewtonCollision)
NewtonReleaseCollision(mpNewtonWorld, mpNewtonCollision);
// Release all subshapes (for compound objects)
for (int i = 0; i < (int)mvSubShapes.size(); i++) {
mpWorld->DestroyShape(mvSubShapes[i]);
}
}
//-----------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
iCollideShape *cCollideShapeNewton::GetSubShape(int alIdx) {
if (mType == eCollideShapeType_Compound)
return mvSubShapes[alIdx];
else
return this;
}
int cCollideShapeNewton::GetSubShapeNum() {
if (mType == eCollideShapeType_Compound)
return (int)mvSubShapes.size();
else
return 1;
}
//-----------------------------------------------------------------------
cVector3f cCollideShapeNewton::GetInertia(float afMass) {
cVector3f vInertia(1, 1, 1);
// Box
if (mType == eCollideShapeType_Box) {
float fInv = 1.0f / 12.0f;
vInertia = cVector3f(
afMass * (mvSize.y * mvSize.y + mvSize.z * mvSize.z) * fInv,
afMass * (mvSize.x * mvSize.x + mvSize.z * mvSize.z) * fInv,
afMass * (mvSize.x * mvSize.x + mvSize.y * mvSize.y) * fInv);
}
// Sphere
else if (mType == eCollideShapeType_Sphere) {
float fI = 0.4f * afMass * mvSize.x * mvSize.x;
vInertia = cVector3f(fI, fI, fI);
}
// Cylinder
else if (mType == eCollideShapeType_Cylinder) {
float fRadius = mvSize.x;
vInertia.x = afMass * (fRadius * fRadius) * (1.0f / 4.0f) + afMass * (mvSize.y * mvSize.y) * (1.0f / 12.0f);
vInertia.y = vInertia.x;
vInertia.z = afMass * (fRadius * fRadius) * (1.0f / 2.0f);
}
// Capsule
else if (mType == eCollideShapeType_Capsule) {
float fRadius = mvSize.x;
vInertia.x = afMass * (fRadius * fRadius) * (1.0f / 4.0f) + afMass * (mvSize.y * mvSize.y) * (1.0f / 12.0f);
vInertia.y = vInertia.x;
vInertia.z = afMass * (fRadius * fRadius) * (1.0f / 2.0f);
}
// Compound
// This is only a very bad approximation.
else if (mType == eCollideShapeType_Compound) {
cVector3f vBoxSize = mBoundingVolume.GetSize();
float fBoxVolume = vBoxSize.x * vBoxSize.y * vBoxSize.z;
float fInv = 1.0f / 12.0f;
vInertia = cVector3f(
afMass * (vBoxSize.y * vBoxSize.y + vBoxSize.z * vBoxSize.z) * fInv,
afMass * (vBoxSize.x * vBoxSize.x + vBoxSize.z * vBoxSize.z) * fInv,
afMass * (vBoxSize.x * vBoxSize.x + vBoxSize.y * vBoxSize.y) * fInv);
// Scale of a bit of the inertia since the compound is not a 100% solid box
vInertia = vInertia * (1.0f - ((fBoxVolume / mfVolume) * 0.3f));
}
return vInertia;
}
//-----------------------------------------------------------------------
void cCollideShapeNewton::CreateFromShapeVec(tCollideShapeVec &avShapes) {
Common::Array<NewtonCollision *> vNewtonColliders;
vNewtonColliders.reserve(avShapes.size());
mvSubShapes.reserve(avShapes.size());
mfVolume = 0;
for (size_t i = 0; i < avShapes.size(); i++) {
mvSubShapes.push_back(avShapes[i]);
cCollideShapeNewton *pNewtonShape = static_cast<cCollideShapeNewton *>(avShapes[i]);
vNewtonColliders.push_back(pNewtonShape->GetNewtonCollision());
mfVolume += pNewtonShape->GetVolume();
}
mpNewtonCollision = NewtonCreateCompoundCollision(mpNewtonWorld, (int)vNewtonColliders.size(),
&vNewtonColliders[0], 0);
// Create bounding volume
cVector3f vFinalMax = avShapes[0]->GetBoundingVolume().GetMax();
cVector3f vFinalMin = avShapes[0]->GetBoundingVolume().GetMin();
for (size_t i = 1; i < avShapes.size(); i++) {
cVector3f vMax = avShapes[i]->GetBoundingVolume().GetMax();
cVector3f vMin = avShapes[i]->GetBoundingVolume().GetMin();
if (vFinalMax.x < vMax.x)
vFinalMax.x = vMax.x;
if (vFinalMin.x > vMin.x)
vFinalMin.x = vMin.x;
if (vFinalMax.y < vMax.y)
vFinalMax.y = vMax.y;
if (vFinalMin.y > vMin.y)
vFinalMin.y = vMin.y;
if (vFinalMax.z < vMax.z)
vFinalMax.z = vMax.z;
if (vFinalMin.z > vMin.z)
vFinalMin.z = vMin.z;
}
mBoundingVolume.SetLocalMinMax(vFinalMin, vFinalMax);
}
//-----------------------------------------------------------------------
void cCollideShapeNewton::CreateFromVertices(const unsigned int *apIndexArray, int alIndexNum,
const float *apVertexArray, int alVtxStride, int alVtxNum) {
float vTriVec[9];
mpNewtonCollision = NewtonCreateTreeCollision(mpNewtonWorld, 0);
// Log("-- Creating mesh collision.:\n");
NewtonTreeCollisionBeginBuild(mpNewtonCollision);
for (int tri = 0; tri < alIndexNum; tri += 3) {
// Log("tri: %d:\n", tri/3);
for (int idx = 0; idx < 3; idx++) {
int lVtx = apIndexArray[tri + 2 - idx] * alVtxStride;
vTriVec[idx * 3 + 0] = apVertexArray[lVtx + 0];
vTriVec[idx * 3 + 1] = apVertexArray[lVtx + 1];
vTriVec[idx * 3 + 2] = apVertexArray[lVtx + 2];
}
NewtonTreeCollisionAddFace(mpNewtonCollision, 3, vTriVec, sizeof(float) * 3, 1);
}
NewtonTreeCollisionEndBuild(mpNewtonCollision, false);
// Set bounding box size
mBoundingVolume.AddArrayPoints(apVertexArray, alVtxNum);
mBoundingVolume.CreateFromPoints(alVtxStride);
}
//-----------------------------------------------------------------------
} // namespace hpl
|