# Getting distance between 'floating' objects

Hi.
I’ve been working on this for sometime and can’t work out the correct method, which is probably my vector math knowledge failure!. I’ll try to explain in game terms…

I am adding some objects in to the game world which I want to lie on top of (along) a piece of terrain, that’s say a meteor. The meteor is not aligned (horizontal) to the world axis, so trying to do a Ray collision using 0,1,0 doesn’t work.
How do I find the correct distance between each vertex of a pipe, or building to the meteor to enable me to move the objects to the correct location(s)?
I believe the crux of the problem is in getting the correct direction vector between the vertex position (say 60,-10,230) and the floating meteor terrain which could has been rotated using a quat, translated and scaled.

Either you haven’t provide enough information or your question can be answered by understanding the math tutorial:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

I went through the tutorial and although useful, I didn’t help in working out what my issue was; I thought there maybe a method to rotate a vector based on a Quat, but nothing is looking obvious.

To explain a bit more then…

When the application starts a front: (0.79018366, -0.029632535, -0.6121444), and Up: (-0.61171776, 0.022939915, -0.79074347) vector is generated and used to set a a Quat using the .lookAt(front, up) method.

All spatials added to the scene, including the main Camera, have their local rotation set to this quat, so as the camera is moved using the device sensors the world appears flat (perpendicular to camera). The terrain quad’s are translated to their correct location and also rotated by this Quaternion, so the world still appears to line up with the camera.

The difficulty is creating new spatials that auto-magically sit on top of the terrain as, when using;
[java]
Vector3f worldOrigin = new Vector3f(128,-4, -43);
CollisionResults results = new CollisionResults();

Vector3f direction = new Vector3f(0,1,0);
terrain.collideWith( new Ray(worldOrigin, direction.mult(-1f)), results);
float dist = 0f;
if (results.size() > 0) {
// how to react when a collision was detected
CollisionResult closest = results.getClosestCollision();
dist = closest.getDistance();
System.out.println("Distance? " + dist );

}
[/java]

either no collision results are produced or the distance is not correct (i.e spatials are above or below the terrain).

I appreciate it would make a lot of sense to not rotate everything by the original front and up vectors, but unfortunately due to the real-world coordinate system I’m using this isn’t possible.

I anticipated that using the original Up vector as the Ray direction vector would produce the correct result, but it isn’t, hence the post.

Thanks.

@MikeR said: I went through the tutorial and although useful, I didn't help in working out what my issue was; I thought there maybe a method to rotate a vector based on a Quat, but nothing is looking obvious.

…that is the main topic in the tutorial…?

Okay, less than a dummy! I was thinking Vector3f.mult( quat ), not Quat.mult( Vector3f ) - sorry

Unfortunately it hasn’t helped doing sceneAxisRotation.mult( Vector3f.UNIT_Y ) in terms of getting the correct distance, but at least I know how to rotate stuff!

Is
[java]terrain.collideWith( new Ray(worldOrigin, direction ), results);[/java] definitely the way to go with this type of requirement (placing objects on a terrain)?

or is it better to get the height from the heightmap that the terrain quad is built from?