I am trying to add homing rockets to my tank game. So far I have:
rigidBody->SetUseGravity( false );
rigidBody->SetLinearVelocity( Urho3D::Vector3( sin( objectNodeRotation * M_PI / 180 ), 0, cos( objectNodeRotation * M_PI / 180 ) ) * WEAPON_HOMINGROCKET_SPEED );
...
void WeaponHomingRocket::Update( Urho3D::StringHash eventType, Urho3D::VariantMap& eventData ) {
auto* physicsWorld = scene->GetComponent< Urho3D::PhysicsWorld >();
for ( auto i = this->projectiles_.begin(); i != this->projectiles_.end() ; i++ ) {
auto position = i->node_->GetPosition();
Urho3D::PhysicsRaycastResult hitResults;
Urho3D::Ray ray( position, Urho3D::Vector3::ONE );
physicsWorld->SphereCast( hitResults, ray, WEAPON_HOMINGROCKET_HOMING_DISTANCE, WEAPON_HOMINGROCKET_HOMING_DISTANCE, LayerFlagsPlayer );
Urho3D::RigidBody* resultBody{ hitResults.body_ };
if ( resultBody ) {
auto* resultNode = resultBody->GetNode();
auto* resultPlayer = resultNode->GetComponent< Player >();
if ( playerComponent->GetId() != resultPlayer->GetId() ) {
auto resultPosition = resultNode->GetWorldPosition();
i->node_->LookAt( resultPosition );
i->objectNodeRotationCurrent_ = i->node_->GetRotation().YawAngle();
auto* rigidBody = i->node_->GetComponent< Urho3D::RigidBody >();
rigidBody->ApplyForce( Urho3D::Vector3( sin( i->objectNodeRotationCurrent_ * M_PI / 180 ), 0, cos( i->objectNodeRotationCurrent_ * M_PI / 180 ) ) * WEAPON_HOMINGROCKET_HOMING_SPEED );
#ifdef __DEBUG__
auto* debugRenderer = scene->GetComponent< Urho3D::DebugRenderer >();
Urho3D::Sphere sphere( resultPosition, WEAPON_HOMINGROCKET_HOMING_DISTANCE );
debugRenderer->AddSphere( sphere, Urho3D::Color::RED );
#endif
}
}
...
}
}
and it works pretty well.
My problem is
SphereCast()
. None of the samples use it and I can not find it explained in the wiki or on the forums. That along with
Urho3D::Ray
which I have no idea how to use. It half works with this code but often does not track any targets or only does for a brief moment.
At the moment I do not care if it crashes into walls on its way to the target. Once I better understand
Urho3D::Ray
I can probably figure that part out on my own.
So how is
SphereCast()
used with
Urho3D::Ray
?