Direct X: Matrix for positioning
Hello everyone :D
I now understand everything of the "graphics" object and 2D rendering, and ready to move on to 3D :)
I have set up / initialized all components needed:
- init the device with needed settings
- made 2 textures and loaded them as "texture" objects
- made a "Geometry" class which it can render sucessfully with the textures
Now the problem.
I need to position my geometry and my camera using 2 vectors:
- position vector3
- rotation vector3
When I use this code:
Code:
Sub Draw(ByVal Position As Vector3, ByVal Rotation As Vector3)
Dim m1 As Matrix = Matrix.Translation(Position)
Dim m2 As Matrix = Matrix.RotationYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z)
Device.Transform.World = Matrix.Multiply(m1, m2)
Device.VertexFormat = CustomVertex.PositionTextured.Format
Device.SetStreamSource(0, Buffer, 0)
Device.SetTexture(0, Me.Textures(0))
Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 10)
Device.SetTexture(0, Me.Textures(1))
Device.DrawPrimitives(PrimitiveType.TriangleList, 30, 2)
End Sub
The problem is that, when I rotate, it rotates it around the [0,0,0] Coordinate. I want it to rotate around the position itself. I tried this:
Code:
Dim m As Matrix = Matrix.RotationYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z)
m.Translate(Position)
Device.Transform.World = m
But now you can't move/rotate at all. I also need this code for the camera ; so I can roll, yaw and pitch properly.
Anyone has any idea or has an example of how to make a matrix for positioning and rotating around the position?
EDIT
Sigh how lame, in maths it doesn't make a different one what side you multiply, but with matrices it does.
This was the so easy solution:
Code:
Public Function GetMatrix(ByVal Position As Vector3, ByVal Rotation As Vector3)
Return Matrix.Multiply(Matrix.RotationYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z), Matrix.Translation(Position))
End Function