moving object while being snapped to another one lwjgl

hi now I want to move an object(cube) while being snapped to the surface of another one (sphere) with the same surface of the cube how can i achieve this in lwjgl here is a diagram. to describe my point thanks in advance ☺️

This problem of course requires a logical system and algorithm. Since I do not know a lot about your Game Engine I can give you only a small glance of a solution.

At first you need the Surface of the Object A the Object B should stick to. If your Object A is like a terrain (in form of a grid) you can get the current 3 nearest vertices to Object B and "interpolate" between them. To do that you can use a barry centric method. I have this one in my Tools class:

public static float barryCentric(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f pos) {
float d = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z))/d;
float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z))/d;
float l3 = 1.0f - l1 - l2;
return l1 * p1.y + l2 * p2.y + l3 * p3.y;


p1, p2 and p3 are the positions of the vertices. pos is the x and z coordinate of the "sticking point" of your Object B. It then returns the y coordinate to make your object stick.

If your object is unfortunaly not always under the player (so that the player like in your diagram must "stick" to it even sideways) you need a more complex algorythm.

You could rotate both objects so that Object B is above Object A (

pseudo-code:
if B is left of surface of A
rotate both 90 degree around center of A
if B is under surface of A
rotate both 180 degree around center of A
...


) adjust the y coordinate of A with the barry centric method and rotate them back. This is just one simple solutions, perhaps there a dozen better or simpler solutions, so just try whats in your mind.

I hope i have helped at least a little bit :) Sorry for my english, its late