Debug RayCast Ray

Do I need to enable debug data on the physics render to view a single raycast?

You can use DebugRenderer to draw line… There are many posts on this…

e.g.
DebugRenderer* debug = scene_->GetComponent();
debug->AddLine(Vector3(0, 0, 0), Vector3(mx, my, 0), Color(1, 1, 1, 1), false);

1 Like

Oddly I tried this before creating this thread. For some reason it will not render the line. I use it for other things and it works fine.

You need to draw things after the scene drawing is finished. Subscribe to the E_POSTRENDERUPDATE and do your drawings there.

1 Like

Okay I will try this. Seems odd though. I don’t do this for other cases and the lines render fine.

So I subscribe to the E_POSTRENDERUPDATE. I can see the ray briefly before it disappears. I’m try to raycast forward from a rocket. The line seems to be aiming right of the rocket projectile not it’s forward facing. Maybe I have a mistake in my raycast.

PhysicsWorld* physicsWorld_ = scene_->GetComponent<PhysicsWorld>();
PhysicsRaycastResult result;
Vector3 pos(boxNode->GetWorldPosition());
Ray ray(pos, boxNode->GetWorldDirection().FORWARD);  // the given vector is the direction
physicsWorld_->RaycastSingle(result, ray, 250.0f, 1);
debug->AddLine(pos, result.position_, Color::YELLOW,false);

Remove the .FORWARD: A Node's direction is already the local forward. The constant Vector3::FORWARD holds a { 0.0f, 0.0f, 1.0f } vector. Similarly a Node has GetUp() and GetRight() functions which return rotation_ * Vector3::UP and rotation_ * Vector3::RIGHT respectively.

I removed the FORWARD part. The ray is still off. It does not travel the same direction as the moving rocket projectile.

Does it use physics? If so, you may want to use rigidBody_->GetLinearVelocity().Normalized() instead of the Node's direction.

Okay I will try this

So I got the rigid body and used the GetLinearVelocity().Normalized(), but the same thing still happens.

Could you share a screenshot or video?

The rocket is not pointing forward because I removed any rotation on it. I thought this was causing my issue.

The line does seem aligned with the launcher. Maybe a DebugRenderer::AddNode(boxNode) could help to understand what is going on. Did you check the asset’s alignment?

I noticed this as well. I’m sure I was using only the projectiles node though. I just spawn it slightly in front of the launcher. Here is a better screenshot I corrected the projectile rotation.

boxNode->SetPosition(Pos + Vector3(0.0f,0.0f,1.0f));
boxNode->SetRotation(objectNode->GetWorldRotation() * Quaternion(-90, Vector3(0, 1, 0)));

Pos is the launchers world position. I move it forward slightly.

If your asset is not oriented correctly, modify the asset. When this is no option, the correction should happen by attaching the model component to a child node and rotating that (90 degrees left) as to not mess up the logic with it.

Okay I will give this a try.

Is the line pointing towards the world position 0,0,0? You should check if there is a hit, because if not result.position_ will be Vector3::ZERO

That sounds about right. The ray always tries to point a certain direction no matter how much the character rotates.

Added this.

if (result.distance_ < 250)
{
    objectNode->SetWorldPosition(result.position_);
}