简体   繁体   中英

Rotation for rolling 3d ball

I have a Ball in 3D space that has the following attributes:

location - a Vector3f representing where the ball is rotation - a Vector3f representing the x, y, and z axis rotation angles

I want to roll the ball in a particular direction denoted by a Vector3f "direction". How would I calculate the appropriate axis rotation vector (see above) based on the direction I want the ball to roll towards?

I've tried the following:

  1. set rotation.x to direction.z
  2. set rotation.z to direction.x

Calculate the ball's transform matrix as:

private Matrix4f calculateEntityMatrix(EEntity entity)
{
    Matrix4f matrix = new Matrix4f();
    matrix.translate(new Vector3f(entity.getXLocation(), entity.getYLocation(), entity.getZLocation()));

    if(entity.getXRotation()>0)
    {
        matrix = matrix.rotate(entity.getXRotation(), new Vector3f(1f, 0f, 0f));
    }
    if(entity.getYRotation()>0)
    {
        matrix = matrix.rotate(entity.getYRotation(), new Vector3f(0f, 1f, 0f));
    }
    if(entity.getZRotation()>0)
    {
        matrix = matrix.rotate(entity.getZRotation(), new Vector3f(0f, 0f, 1f));
    }
    if(entity.getXScale()!=1 || entity.getYScale()!=1 || entity.getZScale()!=1)
    {
        matrix = matrix.scale(new Vector3f(entity.getXScale(), entity.getYScale(), entity.getZScale()));
    }

    return matrix;
}

This works when rolling down either the x or z axis, but when I roll in a direction between the two axes the rotation appears incorrect. My assumption is that this is caused by the fact that the rotation is being calculated as follows:

  1. the ball is rotated by rotation.x along the X axis
  2. the ball is then rotation by rotation.z along "new" X axis created by step 1.

Any suggestions how this behaviour could be changed so that each rotation is calculated independently of each other?

Unless you want to implement slipping and/or backspin, I think you should approach this problem slightly different. You already have a Matrix.rotate() that supports rotation around an arbitrary axis, use it .

Attributes to store for the ball

  • position
  • rotation matrix

Note: in a matrix-oriented system / scene graph, bould would usually be stored in a single 4x4 transformation matrix. That might or might not be more convenience, depending on the rest of your current code base.

Algorithm for moving around

  1. Given the ball direction and the standard up vector (0, 1, 0) , calculate the rotation axis using the cross product. (ie it's perpendiciular to both direction and up axis)

  2. Rotation is now simply a matrix.rotate( rotationSpeed, rotationAxis) , applied to the existing rotation matrix.

If the ball is allowed to roll up/down surfaces, replace the standard up vector with the appropriate surface normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM