Rotation & 3d math
Rotation & 3d math
Rotation & 3d math
Rotation & 3d math
Rotation & 3d math
Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math
Rotation & 3d math Rotation & 3d math
Rotation & 3d math
Go Back  Xtreme Visual Basic Talk > > > > Rotation & 3d math


Reply
 
Thread Tools Display Modes
  #1  
Old 04-11-2005, 06:56 PM
Chris Ara Chris Ara is offline
Centurion
 
Join Date: Feb 2005
Posts: 176
Question Rotation & 3d math


Once again on the prowl for directx and 3d knowledge.
I have two questions to ask. The first one is the probally the most simple so I'll start there.

I wantto know how to rotate a matrix around it's current axis?
say I have a matrix and I translate it from it's origin (0,0,0) by say (50,-25,100) now I want to rotate the Z axis around its current position 100 units back. if I use the direct3d matrix multiply it will automatically default to the origin (0,0,0) and rotate around the Z axis at 0 ,

matrixOne = matrix.multiply(matrixone,matrix.rotationz(Z * math.pi / 180))

doing this will cause the image to jump back to the origin (0,0,0) and rotate there when I want it to rotate it in place around its new origin (50,-25,100)

I have tried building a transform matrix using matrix one but I don't to much about this function and can't figure it out

Some one help me out here how is this done?

O.k the second question might be a little simpler can some one point in the direction of a tutorial on converting 3d co-ordinates to 2d Screen space or maybe explain the basics to me?

thanks a lot

Chris
Reply With Quote
  #2  
Old 04-13-2005, 02:32 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

With regards your first question, could you post the bit of code where you are trying to do the rotation. I might be able to help you on this if I can see what code you already have.

As for the second question, have you done a search on google?
__________________
Cheers
Reply With Quote
  #3  
Old 04-13-2005, 05:11 PM
Chris Ara Chris Ara is offline
Centurion
 
Join Date: Feb 2005
Posts: 176
Default

Code:
   Public Sub RotateTank(ByVal Tank As Integer)
        If Tank = 1 Then
            TankOneCurrentTexture += 1
            If TankOneCurrentTexture >= 15 Then
                TankOneCurrentTexture = 1
            End If
            TankOneMatrix = Matrix.Multiply(TankOneMatrix, Matrix.RotationZ(Z * Math.PI / 180))
            Exit Sub
        ElseIf Tank = 2 Then
            TankTwoCurrentTexture += 1
            If TankTwoCurrentTexture >= 15 Then
                TankTwoCurrentTexture = 1
            End If
            TankTwoMatrix = Matrix.Multiply(TankTwoMatrix, Matrix.RotationZ(Z * Math.PI / 180))
            Exit Sub
        ElseIf Tank = 3 Then
            TankThreeCurrentTexture += 1
            If TankThreeCurrentTexture >= 15 Then
                TankThreeCurrentTexture = 1
            End If
            TankThreeMatrix = Matrix.Multiply(TankThreeMatrix, Matrix.RotationZ(Z * Math.PI / 180))
            Exit Sub
        ElseIf Tank = 4 Then
            TankFourCurrentTexture += 1
            If TankFourCurrentTexture >= 15 Then
                TankFourCurrentTexture = 1
            End If
            TankFourMatrix = Matrix.Multiply(TankFourMatrix, Matrix.RotationZ(Z * Math.PI / 180))
            Exit Sub
        End If

    End Sub
what I am trying to do here is make a tank game and the basic movements of the tanks is one button would rotate the tank left or right and the other would move the tank foward in the direction the tank is facing after the rotation but my problem is when I rotate any of tanks matrixs they always start rotating around the origin of the matrix (0,0,0) when I want the tank to rotate in place in it's current position. kind of like when you rotate a 3d model in a modeller how it just rotates in a static position and not aroung the worlds origin

I have tried using the yaw , pitch , roll function to multiply with the matrix but it still defaults back to the origin

I know this problem has something to with the matrixs transform settings because when you multiply the view matrixs transform by a rotation matrix it rotates in its current spot not around the origin

Code:
Public Sub RotateCamera(ByVal Vec As Vector3)
        Device.Transform.View = Matrix.Multiply(Device.Transform.View, Matrix.Rotatioz(Z * math.pi / 180))
    End Sub
this is one of the reason I am trying to learn myself how matrixs work and how 3d points are converted to 2d space because with this knowledge learing to program in 3d will be very hard and what I know about the way a computer works in 3d would be superficial. just mindless building off of someones else's hard work

ok enough rambling , back to the drawing board
much gratefull for any help

chris
Reply With Quote
  #4  
