Matrix rotation problem

Started by
0 comments, last by ajoling 21 years, 11 months ago
Hi, I have a small problem regarding the rotation of vertices using matrices. The current problem is that the tree (the object to rotate) is not rotated round it''s center, but around a different point. I''ve worked with matrices before, and I know you have to translate it first to the center, rotate, and translate it back, unfortnatly, all my attemts thus far have failed. For a small image have a look at the picture here: Problem.gif This tree is made of many lines...currently I''m converting these lines to individual vertices every frame, because of the simplicity for the moment. Also, the tree was first made using TL verts, that''s why I''m using the UnProject function to put the tree in object space (the world/view/projection matrices) The code is somewhat a mess, I''ve been messing with it very much ,cause I have been stuck with this problem for 3 days Some more background info, the lines of the tree are defined using: Top and bottom: Top x,y is the highest point of the line (end of the branch) Bottom x,y the start of the branch (lowest part) The code below renders one ''line''/branch part (4 verts) seperately.

    For I = UBound(Lines) To 0 Step -1
        
       D3DXMatrixIdentity matTree
       D3DXMatrixIdentity matTrans
       D3DXMatrixIdentity matWorld
       
       With vertsTree(0)
            With myVec2
                .X = Lines(I).Bottom.X
                .Y = Lines(I).Bottom.Y
                .z = 0
            End With
            Call D3DXVec3Unproject(myVec, myVec2, myView, matProj, matView, matWorld)
            
            .X = myVec.X
            .Y = myVec.Y
            .tu = 0: .tv = 1
            .color = &HFFFFFF
        End With
    
        
        With vertsTree(1)
            With myVec2
                .X = Lines(I).Top.X
                .Y = Lines(I).Top.Y
            End With
            Call D3DXVec3Unproject(myVec, myVec2, myView, matProj, matView, matWorld)
            
            .X = myVec.X
            .Y = myVec.Y
            .tu = 0: .tv = 0
            .color = &HFFFFFF
        End With
        With vertsTree(2)
            With myVec2
                .X = Lines(I).Bottom.X + 4
                .Y = Lines(I).Bottom.Y
            End With
            Call D3DXVec3Unproject(myVec, myVec2, myView, matProj, matView, matWorld)
            
            .X = myVec.X
            .Y = myVec.Y
            .tu = 1: .tv = 1
            .color = &HFFFFFF
        End With
        
        With vertsTree(3)
            With myVec2
                .X = Lines(I).Top.X + 4
                .Y = Lines(I).Top.Y
            End With
            Call D3DXVec3Unproject(myVec, myVec2, myView, matProj, matView, matWorld)
            
            .X = myVec.X
            .Y = myVec.Y
            .tu = 1: .tv = 0
            .color = &HFFFFFF
        End With
       

 Dim sngX As Single, sngY As Single
        sngX = Abs(Lines(I).Bottom.X - Lines(I).Top.X) / 2
        sngY = Abs(Lines(I).Bottom.Y - Lines(I).Top.Y) / 2
        ''Call D3DXMatrixTranslation(matTrans, (Lines(I).Bottom.X - Lines(I).Top.X), -(Lines(I).Bottom.Y - Lines(I).Top.Y), 0)
        Call D3DXMatrixTranslation(matTrans, -sngX, -sngY, 0)
        Call D3DXMatrixRotationZ(matTree, lngAngle * RAD)
        Call D3DXMatrixMultiply(matWorld, matTrans, matTree)
''        Call D3DXMatrixTranslation(matTrans, sngX, sngY, 0)
        Call D3DXMatrixMultiply(matWorld, matWorld, matTrans)
        Call ObjDev.SetTransform(D3DTS_WORLD, matWorld)

        ObjDev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, vertsTree(0), Len(vertsTree(0))

    Next I
 
I hope someone can help me on this... It will probably be so simple, but I just can''t figure it out... Thanks in advance, Almar
www.persistentrealities.com for Inline ASM for VB, VB Fibre, and other nice code samples in C++, PHP, ASP, etc.<br/>Play Yet Another Laser Game!<br/>
Advertisement
The graphics forum would be a better place for this. A transform matrix changes coordinate systems. As an example a rotation is always around the origin so if you want to rotate around a point you change to a coordinate system where the point is the origin. You always have to keep track of what coordinate system you are in.

Generally a model has its own coordinate system. Since you may position the model anywhere in the world it wouldn''t be too handy to have world relative coordinates. The models coordinate system has an origin. If you just rotate the model then that is what you rotate around. So normally you use an origin of the point you want to rotate around by default. So normally you would rotate and scale in the models coordinates. Then you would move it into world coordinates. That is not multiplication by the world transform since the world transform takes you from world coordinates to camera relative coordinates. Rather it is just a translation. You would only do a rotation if you wanted the object to orbit a position in the world.

Using the center of the tree as it''s origin most likely isn''t the best choice. You generally need to position the tree on the ground. So the point you actually need to stick on the ground would make things a lot easier. Otherwise you have to figure out where the center of the tree would have to be given its orientation so that point is on the ground. Not all that difficult, but it is one more bit adding to the overall complexity. Things are difficult enough as it is so always try to make choices that keep things simple.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement