简体   繁体   中英

How to compute the Rotation-Matrix based on a Direction-Vector?

In my 3D world implementation I use Direction-Vectors (unit vector) to decide the orientation of my 3D-objects.

Each 3D-object has its own Direction-Vector which by default has the orientation V3(1, 0, 0) with Origin at V3(0,0,0).

This is how I apply the directional Rotation-Matrix "D" (the matrix "A" is used to rotate 3D-objects around their Direction-Vector as an axis, this seems to work fine) :

Model3D model = actor.model;
// Loops through all the edges in the model
for (int i = 0; i < model.edges.length; i++) {
    M3 D = directionMatrix(actor);
    M3 R = rotationMatrix(actor);
    // Draws a line based on each edge in the model.
    // Each line consists of two points a and b.
    // The matrix R rotates the points around a given axis.
    // The D matrix rotates the points towards a given axis - not around it.
    S.drawLine(g,
        D.mul(R.mul(model.points[model.edges[i].a])).scale(actor.scale),
        D.mul(R.mul(model.points[model.edges[i].b])).scale(actor.scale)
    );
}

This is how I calculate my current directional Rotation-Matrix "D":

public M3 directionalRotationMatrix(c_Actor3D actor) {
    double x =  Math.atan2(actor.direction.z, actor.direction.y);
    double y =  Math.atan2(actor.direction.x, actor.direction.z);
    double z =  Math.atan2(actor.direction.y, actor.direction.x);
    double sin_x = Math.sin(x), sin_y = Math.sin(y), sin_z = Math.sin(z);
    double cos_x = Math.cos(x), cos_y = Math.cos(y), cos_z = Math.cos(z);
    return new M3(
            cos_x * cos_y, (cos_x * sin_y * sin_z) - (sin_x * cos_z),
            (cos_x * sin_y * cos_z) + (sin_x * sin_z), sin_x * cos_y, (sin_x * sin_y * sin_z) + (cos_x * cos_z),
            (sin_x * sin_y * cos_z) - (cos_x * sin_z), -sin_y, cos_y * sin_z, cos_y * cos_z);
}

My problem is to create the correct directional Rotation-Matrix that rotates the 3D-objects in the direction of their respective Direction-Vectors.

I'm not sure at all what I do wrong... My idea is to first rotate the cube towards a direction, then rotate the cube around the axis of the direction. After all that comes position transformation etc.

Thank you for your help guys!

Sounds like you are trying to move a 3D object in the direction of it's forward facing vector. To do this you will need the position of the object (x,y,z) and 3 vectors (forward, up, and right). You can rotate the 3 vectors using pitch yaw and roll based vector math(see below link). For the forward movement you then add the position of the object plus the speed multiplied by the forward vector, ie: position += speed * forward

Use the following complete example code posted here to figure out how to implemented your own version. http://cs.lmu.edu/~ray/notes/flightsimulator/

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