Old 04-14-2005, 02:12 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Talking

I understand what you want to do with your code. This is a sub that I use which rotates the object on it's current axis.

Code:
Public Sub MoveObj(ByVal scaleX As Single, ByVal scaleY As Single, ByVal scaleZ As Single, _
                        ByVal angleX As Long, ByVal angleY As Long, ByVal angleZ As Long, _
                        ByVal posX As Single, ByVal posY As Single, ByVal posZ As Single)
        ' Create matworld
        Dim CosRx As Single, CosRy As Single, CosRz As Single
        Dim SinRx As Single, SinRy As Single, SinRz As Single
        Dim mat1 As Matrix
        CosRx = CSng(Math.Cos(angleX * Rad)) 'Used 6x
        CosRy = CSng(Math.Cos(angleY * Rad)) 'Used 4x
        CosRz = CSng(Math.Cos(angleZ * Rad)) 'Used 4x
        SinRx = CSng(Math.Sin(angleX * Rad)) 'Used 5x
        SinRy = CSng(Math.Sin(angleY * Rad)) 'Used 5x
        SinRz = CSng(Math.Sin(angleZ * Rad)) 'Used 5x
        With mat1
            .M11 = (scaleX * CosRy * CosRz)
            .M12 = (scaleX * CosRy * SinRz)
            .M13 = -(scaleX * SinRy)
            .M21 = -(scaleY * CosRx * SinRz) + (scaleY * SinRx * SinRy * CosRz)
            .M22 = (scaleY * CosRx * CosRz) + (scaleY * SinRx * SinRy * SinRz)
            .M23 = (scaleY * SinRx * CosRy)
            .M31 = (scaleZ * SinRx * SinRz) + (scaleZ * CosRx * SinRy * CosRz)
            .M32 = -(scaleZ * SinRx * CosRz) + (scaleZ * CosRx * SinRy * SinRz)
            .M33 = (scaleZ * CosRx * CosRy)
            .M41 = posX
            .M42 = posY
            .M43 = posZ
            .M44 = 1.0#
        End With
        GFX.SetTransform(Direct3D.TransformType.World.World, mat1)
    End Sub
Hope this helps!
__________________
Cheers
Reply With Quote
  #5  
Old 04-24-2005, 11:10 AM
Chris Ara Chris Ara is offline
Centurion
 
Join Date: Feb 2005
Posts: 176
Cool None


Maybe I'm not using your function right or something but it's seem to me your code for rotation is the same as doing this

Code:
Public Sub RotateZ(ByVal Angle As Double)
        Angle = Z * Math.PI / 180
        TankOneMatrix.M11 = Math.Cos(Angle)
        TankOneMatrix.M12 = Math.Sin(Angle)
        TankOneMatrix.M21 = -Math.Sin(Angle)
        TankOneMatrix.M22 = Math.Cos(Angle)
        TankOneMatrix.M33 = 1
        TankOneMatrix.M44 = 1
    End Sub
it does rotate the tank around it's current matrix but not the way I am looking for , I'm trying to get the object to spin in place like if you stood and the middle of your room and just spun around , this code is giving the effect of like if you where walking around a pole it does rotate but not without moving the object in huge loop ....... maybe I don't understand the math or something I do need to brush up on my geometry or maybe it's just the computer
(Gives the computer a threatening look) " Do as I say , not as I type. "
Reply With Quote
  #6  
Old 04-25-2005, 02:06 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

So you want to just rotate the tank on a single axis? If this is the case then use the function like so:
Code:
' In your rendering part
Angle+=1
MoveObj(1,1,1,0,AngleX,0,posX,posY,posZ)
This will spin the object on it's current position. That is if I've understood what you've put.
__________________
Cheers
Reply With Quote
  #7  
Old 12-06-2005, 07:38 PM
Chris Ara Chris Ara is offline
Centurion
 
Join Date: Feb 2005
Posts: 176
Talking Back at it again

Hey it has been a while since I last worked on my 3d Engine here.
I am still working on the basics with rotation though

I Figured out how to make an object rotate around its current axis in odd ways though

here m is the matrix

Code:
Public Sub RotateAroundAxis()

        M = Matrix.Multiply(M, Matrix.RotationAxis(GetCurrentAxis, 0.15))

    End Sub
but when you do this the object rotates in a wild fashion
what I need is the object to rotate around its current matrix location in a specific direction

such as just have it rotate to the left or right like on the X axis

like when I hold a soda bottle and spin it in one direction on my desk

little help please

thanks a lot chris
Reply With Quote
  #8  
Old 12-07-2005, 01:52 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

