Calculate new Vector based on distance and direction?

My knowledge on vectors is minimal. Help on the math and whether my thinking is correct is very appreciated.

Let’s assume we have the following:

  • start(1,1,1) vector
  • direction(?,?,?) vector
  • end(?,?,?) vector
  • distance between start() - end()

Some details:

  • The direction() vector is the mouse and constantly moving so I have to calculate it first.
  • The end() vector is always between start() and direction() on the same line.

So to calculate end(), I need:

  • the direction between start() and direction()
  • then add the distance to the start() vector based on the direction()

Possible solution I have in my mind
To get the direction, I need to

direction().subtract(start())

And then normalize() the result?

But how do I add the distance based on the new unit vector?

Sorry if I got the terminology wrong. Is this book Mathematics for 3D Game Programming and Computer Graphics: Amazon.co.uk: Eric Lengyel: 8601419489978: Books the right one to better understand the math?

@JohnK your help is very appreciated!

so wait you want to get the end point from the start point and the distance vector?

that’s just A+C = B

to get direction its

B - A = C

to get magnitude from that its C.length()

A = start, B = End, C = Distance Vector

1 Like

Hi @Pryme8 I have just seen that you posted the answer while I was typing this dissertation.

A little of the basics first

A vector is an object with direction and and length. In this image all examples of a are the same vector, this is also true of b and c.

Given a fixed origin vectors can be used to fix a point in space, any vectors starting from O are called position vectors. In Figs 1 and 2 O is a fixed origin. In Fig 1, a, b and c are position vectors, in Fig 2 only a and b are position vectors.

If you look at the two triangles you can get from the starting point of c to the end point of c by going backwards along a and then along b. We can write

c = b - a and it follows

b = c + a as you can see in this picture

image

In Babylon.js when A, B and C are Vector2s, Vector3s or Vector4s with

C = B.subtract(A) then B = C.add(A)

When start and end are two position vectors and you travel from start to end then

end = start + direction just follow the arrows

image

In Babylon.js

end = start.add(direction)

You can find the length of the direction vector with direction.length().

A unit vector u is one that has length 1, and by multiplying by a scalar p you get a vector in the same direction of length p

When you know a direction vector in Babylon.js you get a unit vector by

direction.normalize() this changes the direction vector to a unit vector and so direction.length() with now be 1.

For any end point change the value of P in

end = start.add(direction.scale(P));

Do not know the book you included in the post. You can learn a lot about vectors from the web, including Wikipedia.

5 Likes

I finished the feature I wanted, thanks.

This was very helpful

end = start.add(direction.scale§);

My main confusion about vectors now is… should I be thinking about vectors as lines or as points in space? Or both?

image

Because if I think of vectors in points, the above image doesnt make sense.

So I am guessing its lines? With a direction and length (as you said).

But each line has its own start, ending? Because if I think of vectors in points, then vector a & c do not have the same starting/end points in the diagram above. But I think I am overcomplicating how this works…

In English, the word “vector” means both the point in space and the direction (oriented bipoint). This can be confusing, I agree.
You can then imagine that a point in the space is also the direction from the system origin to this point : point.substract(origin)

1 Like

A vector is a directed line segment.

A positional vector is a directed line segment that starts at a specific point. All vectors that start from that same point define all points in that space. Not all vectors in that space start from the specified point.

So vectors are lines not points but can be used to define points.

2 Likes

So my assertion about how I’ve understood “vector” so far in some documentation or in this forum isn’t that correct : it’s not a point… but it can used to define a point.
So please follow the JohnK’s definition instead of mine.

Note : in french, we use the term “point” to define a point in the space and nothing else, and we use the word “vector” to define an oriented bi-point. AB (with an arrow above the letters) = from A to B

2 Likes

IIt represents a distance and magnitude. You decide how to track its point position in space technically.

It could all be @ zero or they could be offset or they could even exist in a higher dimension… (nth power vectors)

But for all intensive purposes they are comprised of at least two points.

Its the same in English in strict maths. The way points and vectors are represented can be confusing as the can both be represented in exactly the same way. Given a set of axes then a triple of numbers (x, y, z) can be coordinates and refer to a point [position no magnitude] or a vector [direction and magnitude]. It is all in the context.

(An aside, this happens in English as well, for example the word fast can refer to completely different things, the car moves fast, the rope held him fast, the monks are on a fast).

It is important in maths at least to distinguish between them. For example adding two points together makes no sense, adding a point to a vector also makes no sense (they are different objects) but adding two vectors does.

However in maths there is a one to one correspondence between points in a space and position vectors in the same space. We then can say let A be the point with positional vector P = (p, q, r) let V be the vector (a, b, c) then (p + a, q + b, r + c) = P + V will be the positional vector that gives you the point reached when setting out from A along the vector V.

The main point is if you are dealing with using direction in any sense then you are using vectors ( some of which will be positional vectors) not points

2 Likes