0
\$\begingroup\$

TLDR: I'm asking for the direction of the up vector of an entity that can rotate in JavaScript that has a x, y, and rotation (in radians).


I'm using a JavaScript game library. It doesn't have a transform.up like that of Unity. I've been looking around and I ended up with a source code reference of Unity stating the following:

public Quaternion rotation;
public Vector3 up { get { return rotation * Vector3.up; } }

And looking at the multiplication between the Quaternion and Vector3.up:

// Rotates the point /point/ with /rotation/.
public static Vector3 operator*(Quaternion rotation, Vector3 point)
{
    float x = rotation.x * 2F;
    float y = rotation.y * 2F;
    float z = rotation.z * 2F;
    float xx = rotation.x * x;
    float yy = rotation.y * y;
    float zz = rotation.z * z;
    float xy = rotation.x * y;
    float xz = rotation.x * z;
    float yz = rotation.y * z;
    float wx = rotation.w * x;
    float wy = rotation.w * y;
    float wz = rotation.w * z;

    Vector3 res;
    res.x = (1F - (yy + zz)) * point.x + (xy - wz) * point.y + (xz + wy) * point.z;
    res.y = (xy + wz) * point.x + (1F - (xx + zz)) * point.y + (yz - wx) * point.z;
    res.z = (xz - wy) * point.x + (yz + wx) * point.y + (1F - (xx + yy)) * point.z;
    return res;
}

More importantly, the quaternion x, y, z, & w are not in radians and are values between 0 and 1.

But my issue starts with that I have an object that has a x, y, and rotation (in radians). What math and code will I be needing in order to determine that this direction is indeed the up vector of this object? See photo below: photo Pls note that I am not asking "how to rotate around a pivot point"

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This is just an application of the Unit Circle.

If your forward/right direction is

(cosine(angle), sine(angle))

then a perpendicular to that is

(-sine(angle), cosine(angle))

or

(sine(angle), -cosine(angle))

Depending on your coordinate system conventions, one of these will be the up vector and the other the down vector, so choose the one that points in the direction you want to label "up".

Notice that this is just swapping the components of your forward/right vector and negating one of them. So if you already have a forward/right vector you're using for movement/etc, you can compute the up vector from it without any trig functions.

If you want a 0 angle to point in a different direction than the mathematical standard (0 radians = rightward, increasing counter-clockwise), you can similarly swap and negate the sines and cosines as needed to use that angular convention instead. In future, don't forget to specify the angular convention you use in your question itself.

\$\endgroup\$
2
  • \$\begingroup\$ thank you! the (sine, -cosine) worked :) oh and just to clarify, when you mean "angular convention" that's the "rightward is 0th angle, increasing counter-clockwise", right? \$\endgroup\$ Commented Sep 20, 2021 at 16:03
  • \$\begingroup\$ Yes, that's an angular convention. \$\endgroup\$
    – DMGregory
    Commented Sep 20, 2021 at 17:19

You must log in to answer this question.

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