Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can someone explain how object space works mathematically?

Asked by 8 years ago

Can someone explain how object space works mathematically?

0
MrNicNac covered it in a video that you can find here.. https://www.youtube.com/watch?v=sClE5avo618. ImageLabel 1541 — 8y

2 answers

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

The short version can be taken from the answer to your previous question:

A:inverse() * B is exactly the same thing as A:toObjectSpace(B)

A * Bis exactly the matrix product

A:inverse() corresponds exactly to the matrix inverse of A

If you don't know linear algebra, this probably doesn't mean much to you.


Linear Algebra

Linear algebra is an algebra interested in linear functions. These are functions that play extremely nicely with multiplication and addition, namely:

  • function of a sum is the same as the sum of function: f(x + y) = f(x) + f(y)
  • scale of a function is the same as the function of the scale: f(k*x) = k*f(x)

This gives you simple theorems like f(0) = 0 for all linear functions1.

Vectors

Vectors are incredibly important to linear algebra. When talking about real numbers, vectors are ordered tuples. They are notated <x, y, z> or (x, y, z) or [x, y, z] or [x y z] or

[ x ] [ y ] [ z ]

depending on the notation preferred by whatever book you're reading.

Vectors can represent things like points in space very neatly. You take x as left, y as up, and z as back and forth. ROBLOX uses Vector3s (vectors with three numbers) to represent positions

What can we do with vectors?

We can add them together: (x, y, z) + (a, b, c) = (x + a, y + b, z + c).

We can scale them: k * (x, y , z) = (k*x, k*y, k*z)

In linear algebra, we also talk about using linear functions on vectors. These linear functions are matrices.

Matrices

Matrices are usually notated as a grid of numbers.

When vectors are viewed as 1-column or 1-row matrices, the matrix product corresponds exactly to linear functions.

It is too much to explain all of linear algebra in this answer.

The gist is that you should imagine a matrix as a description of what each axis becomes; you multiply the components of the vector with these to get the result. Picture crudely explaining this

You can easily extend matrix products to include a matrix times another.

Inverse

There is a multiplicative inverse for most numbers. The multiplicative inverse of x is y if x * y = 1. It's called the "multiplicative inverse" because it "undoes" multiplication: (k * x) * y = k * (x * y) = k * 1 = k. For instance, 10 * 5 * 0.2 = 10.

For many (but not most) matrices, there exists an inverse. For the form of matrix used by ROBLOX CFrames, they are always invertible.

CFrames are Matrices

In brief, CFrames are linear functions (read: matrix) that change from object space to world space. What I mean by that is that if you put "in" (0, 0, 0), you get "out" the position of the CFrame; if you put "in" (0, 1, 0) you get "out" a position 1 stud in the "up" direction of the CFrame, etc.

This operation I'm describing actually a matrix * vector, and you can totally do this in ROBLOX -- CFrame.Angles(1, 2, 3) * Vector3.new(4, 5, 6) gives you (7.3973465, -3.83828211, -2.74715352).

Since a CFrame is a matrix, you can use matrix product and matrix inverse.

Magical fourth dimension

If you're reading very carefully, you'll have noticed I contradicted myself.

I said that "f(0) is 0 for all linear functions", but I also said CFrames are linear functions, while saying that (0, 0, 0) goes to the position of the CFrame. That's not 0. How does this work?

There's a hidden "4th dimension". It is constantly 1. It cannot not-be 1. This is how translation, a non-linear operation is accomplished -- since you get "for free" a column of the matrix which will always simply be added to the x, y, and z.

Math

And once more:

  • A:toWorldSpace(B) is the same thing as A * B, the matrix product
  • B:toObjectSpace(B) is the same thing as A:inverse() * B, a matrix inverse and a matrix product.

  1. Pf. 0 = 0 * 1. Thus f(0) = f(0 * 1). f is linear, so 0 * f(1) = 0 = f(0). QED. 

Ad
Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
8 years ago

toObjectSpacelet's you know where something is in relation to something else. For instance, if you had too parts (Part1, Part2). It would allow you to figure out just where Part2 is relative to Part1, if the XYZ axii was "centered" at Part1, including rotation.

I don't know how much more detail can go into explaining something like this, so reading up on already written articles and video tutorials should hopefully expand a little bit more on what I've covered.

more on CFrame's member functions here, and here

Answer this question