void EditorScene::TrySelection(Vector3 from, Vector3 direction) { if (selection) selection->SetDebugFlags(selection->GetDebugFlags() & (~DebugRenderComponent::DEBUG_DRAW_AABOX_CORNERS)); btVector3 pos(from.x, from.y, from.z); btVector3 to(direction.x, direction.y, direction.z); btCollisionWorld::AllHitsRayResultCallback cb(pos, to); collisionWorld->rayTest(pos, to, cb); btCollisionObject * coll = 0; if (cb.hasHit()) { //Logger::Debug("Has Hit"); int findedIndex = cb.m_collisionObjects.size() - 1; if(lastSelectedPhysics) { BulletComponent * bulletComponent = (BulletComponent*)lastSelectedPhysics->GetComponent(Component::BULLET_COMPONENT); BulletObject * bulletObject = (BulletObject*)bulletComponent->GetBulletObject(); if (bulletObject) { for (int i = cb.m_collisionObjects.size() - 1; i >= 0 ; i--) { if (bulletObject->GetCollisionObject() == cb.m_collisionObjects[i]) { findedIndex = i; break; } } while (findedIndex >= 0 && bulletObject->GetCollisionObject() == cb.m_collisionObjects[findedIndex]) findedIndex--; findedIndex = findedIndex % cb.m_collisionObjects.size(); } } // Logger::Debug("size:%d selIndex:%d", cb.m_collisionObjects.size(), findedIndex); if (findedIndex == -1) findedIndex = cb.m_collisionObjects.size() - 1; coll = cb.m_collisionObjects[findedIndex]; selection = FindSelected(this, coll); lastSelectedPhysics = selection; SetSelection(selection); } else { SetSelection(0); } }
Entity * EditorScene::FindSelected(Entity * curr, btCollisionObject * coll) { Entity * node = curr; // LIGHT // if (node == 0) // node = dynamic_cast<LightNode *> (curr); if (node == 0) node = dynamic_cast<UserNode *> (curr); BulletComponent * bulletComponent = (BulletComponent*)node->GetComponent(Component::BULLET_COMPONENT); if (bulletComponent && bulletComponent->GetBulletObject()) { BulletObject * bulletObject = (BulletObject*)bulletComponent->GetBulletObject(); if (bulletObject->GetCollisionObject() == coll) return curr; } int size = curr->GetChildrenCount(); for (int i = 0; i < size; i++) { Entity * result = FindSelected(curr->GetChild(i), coll); if (result) return result; } return 0; }