void Navigation::AddOrRemoveObject() { // Raycast and check if we hit a mushroom node. If yes, remove it, if no, create a new one Vector3 hitPos; Drawable* hitDrawable; if (Raycast(250.0f, hitPos, hitDrawable)) { // The part of the navigation mesh we must update, which is the world bounding box of the associated // drawable component BoundingBox updateBox; Node* hitNode = hitDrawable->GetNode(); if (hitNode->GetName() == "Mushroom") { updateBox = hitDrawable->GetWorldBoundingBox(); hitNode->Remove(); } else { Node* newNode = CreateMushroom(hitPos); updateBox = newNode->GetComponent<StaticModel>()->GetWorldBoundingBox(); } // Rebuild part of the navigation mesh, then recalculate path if applicable NavigationMesh* navMesh = scene_->GetComponent<NavigationMesh>(); navMesh->Build(updateBox); if (currentPath_.Size()) navMesh->FindPath(currentPath_, jackNode_->GetPosition(), endPos_); } }
void Navigation::SetPathPoint() { Vector3 hitPos; Drawable* hitDrawable; NavigationMesh* navMesh = scene_->GetComponent<NavigationMesh>(); if (Raycast(250.0f, hitPos, hitDrawable)) { Vector3 pathPos = navMesh->FindNearestPoint(hitPos, Vector3(1.0f, 1.0f, 1.0f)); if (GetSubsystem<Input>()->GetQualifierDown(QUAL_SHIFT)) { // Teleport currentPath_.Clear(); jackNode_->LookAt(Vector3(pathPos.x_, jackNode_->GetPosition().y_, pathPos.z_), Vector3::UP); jackNode_->SetPosition(pathPos); } else { // Calculate path from Jack's current position to the end point endPos_ = pathPos; navMesh->FindPath(currentPath_, jackNode_->GetPosition(), endPos_); } } }
void Navigation::RecalculatePath() { NavigationMesh* navMesh = scene_->GetComponent<NavigationMesh>(); navMesh->FindPath(currentPath_, startPos_, endPos_); }