0
\$\begingroup\$

Assuming i know the geometry and the rotation angle of a cart, how can i calculate the rotation angle of a swiveling wheel?

enter image description here

To get the rotation angle of the wheel, i tried by calculating the distance from the rotation axis, and to apply a friction coefficient:

var dx = cartCX - wheelCX,
    dy = cartCY - wheelCY,
    dist = Math.sqrt(cartCX * wheelCX + cartCY * wheelCY);

var wheelRotation = Math.atan2(-dy, -dx) * dist * friction;

But all this has not brought me any acceptable result.

Which is a simple way to get the wheels rotation angle about the Y axis when the cart is rotating?

I don't need exact physics simulation, just a smooth rotation angle which i can tween. I think there should be no need to use any physics engine, or i'm wrong?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

When in doubt: Fake it.

Instead of calculating the wheels from where the cart is, calculate the wheel direction from where the wheel was.

Something like this:

//Constants, per wheel. I'm assuming Y is up/down
var wheelOffset = Vector3( -40, 0, -10 );
//Variables kept over multiple frames, per wheel
var wheelAngle = 0;
var lastWheelPosition = 0;

//Per frame:
//Calculate the world position of the wheel from the carts transformation matrix, there's other ways if you don't use a matrix but a matrix is probably the most common to use
var wheelPosition = cartTransformation * wheelOffset;

//Calculate how much the wheel moved, from *this* we can then calculate an angle
var wheelDist = wheelPosition - lastWheelPosition;
var newAngle = Math.atan2( wheelDist.x, wheelDist.z );

//This is the final angle we use for this wheel. There's various tweaks you can do to get behaviour you want. The 0.1/0.9 variables adjust how fast a rotation of the cart is send to the wheels
wheelAngle = newAngle * 0.1 + wheelAngle * 0.9;

I may have some signs swapped, this is off the top off my head and not tested.

\$\endgroup\$
2
  • \$\begingroup\$ I really appreciate your help! Using wheels angles was my first attempt, i already have this information stored, so i'm already doing the transformation - but this is apparently not enough. Consider the case where: 1) the wheel is in a stable position, aligned with the rotation circumference after a clock-wise rotation - then, 2) you start to apply a ccw-rotation to the cart- the swivel should lead the wheel to perform an initial 180 degree ccw-rotation, which shall be adeguately tweened. Actually, i'm stuck with this problem... \$\endgroup\$
    – deblocker
    Commented Jun 15, 2016 at 6:55
  • \$\begingroup\$ One trick this is missing is ensuring that angles wrap around nicely from -pi to +pi. It can also help to treat the wheel's pivot/attachment point to the cart as offset from the center of the wheel to help get that castor trailing effect - I show a version of that in this answer. \$\endgroup\$
    – DMGregory
    Commented Dec 21, 2021 at 5:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .