Mesh on the surface of another mesh. I need help)

Hello, guys!

I need some help again…
I saw some topics RE my problem and implement approaches that I saw there:

But unfortunatelly this approaches doesnt work for me because I want to prevent changing of moving direction.

Problem
So when I move mesh by +Y/-Y +X/-X +Z/-Z surfaces
And when mesh moves from Z surface to Y everything works fine as well as from X to Z etc. But when I move mesh from X to Y I have a strange behaviour that you can see in the video…

PG (Use arrows to move the mesh by surface):

Illustration
And I also prepare video that illustrate the problem:

cc @Cedric

1 Like

Hello :slight_smile:

Then I think your approach is not right, since your are defining the orientation of your box globaly, starting from the picked (ray) point, and the normal of the surface :

box.position = pick.pickedPoint!;
let normal = pick.getNormal() as BABYLON.Vector3;
let quaternion = BABYLON.Quaternion.Identity();
BABYLON.Quaternion.FromUnitVectorsToRef(
    new BABYLON.Vector3(0,0,1),
    normal,
    quaternion
);
box.rotationQuaternion = quaternion;

While you should take into consideration, your speed (delta move from previous position)


Let me explain better.
As far as I understood you are happy with this :
ok
But you are unhappy with this :
no_ok


So what you are looking for (if I understood right) is, when going from X plane to Z plane, you want this :
image


But that’s inconsistent with the working version (first GIF). Have a look when going from Z plane to Y plane, to axis would not be in the same direction (orange points references) :

If the final orientation depends on where you come from, you cannot set it up globally from current position + normal …


I don’t know if it’s clear, ahah :sweat_smile:

++
Tricotou

3 Likes

Hello! Thanks for reply!

You understand me right

This is not problem:
image

For my case changing of axis direction in the way we have on image is fine

Sure ! What I was pointing, is that in the image both axis (on both orange points) have the same normal, while not the same orientation. So they cannot be set by a function which takes only the normal as reference. You must add a new var, such as the move direction…

1 Like

Ahh… Thanks. I will think about how I can do it

Your reply push me very close to solution! (I quess :sweat_smile:)

But there is strange behaviour. Object rotates by 90 degrees when intersects the x->y and stucks when intersects -y -x. Why it could happens?

@Tricotou
I updated the PG and as I understand I took in account direction of my mesh, using “forward”. But I still have this inverting of the axis when I go from +z to +y

Hello @simon

I had a look in your code, I think the problem comes from the new logic you are trying to apply (merging the previous “moving with keyboard forward, backward, left, right” and the new “orientation depending on the move direction”)

I tried to draw the “loop” in a picture. Let’s say forward (up arrow) is toward X>0. No problem.
But now backward is toward X<0


Then :

  1. Backward toward X<0
  2. New orientation with red axis following the move
  3. Backward again toward a new X<0 (still pushing down arrow)
  4. New orientation again
  5. etc… (LOOP)

Using for example the pointer as the new position it would not create this behavior :
Screencast from 03-09-2024 14:06:42

2 Likes

Thank you! I’ll thinking through how I can apply it for my case