Capsule rotation

Hi, I am trying to implement capsule to capsule collision avoidance. While they do correctly, capsules orientation is not correct. My intention is to capsule’s facing direction (pink arrow) needs to be parallel to it’s velocity direction (blue arrow). For that, I do rotation. But that rotates the capsules but there are lot of oscillations in rotations as their velocity change frequently. how can i make it better? or is there any better ways to make capsules to orient to their facing direction?

Thanks.

const position = new Vector3();
const direction = new Vector3();

capsule.getWorldPosition(position);
capsule.getWorldDirection(direction);

const target = position.add(direction);
capsule.lookAt(target);

Consider avoiding doing 3D rotation math by hand like fire - things like sin / cos / atan2 are often suggested in OpenGL / SO answers, but are not the easiest solutions in most cases - it’s quite extremely easy to accidentally mess something up and end up spending whole weekend debugging wrong order / quadrants / gimbal-lock.

2 Likes

I’m just curious why it is item.px in the calculation of dz.

Hi @PavelBoytchev ,
It was a typo. I have corrected it. thanks for mentioning.

I see.

Just to clarify, you have the left situation and you want to get the right situation? I may have a look at it in half a day, if you have no solution by then.

image

Hi @mjurczyk , I tried your suggestion as below:

But nothing happens, capsules stuck in the middle. Also, one capsules front direction is in the wrong side (as indicated by blue circle). Shouldn’t the velocity direction be normalized? Also, in position vector, should the vector contain the current position of the capsules or the predicted position in the next step?

Could you please help me in to find it out?

Thank you.

Hi @PavelBoytchev , thank you for your comment again. Yes, you are correct. Right one’s orientation is correct.

Hi @PavelBoytchev , please let me know if you have any better solution for me. Thank you.

To make rotations easier, you have to pick suitable orientations, rotations, axes. I picked the following setup:

  • capsules are in XY plane (see lines 74-75)
  • capsules roll to indicate their “forward” direction (see line 130)
  • capsule rotation order is set to ZXY (see line 76)

Because of this setup:

  • rolling is just rotation around capsule’s Y axis (see line 131)
  • rotation towards the forward direction uses capsule’s Z axis (see line 118)

Here is the demo, rotation towards the velocity vectors is done in lines 116-119:

https://codepen.io/boytchev/full/oNVVoQW

image

2 Likes

hi @PavelBoytchev , i really appreciate your effort.
But as I shared earlier, changing capsules orientation change using their velocity or predicted position direction doesn’t help in my case. It causes oscillation.

Do you have any other suggestions other than using velocity direction to smoothen the orientation changes?

Thanks

I’m significantly confused. It appears that I have completely misunderstood the issue. My suggestion is how to orient a capsule to face a direction. How is this direction calculated is off-topic. If the direction changes violently, the capsule will rotate violently.

The second video that you posted reminds me of two possible cases:

  • your engine works by probing different directions and this causes the fluctuations in the vectors
  • or your engine has numerical instability, which might happen when the simulation steps are much larger than changes in values

In any case, if you want the orientation of capsules to be smooth, although the velocities are not, try these approaches:

  • If there is another property that is smooth, then use this property. For example, if the trajectory is smooth, use the trajectory for the orientation, not the velocity vector.
  • If there is no such property, then try to lerp the motion. Lerping will smooth the orientation to some extent, at the cost of some lagging. The problem is that lerping uses only current and old values, so when a new value is drastically different, lerp has no idea whether this is some random fluctuation or not.
  • Experiment with finer simulation steps. If the step is too big, your calculations may leap over the desired values, then leap over backward and so on.
1 Like