XNA Creators Club Online
Page 1 of 1 (15 items)
Sort Posts: Previous Next

Calculating a XYZ-pos for a vector with rotation in XYZ

Last post 8/10/2009 3:37 PM by Fabian Viking. 14 replies.
  • 6/10/2009 4:30 PM

    Calculating a XYZ-pos for a vector with rotation in XYZ

    I normally think this is easy in 2d, but now I am a bit stuck..

    I have a 3d-Vector with the length 1
    I have rotation in XYZ
    How do I get the position of the vector3
  • 6/10/2009 4:45 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    TG Viking:
    I have a 3d-Vector with the length 1
    I have rotation in XYZ
    How do I get the position of the vector3

    I'm not quite understanding. You have a Vector3 that contains the rotation of an object, and you want to extract the position from it? Vector3s can only hold one type of data at a time - if you're using it for rotation then I don't see how you could also fit position in there.

    Are you thinking of a Matrix, which can hold position, rotation and scale? If so, it is very difficult to extract anything out of it once it's inside the matrix.

    What I would do is keep the position and rotation as Vector3s, and combine them into a Matrix when you need to render.

    Or am I misunderstanding the problem?
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 6/10/2009 5:12 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    The formula will look something like:

    Vector3 Rotation; //Known value
    float Length = 1; //Known value

    Vector3 position; //I want to calculate this

    position.X = (float)Math.Sin(Rotation.Z) * Length;
    etc.
  • 6/10/2009 5:32 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Answer
    Reply Quote
    I see now. I'm not sure if there's a built in way of doing that (I haven't seen one), but here's one way I thought of to do that:

    Create a matrix from your rotation and get the forward position, move your object by that amount * length, and then use the resulting position.
    Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z);
    Vector3 ObjectPosition = Vector3.Zero; //you may want to set this to another position 
    ObjectPosition += (rotationMatrix.Forward * Length); 

    Is that what you need?
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 6/10/2009 6:26 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Thanks a lot! It does almost work now, I still got some weird bug, but I think I will figure it out.
  • 6/10/2009 6:49 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Ok, it looks like Matrix.Forward only moves the object in two dimensions, it doesn't matter what Rotation.Z is, the outcome is always the same.
  • 6/10/2009 7:40 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Answer
    Reply Quote
    Interesting! I didn't know that. That could explain a very weird bug I was having a while ago.

    Maybe you can use a combination of Matrix.Forward and Matrix.Down?
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 6/10/2009 9:28 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Solved!

    I works now, don't really know what I did wrong but it works now. The goal was to make one 3d object(child) follow another(parent). I still doesn't work if the child have any rotation.

    childImage.Position = parent.Position + parent.Scale * Position_Relative; 
                childImage.Scale = parent.Scale * Size_Relative; 
                if (parent.Rotation != Vector3.Zero) 
                { 
                    childImage.Rotation = parent.Rotation + Rotation_Relative; //This doesn't work yet 
                    Vector3 posDiff = childImage.Position - parent.Position; 
                    if (posDiff != Vector3.Zero) 
                    { 
                        Vector3 vectorRotation = Vector3.Zero; 
                        float vectorLength = posDiff.Length(); 
                        vectorRotation.X = ((float)Math.Asin(posDiff.X / vectorLength) + parent.RotateX); 
                        vectorRotation.Y = (float)Math.Asin(posDiff.Y / vectorLength) + parent.RotateY; 
                        vectorRotation.Z = ((float)Math.Asin(posDiff.Z / vectorLength) + parent.RotateZ); 
     
                        Matrix calcPos = Matrix.CreateFromYawPitchRoll(vectorRotation.X, vectorRotation.Y, vectorRotation.Z); 
                        childImage.Position = calcPos.Forward * vectorLength + parent.Position; 
                    } 
                } 


  • 6/11/2009 1:42 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Answer
    Reply Quote
    If you need to point something at another object, there's a great chunk of code in the FAQs that may help: link.

    Glad you got it working. Problems like this make me miss the good old days of Blitz3D with it's PointEntity and MoveEntity commands...
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 6/11/2009 2:04 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Thanks a lot, rigth now I am trying to figure out how the child object should rotate when it's parent is rotating. I guess I should see the two object facing in two vector3-directions and use matrixes to add them together somehow.

    childImage.Rotation = parent.Rotation + Rotation_Relative; //Wrong way to do it

    Ubergeek: your signature "This signature is to big fore the 512-char limit", it looks like a error message made by the homepage, maybe you should write "Read more about me" instead, will make it look more apealing.
  • 6/11/2009 2:27 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Answer
    Reply Quote
    So you want the child object to have the same rotation of the parent object? If you're pointing the child at the parent then why would it matter how the parent is rotated? Or are you trying to intercept the parent like this:

    C
      \
       \
        \
         \
    P--->

    If you want to add the rotation matrices together, then you can use Matrix.CreateFromYawPitchRoll for each of them, multiply them together, and if you need to use Matrix.Decompose to get a quaternion rotation out.

    About the signature: it's supposed to be a joke; I thought it would be apparent since it's a link and is explained at the signature page... I'll give it some more thought and maybe come up with something better. Thanks for pointing that out!
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 6/12/2009 7:55 AM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Nope, I can not figure this one out, I guess I found the limit of my math knowledge. I better find some explaining homepages.

    Edit: After reading through a big bunch of posts. And I got the feeling that I am on the completely wrong track here. People are talking about replacing position and rotation with a "quaternion" (new word for me) to make the math easier to get. Looks like i have to redo a big bunch of code *sniff

    Edit2: Now have I exterminated all existing angles and replaced them with quaternions, and the the posters was right - everything is much easier now!

    Should I post the correct code?
  • 8/10/2009 12:55 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Yes please post.
  • 8/10/2009 2:26 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    And rather than changing the thread subject, just mark the thread as answered. :)
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 8/10/2009 3:37 PM In reply to

    Re: Calculating a XYZ-pos for a vector with rotation in XYZ

    Jim Perry:
    And rather than changing the thread subject, just mark the thread as answered. :)

    It was like half a year ago I wrote the last post in this thread, surprised that it would be brought up again!

    This how my code looks like now:
    childImage.Scale = parent.Scale * Size_Relative; 
                childImage.QuatRotation = parent.QuatRotation * Rotation_Relative; 
                Vector3 vectorRotation = Vector3.Zero; 
                Matrix calcPos = Matrix.CreateFromQuaternion(parent.QuatRotation); 
     
                childImage.X = parent.X + calcPos.Right.X * Position_Relative.X * parent.Scale.X; 
                childImage.Y = parent.Y + calcPos.Down.Y * Position_Relative.Y * parent.Scale.Y; 
                childImage.Z = parent.Z + calcPos.Backward.Z * Position_Relative.Z * parent.Scale.Z; 
    The child will always follow the parent in scale, rotation and position.
Page 1 of 1 (15 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG