Stormworks: Build and Rescue

Stormworks: Build and Rescue

评价数不足
The Math behind the XML
由 Kernle Frizzle 制作
This guide provides the mathematical context behind those funny 9 numbers in a vehicle's xml.
   
奖励
收藏
已收藏
取消收藏
What is XML editing?
There's plenty of guides about how to XML edit, chances are they've said everything that can be said already, so go check em out.
What is a vector..?
In order to understand what the 9 numbers in a component's XML really mean, you first have to understand vectors and how they're used for expressing coordinates in 3D space.

This section is primarily for people who haven't been exposed to vectors or vector notation, or who need a refresher.

What does a vector represent?

A vector is a line with both direction and magnitude. booyah.

A vector is essentially a mathematical "object" that stores the coordinates of a position and allows operations to be performed on those coordinates without needing to evaluate each coordinate individually.

When you think about a point in 3D space, that point is usually represented by x,y and z coordinates in the same way a point on a 2D graph is represented by x and y coordinates. The point (x,y,z) can also be thought of as a vector, who's tail is centered at (0,0,0) (the origin) and who's arrow ends at the point (x,y,z).

In Stormworks, the positive z axis faces directly north. The y axis faces directly up, and the x axis points to the right. In terms of the editor, z faces forward, y up and x to the right.

Notation

There are several notations for vectors, but the notation I'll be using is <x,y,z>. x represents the x coordinate of the tip of the vector, y is the y and z is the z.

you can condense <x,y,z> into a single variable, v. Vectors are normally boldfaced letters.

Every vector has what's called a magnitude, which just means the length of the arrow. You find the length with the formula sqrt(x^2+y^2+z^2), which is just the pythagorean theorem. The common notation for the magnitude of a vector v is ||v||.

Vector addition

When you add two vectors together, you add them "tip to tail." This means the x component of one vector is added to the x component of the other vector to result in the x coordinate of the resulting vector. Likewise for the y and z components.

Example: <1,1,4> + <0,5,1> = <1,6,5>
Unit Vectors
A unit vector is a vector whose magnitude is 1.

Another common notation you might see for vectors is v = ai + bj + ck

In this case, i, j and k are unit vectors in the directions of the x y and z axes, and a,b and c are factors the unit vectors are scaled by.

The normal definitions for i, j and k are

i = <1,0,0>
j = <0,1,0>
k = <0,0,1>

Lets say v = 2i + 3j - 5k

Expanding this would give you

v = 2*<1,0,0> + 3*<0,1,0> - 5*<0,0,1> = <2,0,0> + <0,3,0> + <0,0,-5> = <2,3,-5>

You can see how this notation results in the same vector as the normal <x,y,z> notation. The one difference is, instead of directly defining it in terms of the coordinate space's axes, the vector v is defined with reference to three unit vectors representing those three axes. They're like middlemen.
Do i, j and k HAVE to be unit vectors?
Short answer: no.

Long answer: no.

What are local i, j and k basis vectors..?

One way to represent the orientation of an object in space is by using a vector. A vector can point in any direction , and can therefore be used to represent any direction. A problem arises however when you want to represent the full orientation of an object with just a single vector.

Sure, a vector can point in the direction the object is facing, but there is no way to know what that object's "roll" might be just from a straight line. It is for this reason that orientations are commonly represented by three separate vectors, each corresponding to the i, j and k vectors of that object's reference frame. These three vectors make up what's called the "basis" of the space, but throughout this guide I'll be calling them the local i, j and k vectors. They only represent the x, y and z axes when you look from the point of view of the object.

It is important to remember, the coordinates of each unit vector are still in terms of the global coordinate space.

Getting back to Stormworks. Every component's mesh and hitbox is defined relative to their reference frame, in terms of local unit vectors i, j and k.

Does <1,0,0,> <0,1,0> <0,0,1> look familiar?

Because if you have used XML editing before, it should. That is exactly what the list of 9 numbers in the XML is.

r="1,0,0,0,1,0,0,0,1"

All three local unit vectors are combined into a list of numbers.

Now what happens if we change the coordinates of the unit vectors?
This image shows how changing the sizes and directions of the unit vectors skews and stretches the mesh of a component. The grey point represents one vertex of a triangle that makes up the mesh. I forgot to make the ()'s into <>'s, so just pretend they're <>'s.

Because every triangle's vertex is defined in terms of i, j and k, scaling or changing the values of i, j and k ends up scaling and changing every point on the mesh, resulting in the classic XML stretches.

At this point you might be wondering, why not scale by numbers that aren't necessarily integers?

If we were able to do that, we could technically rotate and orient a component however we want. However, the game does not allow non-integer numbers, and for good reason.

The hitbox of each component is also defined in terms of i, j and k. If i, j and k weren't nice clean numbers, the hitbox wouldn't line up 1:1 with blocks around it, and I bet you can see how that could be an issue.

With this system, the i, j and k vectors can technically be anything. They can even be <0,0,0>.
That's about it
If you've seen people say those numbers are the transform matrix of the mesh, don't worry about the fancy name, the math is exactly the same as what's going on with the vectors.

I think I covered everything you need to understand what those 9 numbers in the XML represent, but if you have any questions, don't hesitate to like and subscribe and hit that notification bell

oh and if you have questions ask away in the comments
2 条留言
programmer137 1 月 11 日 上午 12:18 
That's the good stuff right there
DinoDude777 2024 年 10 月 18 日 下午 3:54 
w i d e