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

View file

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