add findClosestParticle()

This commit is contained in:
ZappoMan 2013-12-12 12:24:07 -08:00
parent a80acc6806
commit 1a6f730659
6 changed files with 82 additions and 2 deletions

View file

@ -141,6 +141,7 @@ public:
Camera* getCamera() { return &_myCamera; }
ViewFrustum* getViewFrustum() { return &_viewFrustum; }
VoxelSystem* getVoxels() { return &_voxels; }
ParticleTreeRenderer* getParticles() { return &_particles; }
VoxelSystem* getSharedVoxelSystem() { return &_sharedVoxelSystem; }
VoxelTree* getClipboard() { return &_clipboard; }
Environment* getEnvironment() { return &_environment; }

View file

@ -61,6 +61,16 @@ void Hand::reset() {
}
void Hand::simulateToyBall(PalmData& palm, const glm::vec3& fingerTipPosition, float deltaTime) {
glm::vec3 targetPosition = fingerTipPosition / (float)TREE_SCALE;
float targetRadius = (TOY_BALL_RADIUS * 2.0f) / (float)TREE_SCALE;
const Particle* closestParticle = Application::getInstance()->getParticles()
->getTree()->findClosestPartice(targetPosition, targetRadius);
if (closestParticle) {
printf("potentially caught... particle ID:%d\n", closestParticle->getID());
}
// Is the controller button being held down....
if (palm.getControllerButtons() & BUTTON_FWD) {
// If grabbing toy ball, add forces to it.
@ -71,8 +81,10 @@ void Hand::simulateToyBall(PalmData& palm, const glm::vec3& fingerTipPosition, f
// isCaught is also used as "creating" a new ball... for now, this section is the
// create new ball portion of the code...
bool isCaught = true;
if (isCaught) {
bool isCaught = false;
// If we didn't catch something, then create a new ball....
if (!isCaught) {
_toyBallInHand = true;
_hasToyBall = true;

View file

@ -64,6 +64,57 @@ void ParticleTree::storeParticle(const Particle& particle) {
_isDirty = true;
}
class FindNearPointArgs {
public:
glm::vec3 position;
float targetRadius;
bool found;
const Particle* closestParticle;
float closestParticleDistance;
};
bool ParticleTree::findNearPointOperation(OctreeElement* element, void* extraData) {
FindNearPointArgs* args = static_cast<FindNearPointArgs*>(extraData);
ParticleTreeElement* particleTreeElement = static_cast<ParticleTreeElement*>(element);
// If this particleTreeElement contains the point, then search it...
if (particleTreeElement->getAABox().contains(args->position)) {
const Particle* thisClosestParticle = particleTreeElement->getClosestParticle(args->position);
// we may have gotten NULL back, meaning no particle was available
if (thisClosestParticle) {
glm::vec3 particlePosition = thisClosestParticle->getPosition();
float distanceFromPointToParticle = glm::distance(particlePosition, args->position);
// If we're within our target radius
if (distanceFromPointToParticle <= args->targetRadius) {
// we are closer than anything else we've found
if (distanceFromPointToParticle < args->closestParticleDistance) {
args->closestParticle = thisClosestParticle;
args->closestParticleDistance = distanceFromPointToParticle;
args->found = true;
}
}
}
// we should be able to optimize this...
return true; // keep searching in case children have closer particles
}
// if this element doesn't contain the point, then none of it's children can contain the point, so stop searching
return false;
}
const Particle* ParticleTree::findClosestPartice(glm::vec3 position, float targetRadius) {
// First, look for the existing particle in the tree..
FindNearPointArgs args = { position, targetRadius, false, NULL, FLT_MAX };
recurseTreeWithOperation(findNearPointOperation, &args);
return args.closestParticle;
}
int ParticleTree::processEditPacketData(PACKET_TYPE packetType, unsigned char* packetData, int packetLength,
unsigned char* editData, int maxLength, Node* senderNode) {

View file

@ -43,6 +43,7 @@ public:
virtual void update();
void storeParticle(const Particle& particle);
const Particle* findClosestPartice(glm::vec3 position, float targetRadius);
void addNewlyCreatedHook(NewlyCreatedParticleHook* hook);
void removeNewlyCreatedHook(NewlyCreatedParticleHook* hook);
@ -51,6 +52,7 @@ private:
static bool updateOperation(OctreeElement* element, void* extraData);
static bool findAndUpdateOperation(OctreeElement* element, void* extraData);
static bool findNearPointOperation(OctreeElement* element, void* extraData);
void notifyNewlyCreatedParticle(const Particle& newParticle, Node* senderNode);

View file

@ -104,6 +104,19 @@ bool ParticleTreeElement::updateParticle(const Particle& particle) {
return false;
}
const Particle* ParticleTreeElement::getClosestParticle(glm::vec3 position) const {
const Particle* closestParticle = NULL;
float closestParticleDistance = FLT_MAX;
uint16_t numberOfParticles = _particles.size();
for (uint16_t i = 0; i < numberOfParticles; i++) {
float distanceToParticle = glm::distance(position, _particles[i].getPosition());
if (distanceToParticle < closestParticleDistance) {
closestParticle = &_particles[i];
}
}
return closestParticle;
}
int ParticleTreeElement::readElementDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
ReadBitstreamToTreeParams& args) {

View file

@ -80,6 +80,7 @@ public:
bool containsParticle(const Particle& particle) const;
bool updateParticle(const Particle& particle);
const Particle* getClosestParticle(glm::vec3 position) const;
protected:
void storeParticle(const Particle& particle);