Best method for adjusting collision ray

Hello everyone.

I have a node that casts a ray in front of it to detect impending collisions. However, just casting the ray from the node’s origin means that it can detect itself.

To get around this, I offset the ray so it starts outside the node’s boundary:

[java]Vector3f origin = headNode.getWorldTranslation()
Vector3f dir = headNode.getWorldRotation().getRotationColumn(2).normalize()

Vector3f offset = dir.mult(3) // ray should start 3 units along the direction vector
offset.addLocal(origin)

Ray ray = new Ray(offset, dir)
rootNode.collideWith(ray, res)[/java]

This works ok, but it seems convoluted. Is this the best way to do it?

Whats convoluted about it?

It feels as though there are too many steps - a quaternion, to a vector, to a normalised vector, which is scaled and then added to the origin - to do something that I’m guessing is fairly common for ray casting and collision avoidance.

I’m wondering if there’s a convenience method I’m missing. But from your answer I’m assuming not.

You could just skip self in the collision detection when you look at the results returned?

@davekidd said: Hello everyone.

I have a node that casts a ray in front of it to detect impending collisions. However, just casting the ray from the node’s origin means that it can detect itself.

To get around this, I offset the ray so it starts outside the node’s boundary:

[java]Vector3f origin = headNode.getWorldTranslation()
Vector3f dir = headNode.getWorldRotation().getRotationColumn(2).normalize()

Vector3f offset = dir.mult(3) // ray should start 3 units along the direction vector
offset.addLocal(origin)

Ray ray = new Ray(offset, dir)
rootNode.collideWith(ray, res)[/java]

This works ok, but it seems convoluted. Is this the best way to do it?

Presuming no scaling:
Vector3f offset = headNode.localToWorld( new Vector3f(0, 0, 3), null ); // could pass a constant
Vector3f dir = headNode.getWorldRotation().mult( Vector3f.UNIT_Z );

If it’s scaled, just factor that into the 3, ie: 3 * headNode.getWorldScale().z