1
votes

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!

1
rotate in the direction of a vector ... huh? rotations usually rotate around a fixed axis (that might be given as a vector) in a certain direction, but I do not understand what you want to do. Moreover, what is the problem? You get wrong results? What do you get?463035818_is_not_a_number
Imagine that you have a Pyramid, aka 5 vertices bound by 8 edges... This Pyramide have the center at V3(0, 0, 0) where the pointy vertex have position at V3(0, 0, 1). The other points of the Pyramid lay at V3(+-1, +-1, -1). So, there are 4 vertices in the bottom and 1 in the top -> a Pyramid. I also have a Direction-Vector, this vector determins the direction that the Pyramid should be facing... So what I want to do is, calculate the RoationMatrix based on the Direction-Vector so that I can rotate all the vertices in the Pyramid, so that the top of the Pyramid face the given direction.Marcus Persson
To partially echo tobi303: What's not working? Give us an example the kind of input that's causing problems, the output your code produces for that input, and what you believe the output should be. (Unless the error is obvious --- like a thrown exception or something --- please include why your output is more correct than what the code produces.)Kevin J. Chase
Also, a terminology question: You've used the terms "transformation matrix" and "rotation matrix" seemingly interchangeably (sometimes with "directional" prepended). Are they the same thing in your example, or is there a difference I'm missing? If they're the same, it might help if you picked one term and stuck to it --- probably "rotation matrix", since that's what your code calls it.Kevin J. Chase
Yes, Rotation Matrix... I dont know why I wrote transformation... What I want is to make my 3d objects face a direction and this direction is determined by my "Direction Vector". Also, when an object is facing the direction of the dir-vector, I want to be able to further rotate the object around the dir-vector. I guess what I want to achieve is pitch-yaw-roll rotation? Im making a space-flight shooter/simulator game..Marcus Persson

1 Answers

0
votes

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/