Test if two spatials are in right angle


I have two Spatials (viewerSource, geom) and I want to figure out whether the geom is relative to the viewerSource’s translation and rotation ± a specific angle.

It’s hard to describe without knowing the right words, here’s an image:

I want to test if the target (orange) is between the green lines.

I tried to write some code. My current solution is not right, but I have no idea how to solve that. Can somebody please tell me, how to do that right?

My current code is:

final Vector3f viewLocation = tmp.vect1.set(viewerSource.getLocalTranslation());
final Vector3f targetLocation = tmp.vect2.set(geom.getLocalTranslation());

final Vector3f viewDirection = tmp.vect4.set(Vector3f.UNIT_Z);

final float distanceBetween = tmp.vect5.set(viewLocation).distance(targetLocation);
final Vector3f testLocation = tmp.vect6.set(viewDirection).multLocal(distanceBetween).addLocal(viewLocation);


You can use Vector3f.angleBetween

targetDirection=new Vector3f(targetLocation).substractLocal(viewLocation).normalizeLocal()
1 Like

lol, I played with dot() and such stuff, but it is soooo easy. xD
Thank you very much.

Angle between is essentially using dot() and then passing it through a (relatively expensive) trig call.

For reference, and presuming this might be for a steering behavior, the typical approach would be to dot with the left vector.

Vector3f leftDir = driver.getWorldRotation().mult(Vector3f.UNIT_X);
Vector3f relativeOffset = target.getWorldTranslation().substract(driver.getWorldTranslation());
Vector3f relativeDir = relativeOffset.normalize();
float steer = leftDir.dot(relativeDir);

Where steer will be between -1 and 1 for steering right or left suitable for plugging directly into a steering behavior.

angleBetween() should give you -180 to 180 which is probably desirably for your narrow use-case.
the ‘steer’ approach gives -1 to 1 but that means -90 to 90… things behind you would still need to be detected… but usually for steering you also do an unnormalized dot with the forward vector.

float forward = forwardDir.dot(relativeOffset);

…which lets the steering behavior decide if it has arrived yet or if the target is behind them, whether they should turn or back up, etc…


Thank you very much for explaining.

1 Like