Sorry about that… I guess using the word 'slope' in a 3D environment is sort of out of context, I am glad you got it working though. Next time you can try asking me to write some sample code and save the time thinking
Mindgamer said:
So here is a perfectly simple and working code of how to get world coordinates at a specified plane with mouseray for those fellow newbies who are interested :)
The newbie is already here. Thanks to Rik who send me a link to this topic and thanks to all who contributed to this topic, which helped me a lot. Now I've adapted this code and restricted it on 1 Axis only (so to translate an object on a single Axis only. It works quite nice (the spatial following the mouse each frame), though I am experiencing some last problems: when I face the x-Axis with my camera everything works fine... but when I face the z-Axis (or lets say: the more I rotate my camera in the direction of the z-Axis) the translation of the spatial gets somehow "jittery" when moving the mouse too much left or right of the object. The spatial then slides very fast to the top or the bottom of the screen (or further out of sight)
Maybe I need to "rotate the Axis" by the angle I rotate my camera? Or is this wrong/complicated thinking and there is a simpler solution?
// Get the position on screen
int mouse_x = MouseInput.get().getXAbsolute();
int mouse_y = MouseInput.get().getYAbsolute();
Vector2f mouse_xy = new Vector2f(mouse_x,mouse_y);
// Get the world location of that X,Y value
Vector3f worldCoords = DisplaySystem.getDisplaySystem().getWorldCoordinates(mouse_xy, 0);
Vector3f worldCoords2 = DisplaySystem.getDisplaySystem().getWorldCoordinates(mouse_xy, 1);
Vector3f direction = worldCoords2.subtractLocal(worldCoords).normalizeLocal();
Ray mouseRay2 = new Ray(worldCoords, direction);
float planeZ = spatial.getLocalTranslation().z;
float startZ = mouseRay2.origin.z;
float endZ = mouseRay2.direction.z;
float coef = (planeZ - startZ) / endZ;
float planeX = mouseRay2.origin.x + (coef * mouseRay2.direction.x);
float planeY = spatial.getLocalTranslation().y;
spatial.setLocalTranslation(planeX,planeY,planeZ);
Also I would like to ask what this specific line does: float coef = (planeZ - startZ) / endZ; I know that this calculation serves as a multiplicate factor and that it works fine in the end. But why use (planeZ-startZ)/endZ and not use some constants like 10.0 or 100.0. or only the startZ value? Maybe stupid question but I can't figure it out (though some testing showed me that using some constants result in worse). It's just the theory behind this single line I am interested in. I would not have come to this line myself.