What about using the following:

Code:
Public Sub RotateAbs(ByVal pX As Single, ByVal pY As Single, ByVal pZ As Single)
M = Matrix.RotationYawPitchRoll(pY, pX, pZ)
End Sub
__________________
Cheers
Reply With Quote
  #9  
Old 12-07-2005, 05:05 PM
Chris Ara Chris Ara is offline
Centurion
 
Join Date: Feb 2005
Posts: 176
Cool Nope

Quote:
Originally Posted by ardman
What about using the following:

Code:
Public Sub RotateAbs(ByVal pX As Single, ByVal pY As Single, ByVal pZ As Single)
M = Matrix.RotationYawPitchRoll(pY, pX, pZ)
End Sub
No I fiddled with this a bit but it didnt do what I was looking for thanks anyway
Reply With Quote
  #10  
Old 12-08-2005, 01:52 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

Code:
Private _rotationX, _rotationY, _rotationZ As Single

Public Sub RotateRel(ByVal pX As Single, ByVal pY As Single, ByVal pZ As Single)
_rotationX = pX
_rotationY = pY
_rotationZ = pZ
M = Matrix.RotationYawPitchRoll(_rotationX, _rotationY, _rotationZ)
End Sub
Try this piece of code. Slightly different to before.
__________________
Cheers
Reply With Quote
  #11  
Old 12-30-2005, 11:55 PM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

A matrix cannot be rotated.

A matrix can be used to apply rotations, translations, scalings, and skewings to a vector or set of vectors, in any combination and in any order, in one matrix * vector multiplication.

Now, from what I gather.. you have a matrix which already performs a transformation (at a minimum, its already translating) and you wish to add a rotation around an arbitrary point P at the tail of this transformation

Now, I don't know the specifics of DirectX under .NET so I dont know if you've got operator overloading or whatnot on your matrices.. but basic mathematical the process is:

NewMatrix = TranslationMatrix(p) * RotationMatrix(angle) * TranslationMatrix(-P) * OldMatrix

..ie, append a translation back to the origin (-P) followed by your rotation, followed by a translation back to the original position (P)
Reply With Quote
  #12  
Old 03-15-2009, 12:05 PM
Chris Ara Chris Ara is offline
Centurion
 
Join Date: Feb 2005
Posts: 176
Default

I know this old but here is the answer I found

Public Sub RotateObject( ByVal Degree As Single,ByVal MeshMatrix as Matrix)

Dim N As New Matrix
N = MeshMatrix 'save your meshes original point
MeshMatrix = Matrix.Translation(0, 0, 0) 'move your mesh to center or origin
MeshMatrix = Matrix.Multiply(MeshMatrix, Matrix.RotationY((Math.PI / 180) * Degree)) 'rotate mesh
MeshMatrix = Matrix.Multiply(MeshMatrix, N) 'move mesh back to original point

End Sub

enjoy :>
Reply With Quote
  #13  
Old 03-22-2009, 08:55 AM
Leade's Avatar
Leade Leade is offline
Junior Contributor
 
Join Date: Jul 2005
Location: Wherever i fall over
Posts: 222
Default

The simplest way to transform and to take into acount scaling rotation and position is to use the built in function on the matrix. its what i use and it works fine, you may have to calucalte the angles using radians but its a lot easier than the other processes.

This code will allow you to position a mesh and allow for sling and rotation, all you have to do is edit the variables.


Code:
Quaternion ROTATION = Quaternion.Identity;              //Default value
Vector3 PIVOT, POSITION = new Vector3();
Vector3 SCALE = Vector3(1,1,1);                             //Default value
Matrix MAT;

MAT.Transform(PIVOT, ROTATION, SCALE, PIVOT, ROTOTAION, POSITION);


Now to get the screen position of a 3D vector use this code. the code is in C#, but the process is the same in vb.net

Code:
Vector3 Vect = Vector3.Unproject(Position, Viewport, CameraProjectionMatrix, CameraViewMarix, WorldMatrix);
if u want to go the other way around


Code:
Vector3 Vect = Vector3.Project(Position, Viewport, CameraProjectionMatrix, CameraViewMarix, WorldMatrix);
__________________
Fact: The best ideas come when your smashed of your face. So drink beer and be merry!!!

Last edited by Leade; 03-22-2009 at 09:27 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Rotation & 3d math
Rotation & 3d math
Rotation & 3d math Rotation & 3d math
Rotation & 3d math
Rotation & 3d math
Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math Rotation & 3d math
Rotation & 3d math
Rotation & 3d math
 
Rotation & 3d math
Rotation & 3d math
 
-->