Results 1 to 1 of 1

Thread: Rotating a vector around another vector

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Question Rotating a vector around another vector

    I have tried all possible examples found on the web, but I can't get this to work.
    I need to rotate a vector3 around another vector3 in 3D space.

    The vector to rotate around is always [0, 0, 0].

    Basically, I have a second point in 3D space, and it has to rotate in all axis (x,y,z).

    Examples of what I think that has to be done:
    (rotation should be in radians, not in degrees)
    Point = [0, 12, 0]
    Rotation = [0, 0, 90]
    Return [0, 0, 12]

    Point = [12, 0, 0]
    Rotation = [90, 0, 0]
    Return [0, 0, -12]

    Point = [0, 0, -12]
    Rotation = [0, 90, 0]
    Return [0, -12, 0]

    Anyone has any idea? Because when I use:
    Code:
                    Public Function GetNewPos(ByRef Bone As Bone) As Vector3
                        Dim radius As Single = GetDistance(New Vector3(0, 0, 0), Me.RLocation)
                        GetNewPos = Me.RLocation
    
                        Dim crotz As Single = GetPointRotation(New Drawing.Point(GetNewPos.X, GetNewPos.Y))
                        crotz += Bone.Rotation.Z
                        GetNewPos.X += Math.Cos(crotz) * radius - GetNewPos.X
                        GetNewPos.Y += Math.Sin(crotz) * radius - GetNewPos.Y
    
                        Dim crotx As Single = GetPointRotation(New Drawing.Point(GetNewPos.X, GetNewPos.Z))
                        crotx += Bone.Rotation.X
                        GetNewPos.X += Math.Cos(crotx) * radius - GetNewPos.X
                        GetNewPos.Z += Math.Sin(crotx) * radius - GetNewPos.Z
    
                        Dim croty As Single = GetPointRotation(New Drawing.Point(GetNewPos.Y, GetNewPos.Z))
                        croty += Bone.Rotation.Y
                        GetNewPos.X += Math.Cos(crotx) * radius - GetNewPos.X
                        GetNewPos.Z += Math.Sin(crotx) * radius - GetNewPos.Z
    
                        'GetNewPos.X += Math.Cos(crotz) * radius
                        'GetNewPos.Y += Math.Sin(crotz) * radius
                        'GetNewPos.Z += Me.RLocation.Z
    
                        'GetNewPos.Y += Math.Cos(croty) * radius
                        'GetNewPos.Z += Math.Sin(croty) * radius
                        'GetNewPos.X += Me.RLocation.X
    
                        'GetNewPos.X += Math.Cos(crotx) * radius
                        'GetNewPos.Z += Math.Sin(crotx) * radius
                        'GetNewPos.Y += Me.RLocation.Y
    
                        Return Vector3.Add(GetNewPos, Bone.Position)
                    End Function
                End Structure
    It is used to rotate a cube by changing the points (it is for a basic bone system) One direction works, but when I use combined rotations it fails. (stretches in the weirdest positions).
    The commented-out code is the code that works when used alone (only one of the 3 sections for only one rotation)

    The GetPointRotation function returns the rotation in radians relative to [0,0,0]:
    Code:
        Public Shared Function GetPointRotation(ByVal Point As Point) As Single
            Dim width As Integer = Math.Abs(0 - Point.X)
            Dim height As Integer = Math.Abs(0 - Point.Y)
            Dim r As Double = Math.Sqrt(width ^ 2 + height ^ 2)
            If r = 0 Then
                Return 0
            Else
                If Point.X >= 0 And Point.Y >= 0 Then
                    Return Math.Atan(height / width)
                ElseIf Point.X <= 0 And Point.Y >= 0 Then
                    Return Math.Atan(width / height) + 0.5 * Math.PI
                ElseIf Point.X <= 0 And Point.Y <= 0 Then
                    Return Math.Atan(height / width) + Math.PI
                ElseIf Point.X >= 0 And Point.Y <= 0 Then
                    Return Math.Atan(width / height) + 1.5 * Math.PI
                End If
                Return 0
            End If
        End Function
    Last edited by bergerkiller; Oct 17th, 2010 at 08:45 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width