Home Coding, Scripting, Shaders

Vector math - understanding the output to use in code

jt_knight
polycounter lvl 2
Offline / Send Message
jt_knight polycounter lvl 2
Hi everyone!
This is a general question that I hope I can get some clarification on.  But I'm getting the vector direction, angle, distance, and dot product of an object.  I cannot for the life of me understand what some of the output means or how I can use it to set conditions in my code for the direction.

For example:

direction:  (0.7, 0.2, -0.6) (no clue)
Dot product: -0.3  (no clue)
Angle: 107.5 (makes sense)
Distance: 1.7 (makes sense)
Normalized direction: (-0.1, -1.0, 0.0)  (no clue)

Replies

  • Axi5
    Offline / Send Message
    Axi5 interpolator
    You may need to brush up on some linear algebra it seems, I'll help you understand what you've got but it's worthwhile doing some internet digging. It's a little tricky to understand you without knowing all the inputs and context to this though so this is the best I've come up with without much thought:

    Direction
    Imagine your shoulder is your world origin: 0, 0, 0 and point your finger somewhere. Where your finger is is a value in 3D space relative to that origin, so X=0.7, Y=0.2, Z=-0.6. Directions are very similar to normal XYZ positions, but for most operations they're assumed to be unit length i.e. magnitude == 1, so most of the time these values will be <=1.0.

    Dot Product
    The dot product is most often used to find the angle between two vectors given in -1.0 to 1.0. So your direction vector was compared against another vector and the result was -0.3 meaning your vector is pointing somewhat opposite the other vector. In this case I think you're comparing to a forward or up vector but you've truncated your values so I can't be sure. The dot product is super useful because it can tell you whether vectors point the same direction. If you get a value close to 1 then they're pointing approximately the same way, -1 and they're opposites, 0 and they're perpendicular (see also: Cross Product).

    Angle
    Angle doesn't make much sense to me here, angle around which axis and in relation to what? So you're 107.5 degrees around something. This type of value is most useful for human readable output but it needs context to be understood and that's why it may be obvious to you but not to me.

    Distance aka Magnitude
    Your value here doesn't make sense here either since the magnitude (distance) of your vector is 0.943, though I assume you cut some precision off here so the magnitude is actually closer to 1.0. I also assume what you're actually doing is comparing the distance between two objects here, or distance to world origin maybe.

    Normalized Direction
    just fits your vector to unit length, so above where it's 0.943, it just extends the vector to 1.0, or trims it if it's greater than 1.
  • jt_knight
    Offline / Send Message
    jt_knight polycounter lvl 2
    Thank you so much for the explanation!  I sincerely appreciate it.  I've been doing Udemy 3d math for gaming to help understand this. It's helped a little.  But I'm thinking I need to take a linear algebra course, which I never took in school.

    For context, I'm using the data to rotate a rocket and depending on it's tilt, I would enable particles on either side.  Previously I did it using basic eulerAngles and it worked great if I was only moving from the origin.  So if I rotated on the -X I would enable particles on a certain side.  If I rotated on the +Y I would enable particles on another side, and so on.  But once you start putting the rocket in arbitrary angles it would break.

    It was recommended to me that I try using vectors for this type of thing so that the rocket can be at whatever position in 3D space and still get the same results.  (which I couldn't do before).

    Which brings me to the original question of how I could use that info to get the same affect.  Or if I'm missing some data.  If needed I can post code.  
  • Axi5
    Offline / Send Message
    Axi5 interpolator
    Yeah vectors are perfect for this. So I'm guessing you have a rocket which has something like 4 RCS modules spread out around the middle of the craft? Then whichever is closest to the ground is activated?

    You could do that with dot product. So you need to get the direction the models are facing somehow, in Unity it'd be transform.forward or something similar in Unreal, note that you want this in world space not local. Then get the down vector, you can do this 1 of 2 ways but I'll detail the easiest one here, for now you can assume the ground is flat and wouldn't ever change in which case it'd just be 0, -1, 0 (Unity), or 0, 0, -1 (Unreal) and then just do a dot product.

    You'll get a value between -1 and 1, if the value crosses some threshold i.e. 0.5 then it's probably the closest to the floor and you can activate it.

    Hope that helps.
  • jt_knight
    Offline / Send Message
    jt_knight polycounter lvl 2
    That's almost exactly the scenario and yes it's in Unity.  I'm really enjoying working in it.  But the difference being that the thrusters are on either side of the rocket and are used to stabilize versus landing.  So if I rotated on one axis, the particles on that side would be enabled to rotate it straight again. Same would go for the other sides.
    I was thinking that I needed to get the direction the rocket is point to know what particles to enable. Not sure if that's the right thinking?  
  • Axi5
    Offline / Send Message
    Axi5 interpolator
    Yeah that works too, it's the same logic as above but applied differently. Have a play around with it and you'll start getting it.
  • jt_knight
    Offline / Send Message
    jt_knight polycounter lvl 2
    Thanks again!  Best explanation!  
  • poopipe
    Online / Send Message
    poopipe grand marshal polycounter
    Subscribe to 3Blue1Brown on YouTube.  No need to spend moneys on a course.


  • jt_knight
    Offline / Send Message
    jt_knight polycounter lvl 2
    Thank you for that!  Never heard of that till now.  Just checked it out, SAVED and now party of my study material!
Sign In or Register to comment.