purge support for voxels from PhysicsEngine

This commit is contained in:
Andrew Meadows 2015-01-05 15:02:18 -08:00
parent 244a8503ef
commit 75a8faec86
5 changed files with 0 additions and 164 deletions

View file

@ -24,7 +24,6 @@ PhysicsEngine::PhysicsEngine(const glm::vec3& offset)
_constraintSolver(NULL),
_dynamicsWorld(NULL),
_originOffset(offset),
_voxels(),
_entityPacketSender(NULL),
_frameCount(0) {
}
@ -221,62 +220,6 @@ void PhysicsEngine::stepSimulation() {
_frameCount += (uint32_t)numSubSteps;
}
bool PhysicsEngine::addVoxel(const glm::vec3& position, float scale) {
glm::vec3 halfExtents = glm::vec3(0.5f * scale);
glm::vec3 trueCenter = position + halfExtents;
PositionHashKey key(trueCenter);
VoxelObject* proxy = _voxels.find(key);
if (!proxy) {
// create a shape
ShapeInfo info;
info.setBox(halfExtents);
btCollisionShape* shape = _shapeManager.getShape(info);
// NOTE: the shape creation will fail when the size of the voxel is out of range
if (shape) {
// create a collisionObject
btCollisionObject* object = new btCollisionObject();
object->setCollisionShape(shape);
btTransform transform;
transform.setIdentity();
// we shift the center into the simulation's frame
glm::vec3 shiftedCenter = (position - _originOffset) + halfExtents;
transform.setOrigin(btVector3(shiftedCenter.x, shiftedCenter.y, shiftedCenter.z));
object->setWorldTransform(transform);
// add to map and world
_voxels.insert(key, VoxelObject(trueCenter, object));
_dynamicsWorld->addCollisionObject(object);
return true;
}
}
return false;
}
bool PhysicsEngine::removeVoxel(const glm::vec3& position, float scale) {
glm::vec3 halfExtents = glm::vec3(0.5f * scale);
glm::vec3 trueCenter = position + halfExtents;
PositionHashKey key(trueCenter);
VoxelObject* proxy = _voxels.find(key);
if (proxy) {
// remove from world
assert(proxy->_object);
_dynamicsWorld->removeCollisionObject(proxy->_object);
// release shape
ShapeInfo info;
info.setBox(halfExtents);
bool released = _shapeManager.releaseShape(info);
assert(released);
// delete object and remove from voxel map
delete proxy->_object;
_voxels.remove(key);
return true;
}
return false;
}
// Bullet collision flags are as follows:
// CF_STATIC_OBJECT= 1,
// CF_KINEMATIC_OBJECT= 2,

View file

@ -24,10 +24,8 @@ typedef unsigned int uint32_t;
#include "BulletUtil.h"
#include "EntityMotionState.h"
#include "PositionHashKey.h"
#include "ShapeManager.h"
#include "ThreadSafeDynamicsWorld.h"
#include "VoxelObject.h"
const float HALF_SIMULATION_EXTENT = 512.0f; // meters
@ -56,16 +54,6 @@ public:
/// \return position of simulation origin in domain-frame
const glm::vec3& getOriginOffset() const { return _originOffset; }
/// \param position the minimum corner of the voxel
/// \param scale the length of the voxel side
/// \return true if Voxel added
bool addVoxel(const glm::vec3& position, float scale);
/// \param position the minimum corner of the voxel
/// \param scale the length of the voxel side
/// \return true if Voxel removed
bool removeVoxel(const glm::vec3& position, float scale);
/// \param motionState pointer to Object's MotionState
/// \return true if Object added
bool addObject(ObjectMotionState* motionState);
@ -103,7 +91,6 @@ protected:
private:
glm::vec3 _originOffset;
btHashMap<PositionHashKey, VoxelObject> _voxels;
// EntitySimulation stuff
QSet<EntityMotionState*> _entityMotionStates; // all entities that we track

View file

@ -1,37 +0,0 @@
//
// PositionHashKey.cpp
// libraries/physcis/src
//
// Created by Andrew Meadows 2014.11.05
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <math.h>
#include "PositionHashKey.h"
// static
int computeHash(const glm::vec3& center) {
// NOTE: 0.49f is used to bump the float up almost half a millimeter
// so the cast to int produces a round() effect rather than a floor()
int hash = DoubleHashKey::hashFunction((int)(center.x * MILLIMETERS_PER_METER + copysignf(1.0f, center.x) * 0.49f), 0);
hash ^= DoubleHashKey::hashFunction((int)(center.y * MILLIMETERS_PER_METER + copysignf(1.0f, center.y) * 0.49f), 1);
return hash ^ DoubleHashKey::hashFunction((int)(center.z * MILLIMETERS_PER_METER + copysignf(1.0f, center.z) * 0.49f), 2);
}
// static
int computeHash2(const glm::vec3& center) {
// NOTE: 0.49f is used to bump the float up almost half a millimeter
// so the cast to int produces a round() effect rather than a floor()
int hash = DoubleHashKey::hashFunction2((int)(center.x * MILLIMETERS_PER_METER + copysignf(1.0f, center.x) * 0.49f));
hash ^= DoubleHashKey::hashFunction2((int)(center.y * MILLIMETERS_PER_METER + copysignf(1.0f, center.y) * 0.49f));
return hash ^ DoubleHashKey::hashFunction2((int)(center.z * MILLIMETERS_PER_METER + copysignf(1.0f, center.z) * 0.49f));
}
PositionHashKey::PositionHashKey(glm::vec3 center) : DoubleHashKey() {
_hash = computeHash(center);
_hash2 = computeHash2(center);
}

View file

@ -1,26 +0,0 @@
//
// PositionHashKey.h
// libraries/physcis/src
//
// Created by Andrew Meadows 2014.11.05
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_PositionHashKey_h
#define hifi_PositionHashKey_h
#include <glm/glm.hpp>
#include <SharedUtil.h>
#include "DoubleHashKey.h"
class PositionHashKey : public DoubleHashKey {
public:
PositionHashKey(glm::vec3 center);
};
#endif // hifi_PositionHashKey_h

View file

@ -1,31 +0,0 @@
//
// VoxelObject.h
// libraries/physcis/src
//
// Created by Andrew Meadows 2014.11.05
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_VoxelObject_h
#define hifi_VoxelObject_h
#ifdef USE_BULLET_PHYSICS
#include <btBulletDynamicsCommon.h>
#include <glm/glm.hpp>
// VoxelObject is a simple wrapper for tracking a Voxel in a PhysicsEngine
class VoxelObject {
public:
VoxelObject(const glm::vec3& center, btCollisionObject* object) : _object(object), _center(center) {
assert(object != NULL);
}
btCollisionObject* _object;
glm::vec3 _center;
};
#endif // USE_BULLET_PHYSICS
#endif // hifi_VoxelObject_h