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?

| improve this question | | | | |

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.

| improve this answer | | | | |
  • \$\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 Jun 15 '16 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.