When I substract a Vector3 (position) from a Vector3 (position): What's the result?

When I substract a part position from another part position without .magnitude:

  1. Where are those coordinates? [In between?, at the edge?]
  2. If it were to make a part spawn at the difference between the 2 positions[p2-p1], where would it spawn?

Here is a test I did: I spawned a new part at the difference between both the 2 part’s positions [p2 -p1].
The new part spawned somewhere else randomly, why?

local p1 = game.Workspace:FindFirstChild("Part1")
local p2 = game.Workspace:FindFirstChild("Part2")

local distance = (p2.Position - p1.Position)
print(distance)
wait(4)
------------------------------------
local newpart = Instance.new("Part")
newpart.Position = distance
newpart.Size = p1.Size
newpart.Parent = workspace

-- Result: 40.6, -0.0008, 0.0379

You should get just that, one vector that is subtracted from another.

local v1 = Vector3.new(2, 5, 10)
local v2 = Vector3.new(1, 2, 4)
local subtracted = v1 - v2

That should be equal to Vector3(1, 3, 6) because it subtracts from each corresponding axis:

X: 2 - 1 = 1
Y: 5 - 2 = 3
Z: 10 - 4 = 6

Where would that vector(1,3,6) be in comparison to the other 2 Vectors?
[Ex. Would it be somewhere in between?, on the sides?, etc]

Im trying to understand a visual representation of it.

This puts it in the middle of 2 parts if you are wondering.

local Part = Instance.new("Part", workspace)
Part.Position = workspace.Invest.Position:Lerp(workspace.Invest1.Position, 0.5)

Oh, I know about lerp, Im just trying under to understand vectors a little bit more, especially on the visual side. Like: where would a Vector be[In The Workpace] if you substracted 2 Vectors with one another, or divided, etc.

Right now, Im just focused on the substraction part.

Here I have 2 Vectors
image

Vector V which is at (1, 3)
And Vector Q which is at (3, -1)

To add V + Q I can start drawing the tail of Q at the head of V
image
That gets us a new vector of (4, 2).

Now for your question. Subtraction. Let’s do V-Q

Instead of drawing the tail of Q at V I’m going to draw the head of Q at V and use the position where the tail is as the answer.

image

That gave us a new Vector M which is equal to (-2, 4).

At first glance this might look like it isn’t all that useful, but that’s because we are missing one crucial piece to the puzzle. Let me draw the original Q back in as well.

image

Now with that there you might notice why M is useful. M is now the direction and distance you would have to travel from Q to get to V
image

To apply this into actual code…

Say you had a part at a position we will call target
And there was a character at a position we will call hrp

We can use vector subtraction to find how far in what direction that character will have to travel to get to target.

local target = game.Workspace.Target.Position
local hrp = game.Workspace.Character.HumanoidRootPart.Position

local direction = target - hrp
print("Character has to travel " .. direction)
6 Likes

Thank you, this expodentially increased my knowledge on Vectors.

1 Like

In addition to @tlr22’s great post, I highly suggest watching the first three episodes of this series:

It will further help you understand vectors and CFrames (which are called “transformation matrices” in that series).

2 Likes