3D Vectors

This is a discussion on 3D Vectors within the Game Programming forums, part of the General Programming Boards category; Here's what I'm doing: Moving a space ship from one point in space to another point in space at a ...

  1. #1
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65

    3D Vectors

    Here's what I'm doing:

    Moving a space ship from one point in space to another point in space at a constant speed.

    Here's my question:

    How do I calculate the per-second vector components of the x, y, and z directions? Ei. How do I know how much the spaceship moves in a direction(x,y, or z) per second? I know how to calculate the magnitude of the vector, but I am lost beyond that.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    if you know the angles you can do some trig, however you should probably use the translation vector with the magnitude in it multiplied by a (directional) rotation matrix.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    I guess I have to calculate the angles? I am given three pieces of information: The current coordinates, the desination coordinates, and the speed. For now, direction of the spaceship does not matter (assume it's a standard circular UFO).

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well then another method could be to calculate the unit vector in that direction. Then you just multiply that by the magnitude you're going to jump. That's probably a lot simpler for this purpose.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    A completely vector based system will work fine.

    <newx,newy,newz>=<x2-x1,y2-y1,z2-z1>
    Then normalize the new vector, and add it to the current position.

    If, for some reason, you want to find the angle between the vector representing the direction it is going, and the vector representing some other direction, use a dot product.
    Away.

  6. #6
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    black - by normalizing do you mean to take the cross product?

    fib - I don't quite catch what you mean. Could you explain a little furthur?

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Azmeos
    black - by normalizing do you mean to take the cross product?
    normalize means make the magnitude == 1. This can be done by dividing each component by the current magnatude.

    magnitude = sqrt( x^2 + y^2 + z^2 )

  8. #8
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Hrm, I still don't quite understand. What am I dividing it by? Please explain a bit more.

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    To normalize:

    Code:
    magnitude = sqrt(x^2+y^2+z^2);
    x/=magnitude;
    y/=magnitude;
    z/=magnitude;
    This will ensure that the total length of the vector == 1.
    Away.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Wait a sec...who are you!? Stop confuting me!
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Confuted == (Blackrat && a_name_change)

    Still the same great (lousy) help.
    Away.

  12. #12
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    I see. So, then I just multiply (or do I add??) those new values to the current coordinates? And we still have to deal with the issue of the speed. How does that come in? Just a multiplication? And what if the destination coord < current coord, negative values?

    black/confuted - By the way, I'm a fellow Michigander, from Grand Rapids.
    Last edited by Azmeos; 08-12-2003 at 11:40 PM.
    \0

  13. #13
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    add the normalized direction vector to the current position. It will work even for negative directions.

    Speed will be constant with the namralized vector, regardless of direction. If you want to change this speed, add the normalized vector multiplied by an arbitrary constant k to the current coordinates.
    Away.

  14. #14
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Ok, so

    magnitude = sqrt(x^2+y^2+z^2);

    where x, y and z are really x1-x2, y1-y2, and z1-z2 ?

  15. #15
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    yes.

    next question please.
    Away.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 12:40 PM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 10:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21