include quads in creation of hulls for physics collision. visual model vs collision model debug spew. fix dice script

This commit is contained in:
Seth Alves 2015-03-30 17:04:47 -07:00
parent 4aa6748e11
commit acf30782b6
2 changed files with 66 additions and 6 deletions

View file

@ -45,6 +45,9 @@ var diceButton = Overlays.addOverlay("image", {
var GRAVITY = -3.5;
var LIFETIME = 300;
// NOTE: angularVelocity is in radians/sec
var maxAngularSpeed = Math.PI;
function shootDice(position, velocity) {
for (var i = 0; i < NUMBER_OF_DICE; i++) {
dice.push(Entities.addEntity(
@ -53,8 +56,6 @@ function shootDice(position, velocity) {
position: position,
velocity: velocity,
rotation: Quat.fromPitchYawRollDegrees(Math.random() * 360, Math.random() * 360, Math.random() * 360),
// NOTE: angularVelocity is in radians/sec
var maxAngularSpeed = Math.PI;
angularVelocity: { x: Math.random() * maxAngularSpeed, y: Math.random() * maxAngularSpeed, z: Math.random() * maxAngularSpeed },
lifetime: LIFETIME,
gravity: { x: 0, y: GRAVITY, z: 0 },

View file

@ -311,35 +311,41 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& info) {
if (_model->getCollisionURL().isEmpty()) {
info.setParams(getShapeType(), 0.5f * getDimensions());
} else {
qDebug() << "\n\n--------------------------------------------------\n";
qDebug() << getCollisionModelURL();
const QSharedPointer<NetworkGeometry> collisionNetworkGeometry = _model->getCollisionGeometry();
const FBXGeometry& fbxGeometry = collisionNetworkGeometry->getFBXGeometry();
qDebug() << fbxGeometry.meshes.size() << "meshes";
AABox aaBox;
_points.clear();
unsigned int i = 0;
foreach (const FBXMesh& mesh, fbxGeometry.meshes) {
qDebug() << " " << mesh.parts.size() << "mesh parts";
foreach (const FBXMeshPart &meshPart, mesh.parts) {
QVector<glm::vec3> pointsInPart;
unsigned int triangleCount = meshPart.triangleIndices.size() / 3;
qDebug() << " " << triangleCount << "triangles";
assert((unsigned int)meshPart.triangleIndices.size() == triangleCount*3);
for (unsigned int j = 0; j < triangleCount; j++) {
unsigned int p0Index = meshPart.triangleIndices[j*3];
unsigned int p1Index = meshPart.triangleIndices[j*3+1];
unsigned int p2Index = meshPart.triangleIndices[j*3+2];
assert(p0Index < (unsigned int)mesh.vertices.size());
assert(p1Index < (unsigned int)mesh.vertices.size());
assert(p2Index < (unsigned int)mesh.vertices.size());
glm::vec3 p0 = mesh.vertices[p0Index];
glm::vec3 p1 = mesh.vertices[p1Index];
glm::vec3 p2 = mesh.vertices[p2Index];
aaBox += p0;
aaBox += p1;
aaBox += p2;
if (!pointsInPart.contains(p0)) {
pointsInPart << p0;
}
@ -351,18 +357,67 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& info) {
}
}
unsigned int quadCount = meshPart.quadIndices.size() / 4;
qDebug() << " " << quadCount << "quads";
assert((unsigned int)meshPart.quadIndices.size() == quadCount*4);
for (unsigned int j = 0; j < quadCount; j++) {
unsigned int p0Index = meshPart.quadIndices[j*4];
unsigned int p1Index = meshPart.quadIndices[j*4+1];
unsigned int p2Index = meshPart.quadIndices[j*4+2];
unsigned int p3Index = meshPart.quadIndices[j*4+3];
assert(p0Index < (unsigned int)mesh.vertices.size());
assert(p1Index < (unsigned int)mesh.vertices.size());
assert(p2Index < (unsigned int)mesh.vertices.size());
assert(p3Index < (unsigned int)mesh.vertices.size());
glm::vec3 p0 = mesh.vertices[p0Index];
glm::vec3 p1 = mesh.vertices[p1Index];
glm::vec3 p2 = mesh.vertices[p2Index];
glm::vec3 p3 = mesh.vertices[p3Index];
aaBox += p0;
aaBox += p1;
aaBox += p2;
aaBox += p3;
if (!pointsInPart.contains(p0)) {
pointsInPart << p0;
}
if (!pointsInPart.contains(p1)) {
pointsInPart << p1;
}
if (!pointsInPart.contains(p2)) {
pointsInPart << p2;
}
if (!pointsInPart.contains(p3)) {
pointsInPart << p3;
}
}
QVector<glm::vec3> newMeshPoints;
_points << newMeshPoints;
_points[i++] << pointsInPart;
}
}
qDebug() << "original aaBox:" << aaBox;
glm::vec3 c = aaBox.calcCenter();
qDebug() << "original aaBox center:" << c[0] << c[1] << c[2];
qDebug() << "_model->getOffset():" << _model->getOffset();
// make sure we aren't about to divide by zero
glm::vec3 aaBoxDim = aaBox.getDimensions();
aaBoxDim = glm::clamp(aaBoxDim, glm::vec3(FLT_EPSILON), aaBoxDim);
qDebug() << "aaBoxDim:" << aaBoxDim;
glm::vec3 scale = _dimensions / aaBoxDim;
qDebug() << "scale:" << scale;
AABox afterAABox;
// multiply each point by scale before handing the point-set off to the physics engine
for (int i = 0; i < _points.size(); i++) {
for (int j = 0; j < _points[i].size(); j++) {
@ -370,9 +425,13 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& info) {
_points[i][j] += _model->getOffset();
// scale so the collision points match the model points
_points[i][j] *= scale;
afterAABox += _points[i][j];
}
}
qDebug() << "after aaBox:" << afterAABox;
info.setParams(getShapeType(), _dimensions, _collisionModelURL);
info.setConvexHulls(_points);
}