Rotation about the z-axis...

I have a square on screen (2 triangles), and I'm experimenting with rotating the thing. Rotating about the x and y axis works as expected (the thing spins horizontally and vertically), but when I rotate it about the z-axis, it goes up and down, deforms a bit, and just looks weird.

As for how I'm doing it right now, I'm using Matrix.RotationYawPitchRoll(x, y, z) to get my rotation matrix.


Answer this question

Rotation about the z-axis...

  • jrryfn

    yaw pitch roll should be (y, x, z) and not (x, y, z). Since you're using sprites the only rotation you really want is the Z axis, like Rim said. Also try scaling then rotating(x,y,z) then translating, this is what is usually done when setting up the world transform, and translate then rotate(x,y,z) to get your view transform.
  • yousef.net

    I would say it’s when you’re trying to centre your object. You want to factor this value into the translation at the end. For now forget the scaling and rotation and play with translating, then start adjusting the values you pass for translating to find the centre, the bottom centre, etc… It won’t take long and after combine the rotation and scaling.

    The thing to keep in mind is about space. You have the object’s space, it has it’s own (x,y,z) co-ordinates, but the actual object itself can be off-centre to these. Then you have the world space which is where you’re placing the object’s (x,y,z), it’s these co-ordinates that you’re adjusting to bring the object into line with the world. Then you have the view space, which orients the world with respect to the viewer. The thing with this space is that the viewer is always in the centre and it’s the world that moves around it. Think of it this way, if your heads the viewer (camera) and your out-stretched hand is the world, if you want to move the view to the left in reality you’re moving the world to the right. Then you have the screen space (projection), which basically squishes everything to fit nicely on the screen.


  • gfs

    Are you using transformed co-ordinates My mistake, I see that you are, unless it’s a miss-type. You don’t need to mess world and view matrices, since it’s already in screen space, the top left corner of the screen is (0,0), just like using Window’s co-ordinates system. Try the PositionTextured instead, leave the transformed stuff for backdrops and the like.


  • Rabarbers

    Well, you could try using Matrix.RotationZ to see if the Z-angle you're using isn't causing any problems. Other than that, we really need to see some more of your code to be able to tell what might be going wrong

  • SusanHX

    Using CustomVertex.PositionTextured results in a blank screen! hurray! </sarcasm>


    Seriously, I'm starting to lose hope here. How hard can it be to have 2 polygons rotate... T_T

  • Software Arch

    Are you rotating the object before translating it (i.e., as the leftmost matrix)

  • Guruper

    You could try to let DirectX handle the matrix transformations by setting your matrix on the device as the world matrx, by calling d.Transform.World = getMatrix(); This call will replace the temp transformation of the original geometry and you can just have it draw your original geometry vertices in the DrawUserPrimitives call (which will remain unchanged by this). I'm not entirely sure why you would want to apply the transformation yourself, but by letting DirectX handle it you can at least verify that the matrix itself is correct.

    Other than that, you could try using the * operator instead of the Multiply() method to multiply the matrices, like this:

    return center * rotation * translation * scaling;

    It shouldn't make a difference, but I've seen some strange things with this before, so it might be worth a shot. If all else fails, you could also try applying only the rotation matrix to verify if the problem is caused by that one or by another matrix. I noticed you're using height / 2 etc, which may be an int/int fraction and cause undesired results (99/100=0 for example) so using one float term ( height / 2f ) might be a safer approach.

    Hope this helps :)



  • frusciante

    Bad Habit, thanks for the adivice, but I understand the theory pretty well already. My problem is just getting the thing to rotate on the z-axis, and getting d.transform.world to actually work. The other two axis work, as well as scaling and translating, if I apply the matrix myself.

  • Rodrigao

    I noticed that you centre is 0 for z. Any reason it's different than x and y (It's suspicious since Z is what you seem to have trouble with.)

  • &amp;#160;JD&amp;#160;

    We need to see your Perpective Matrix setting and your View Matrix Setting

    Maybe you are in orthogonal mode...


  • dd1000

    Well, the sprite is on the xy-plane, so I shouldn't need to change it from 0 I think... The way I have it now, it's centered on the z-axis.

  • scritchy

    I forgot to mention, I'm not using ints....anywhere.

  • AG_MD

    Ok, so I changed my code a bit, and now nothing happens, other than the sprite being rendered on the topleft corner of the window/screen.

    public Matrix getMatrix()
    {
    Matrix center = Matrix.Translation(-width/2, -height/2, 0);
    Matrix scaling = Matrix.Scaling(scale, scale, scale);
    Matrix rotation = Matrix.RotationYawPitchRoll(yaw, pitch, roll);
    Matrix translation = Matrix.Translation(pos_x, pos_y, pos_z);

    return center * scaling * rotation * translation;
    }

    public void draw(Device d)
    {
    d.SetTransform(TransformType.World, getMatrix());
    d.SetTexture(0, texture);
    d.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, geometry);
    }

    Am I doing something really, really wrong here or something

  • eddieg

    I get the matrix by using the folling method:

    public Matrix getMatrix()
    {
    Matrix matrix;
    Matrix center = Matrix.Translation((float)(-width / 2), (float)(-height / 2), 0);
    Matrix rotation = Matrix.RotationYawPitchRoll(rotation_x, rotation_y, rotation_z);
    Matrix translation = Matrix.Translation(pos_x + width / 2, pos_y + height / 2, pos_z);
    Matrix scaling = Matrix.Scaling(scale, scale, scale);

    matrix = Matrix.Multiply(center, rotation);
    matrix = Matrix.Multiply(matrix, translation);
    matrix = Matrix.Multiply(matrix, scaling);
    return matrix;
    }

    I think the variables are named relevantly enough for you guys to know what I'm doing.

    My draw method looks like this (and I'm sure there's better ways to do this):

    public void Draw(Device d)
    {
    CustomVertex.TransformedTextured[] temp_geometry = new CustomVertex.TransformedTextured[geometry.Length];
    geometry.CopyTo(temp_geometry, 0);

    // set geometry position, scale and orientation
    Matrix m = getMatrix();

    for(int i = 0; i < temp_geometry.Length; i++)
    {
    temp_geometryIdea.X = temp_geometryIdea.X * m.M11 +
    temp_geometryIdea.Y * m.M21 +
    temp_geometryIdea.Z * m.M31 + m.M41;
    temp_geometryIdea.Y = temp_geometryIdea.X * m.M12 +
    temp_geometryIdea.Y * m.M22 +
    temp_geometryIdea.Z * m.M32 + m.M42;
    temp_geometryIdea.Z = temp_geometryIdea.X * m.M13 +
    temp_geometryIdea.Y * m.M23 +
    temp_geometryIdea.Z * m.M33 + m.M43;
    }

    d.SetTexture(0, texture);
    d.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, temp_geometry);
    }

  • Rotation about the z-axis...