Sign in to follow this  
Dwood

Modular Level Building

Recommended Posts

First off, I am using C#, and am using the June 2007 sdk for DX9 math and matrices to get some quick results.

Skip to the bottom if you only want to read the problem I need help with. As a preface: I have asked several people about this, and they basically just sent me to wikipedia's linear algebra page. Well that doesn't work for me. I can't just read top-level gibberish and immediately understand it. I can, however understand a quick tutorial on calculating a face's normals where they actually express it in a readable programming language such as C/C++.

Now, here's onto the description of my program- I want to write an application that can take pre-defined models and piece them together, kind of like an erector set, Hotwheels tracks, and model railroads, stuff of the like. In non-layman's terms, I want to write a modular world builder from pre-defined pieces, in which while the pieces themselves are pre-defined, the layouts are not. This can have several effects in the end, one such being procedurally-generated levels.

I'm going to wind up repeating a few things over and over here, just to iterate and be sure that my question can be understood.

This is a post I made on another forum on the subject, with a few edits:[s]
[/s][spoiler]
[quote]
I'm working on a project right now, but i've hit a complete stand-still. This problem has to deal with taking 2 3-dimensional models and putting them together, based on things I call "golden areas". I have defined this golden area to be a quadrilateral (ie rectangle) in which all the vertices are parallel on either the X or Y axis (not going to deal with Z axis. Forget that.)

In C#, my vertices are defined basically as any other (textured) vertice, with an XYZ for normals and an XYZ for position. I do believe that I won't have to do jack to the normals, however I do have to translate the vertices, as well as the normals (but that can be changed whenever I rotate them right?)

We can assume a number of things (or I'll go insane, mark my words) 1. All golden areas that are of certain sides will be marked with a tag. +garea## where ## is a number defining the gareas that can attach to them (all garea00's will only attach to other garea00's).

(the below is the reason I'm posting for help!)
Now, here's the real problem(s) I have to determine how much I have to rotate the whole model. This rotation is called yaw.

Thus I gotta somehow use a method so I can find the orientation of the model, such that whenever it is rotated, I won't have, say, a model intersecting a model (ie a box inside a box).

here's a very, very basic example of what I want to do:

Note that I refer to a golden area as a flat plane with 4 verts, and 2 tris where we can attach other golden areas together to. In our case these are somewhat purple.

Edit: the problem is that there's no real way to find out what side of a triangle is on which side.

I'll probably figure this out on my own eventually but if you have any ideas or links please let me know! (wikipedia on linear algebra makes me want to kill small adorable animals)

Editx2: I found the answer to the figuring out of what side a triangle is, via vectors and normals... in my little program, it too, fits the counter-clockwise rule that all normals have to fit under. [s]
[/s][/quote][/spoiler]
[s]
[/s]

A "Golden Area" is an area in which other pieces can attach to - I also call them limbs/branches interchangeably.
[indent]A golden area has 2 faces, each face comprising of 3 vertices.
The golden area for a limb always faces the inside of the limb (closed-world)
[/indent]
-The Problem-
I need help with is taking a flat plane that is in one area with whatever rotation (requiring that this plane is parallel to the Z axis or I'll go nuts) and getting how much I need to rotate that plane via yaw (or around the Z axis, Z being up and down by my preferences) in order such that the two golden areas, when they and their corresponding models are lined up, are facing opposite one another.

[s]how i shot web and make one object face another when object is plane that is parallel to at least 1 axis (Z) C# DX 9 Matrix maths[/s]


Edit: After looking around and realizing that my problem wasn't as cut-and-dried as it sounds like it should be, I turned off the raging. Of course, I will be slowly working on my program on my own and perhaps I will figure out what I need, but I'm leaving it de-crappified and not as likely to get the thread locked.Editx5 Or me banned >.>

Edit 9001: IF this is the wrong forum for this kind of thing, please let me know.

Share this post


Link to post
Share on other sites
Assuming that you maintain a position and direction for each piece and boiling it all down, are you asking how to rotate a known direction vector to align (or anti-align) it with another known direction vector? If so, the angle of a direction vector ( about the Z axis ) is atan2( v.y, v.x ).

I'm envisioning that you have one or more attachment points for each piece, each attachment point having a position and direction. To attach 2 pieces at chosen attachment points ( e.g., point 2 for the first piece, point 3 for the second ), calculate the angle between the two attachment directions, rotate one of the pieces about its attachment point* by the appropriate angle, and translate the piece by the difference in attachment points.

*Or rotate the piece about its origin and calculate the new attachment point.

Share this post


Link to post
Share on other sites
Let me get this straight.

You have two models, M1 and M2.

On M1, there is a known subset of vertices, {u[sub]1[/sub], u[sub]2[/sub], ..., u[sub]n[/sub]}, which I will call M1's "connector vertices."
On M2, there is a known subset of vertices, {v[sub]1[/sub], v[sub]2[/sub], ..., v[sub]n[/sub]}, which I will call M2's "connector vertices."

You want to find a rigid body motion (a translation and rotation) that brings the connector vertices of M2 to those of M1. I.e., you want a rotation matrix R and a translation vector d such that

R v[sub]1[/sub] + d = u[sub]1[/sub]
R v[sub]2[/sub] + d = u[sub]2[/sub]
...
R v[sub]n[/sub] + d = u[sub]n[/sub] .

Is that your problem?

Your unknowns are R and d. You have a bunch of equations. Solve. Here's how:

Define V = [v[sub]2[/sub]-v[sub]1[/sub], ..., v[sub]n[/sub]-v[sub]1[/sub]] and U = [u[sub]2[/sub]-u[sub]1[/sub], ..., u[sub]n-[/sub]u[sub]1[/sub]]. Then what you want for 'R' is,

R V = U

which has the least-squares solution,

R = U V[sup]T[/sup] (V V[sup]T[/sup])[sup]-1[/sup] .

If this matrix does not end up satisfying
det R = 1
and
R[sup]T[/sup] R = I
then it is not a rotation matrix; this tells you that your vertex subsets don't match and the pieces won't join up.

Finally,

d = u[sub]1[/sub] - R v[sub]1[/sub]

or, maybe a little more robustly (you should get the same answer),

d = (1/n)[(u[sub]1[/sub]+...+u[sub]n[/sub]) - R (v[sub]1[/sub]+...+v[sub]n[/sub])] .

Now that you have R and d, your problem is solved.

Also note that you need at least 4 connector vertices for this to work; fewer will not uniquely specify the orientation. For the special case of n=4, you can simplify the rotation matrix computation to,

R = U V[sup]-1[/sup][sup][/sup] .

Share this post


Link to post
Share on other sites
No code from me for this problem, sorry. But a solution (if I made no mistakes) looks like this:

Assuming that you have a part already fixed in the world. Its local-to-global transformation matrix (a.k.a. model matrix) is named [b]M[/b][sub]1[/sub]. W.r.t. this local co-ordinate system, the 4 vertices of the said quad are given. Unfortunately, the quad by itself is not sufficient. Assuming it is not a square, there are still 4 possible solutions to map another, equally sized quad from the 2nd part onto the 1st one. Even using the normals of the quads isn't sufficient, because there are still 2 possible solutions.

For now, let us assume that you doesn't have quads but local co-ordinate axes. Such axes have a position and an orientation in space. They are described by a standard transformation matrix build from translation and rotation, i.e. (using column vectors)
[b]A[/b] := [b]T[/b] * [b]R[/b]
Let us define such an axis in part 1 given w.r.t. the model transformation of said part 1, so that the global counterpart of that axis is
[b]A[/b][sub]1g[/sub] := [b]M[/b][sub]1[/sub] * [b]A[/b][sub]1l[/sub]
and similarly for the 2nd [size="2"]part[/size]
[b]A[/b][sub]2g[/sub] := [b]M[/b][sub]2[/sub] * [b]A[/b][sub]2l[/sub]
[sub][size="3"][size="2"]
Lets assume further that local z direction of such an axis is ever defined to point to the inside of the respective part. Then what you want to do is to find a transformation matrix [/size][b][size="2"]X[/size][/b][size="2"] so that the both axes lie one upon the other in global space, but ... there is one caveat. Although both axes' positions should be identical in global space, their orientations should not, because it is defined that the local z direction ever point into the part. Hence the global z direction of the one axis must point opposite to the global z direction of the other axis. This also enforces that another direction must be opposite, or else we have violated the uniform handedness of the both axes. So lets us say that the y directions should fall coincident, but the x and z should be mirrored. This is expressed by a non-uniform scaling matrix build as
[/size][b][size="2"]S[/size][/b][size="2"]( -1, 1, -1 )

In summary, we have a formula like
[/size][b][size="2"]X[/size][/b][size="2"] * [/size][b][size="2"]M[/size][/b][sub][size="1"]2[/size][/sub][size="2"] * [/size][b][size="2"]T[/size][/b][sub][size="1"]2l[/size][/sub][size="2"] * [/size][b][size="2"]R[/size][/b][sub][size="1"]2l[/size][/sub][size="2"][size=2][size="2"][size=2] * [/size][/size][b][size="2"][size=2]S[/size][/size][/b][/size] = [/size][b][size="2"]A[/size][/b][sub][size="1"]1g[/size][/sub][size="2"]
to solve for [/size][b][size="2"]X[/size][/b][size="2"], what means
[/size][b][size="2"]X[/size][/b][size="2"] = [/size][b][size="2"]A[/size][/b][sub][size="1"]1g[/size][/sub][size="2"] * ( [/size][b][size="2"]M[/size][/b][sub][size="1"]2[/size][/sub][size="2"] * [/size][b][size="2"]T[/size][/b][sub][size="1"]2l[/size][/sub][size="2"] * [/size][b][size="2"]R[/size][/b][sub][size="1"]2l[/size][/sub][size="2"][size=2][size="2"][size=2] * [/size][/size][b][size="2"][size=2]S[/size][/size][/b][/size] )[/size][sup][size="1"]-1[/size][/sup] [/size][/sub]
[sub] [/sub]
[sub][size="3"][size="2"]Now remember that you have a quad instead of the axis. One vertex of that quad may be used for the axis' position, but you have to guarantee that the both vertices (i.e. one of both quads) matches. Then you can use the quad normals as local z direction. However, there is still one direction missed. You can use the difference vectors to the 2 neighbored vertices as up and right vectors (after normalization). However, that again means that you make the order of vertices totally clear. In other words, if you can't work with axes directly, go away from quads and use tris instead, where 1 vertex is marked as origin and the other both vertices are given in a defined order (e.g. CCW).[/size][/size][/sub]
[sub] [/sub]
[sub] [/sub]
[sub][size="3"][size="2"]EDIT: Ups, in the meanwhile Emergent has given a complete answer, too. However, it uses another method.[/size][/size][/sub]

Share this post


Link to post
Share on other sites
Thanks for the help guys!

@ Emergent - I'm not used to the notations of upper-level maths, so when you defined U and V, you're talking about a 4x4 Matrix right?
Also, what does the T in V[sup]T [/sup], R[sup]T [/sup]Signify?

@ haegarr I haven't read through your response all the way yet but it sounds pretty thorough, but I do have one question:

[quote] don't have quads but local co-ordinate axes[/quote]

Are you talking about using the center point of the quad as an origin for the coordinate axes?

Share this post


Link to post
Share on other sites
[quote name='Dwood' timestamp='1298410322' post='4777710']
@ Emergent - I'm not used to the notations of upper-level maths, so when you defined U and V, you're talking about a 4x4 Matrix right?[/quote]

In the line above, I wrote

[quote]
V = [v[sub]2[/sub]-v[sub]1[/sub], ..., v[sub]n[/sub]-v[sub]1[/sub]] and U = [u[sub]2[/sub]-u[sub]1[/sub], ..., u[sub]n-[/sub]u[sub]1[/sub]].[/quote]

What this means is that V and U are both 3x(n-1) matrices (where, recall, 'n' was the number of "connector vertices"). Each column is a vector difference. I.e., the first column of V is the vector from the vertex v[sub]1[/sub] to the vertex v[sub]2[/sub]; this is v[sub]2[/sub]-v[sub]1[/sub]. The second column of V is v[sub]3[/sub]-v[sub]1[/sub]; the third is v[sub]4[/sub]-v[sub]1[/sub]; etc.

You should also know that 'R' is a 3x3 rotation matrix, and 'd' is a 3x1 vector. If you're more familiar with 4x4 homogeneous matrices, R is the upper left 3x3 subblock, and d is the upper right subblock; i.e., you can put them together into the 4x4 matrix
[code]
[ R d ]
[ 0 1 ]
[/code]

As for the matrix equation R V = U... Remember we started with the vector equations,

R v[sub]1[/sub] + d = u[sub]1[/sub]
R v[sub]2[/sub] + d = u[sub]2[/sub]
...
R v[sub]n[/sub] + d = u[sub]n[/sub] .

If you subtract the first equation from the rest, you get


R v[sub]2[/sub] - R v[sub]1[/sub] = u[sub]2[/sub] - u[sub]1[/sub]
...
R v[sub]n[/sub] - R v[sub]1[/sub] = u[sub]n[/sub] - u[sub]1[/sub]

or just

R (v[sub]2[/sub] - v[sub]1[/sub]) = u[sub]2[/sub] - u[sub]1[/sub]
...
R (v[sub]n[/sub] - v[sub]1[/sub]) = u[sub]n[/sub] - u[sub]1[/sub] .

The matrix equation

R V = U

is just a compact notation for this system of vector equations.


[quote name='Dwood' timestamp='1298410322' post='4777710']Also, what does the T in V[sup]T [/sup], R[sup]T [/sup]Signify?[/quote]

The superscript 'T' means "transpose." You rearrange the elements of a matrix so its columns become rows and vice versa. An mxn matrix becomes an nxm one.

Share this post


Link to post
Share on other sites
I _think_ I understand it now. If I don't please correct me. Matrix math is rough to get right... Here's some pseudo code I wrote up, in lieu of your method:

[code]
n = count of vertices

Matrix V[3][n-1]
Matrix U[3][n-1]
Matrix R[3][3]
Matrix D[1][3]
for i = 1; i < 4; i++
v[3][i] = quadV.vert[i] - quadV.vert[0];
U[3][i] = quadU.vert[i] - quadU.vert[0];
end for

R = U * VTransposed * (V * VTrans) ^(-1)

if (RTransposed * R == 1) && (Determinant of R == 1)
{
d = (1/n) * ((sum of quadU vertices) - R * (Sum of all quadV vertices))

}
else
Failure
[/code]

Share this post


Link to post
Share on other sites
[quote name='Dwood' timestamp='1298431022' post='4777823']
I _think_ I understand it now. If I don't please correct me.[/quote]

Yeah, that looks about right. I've made a few edits (below), but I think these fix minor oversights more than conceptual issues.

[quote name='Dwood' timestamp='1298431022' post='4777823']
Matrix math is rough to get right...[/quote]

Good matrix libraries produce more readable code and can help a lot here, I think. Have you tried the matrix library [url="http://eigen.tuxfamily.org/index.php?title=Main_Page"]Eigen[/url]? It's my favorite by far.

Anyway, here are the edits:

[code]
n = count of vertices

Matrix V[3][n-1]
Matrix U[3][n-1]
Matrix R[3][3]
Matrix D[3][1] // was D[1][3]
for i = 1; i < 4; i++
for j = 0; j < 3; ++j // This inner loop does the vector subtraction (there are 3 scalar elements in each vector). Ideally, this loop would live inside an overloaded subtraction operator for whatever vector class you use.
v[j][i-1] = quadV.vert[i][j] - quadV.vert[0][j]; // Fixed column indexing. And I'm assuming quadV.vert[i] is a 3x1 vector, whose jth scalar element is quadV.vert[i][j]
U[j][i-1] = quadU.vert[i][j] - quadU.vert[0][j];
end for
end for

//--- No edits below here

R = U * VTransposed * (V * VTrans) ^(-1)

if (RTransposed * R == 1) && (Determinant of R == 1)
{
d = (1/n) * ((sum of quadU vertices) - R * (Sum of all quadV vertices))

}
else
Failure
[/code]

Share this post


Link to post
Share on other sites
I just realized something about this method. It works in rotating the pieces together but it doesn't do anything about the requirement that each quad must face opposite one another. I guess I can just do a normals test to be sure that they are opposite one another after that....

Share this post


Link to post
Share on other sites
[quote name='Dwood' timestamp='1298410322' post='4777710']
@ haegarr I haven't read through your response all the way yet but it sounds pretty thorough, but I do have one question:

[quote] don't have quads but local co-ordinate axes[/quote]

Are you talking about using the center point of the quad as an origin for the coordinate axes?
[/quote]

The axes (I mean axes like the red arrow for x direction, green arrow for y direction, and blue arrow for z direction together indicating a co-ordinate system as seen in many DDC packages) are complete handles for itself ... err, besides perhaps the attached key value(s) to determine which axes are allowed to match. They have a definite location and orientation. It would be best (IMHO) if stitching could be done by using them alone. However, there is a way to calculate an axis from at least 3 vertices if the requirements are met that are described in my previous post. The quad's center may be used as origin as well as any of its corner, or any other point computed by interpolation if you wish. However, not the origin but the directions are the more problematic thing. To have matching directions, you need a defined order of vertices (similar to the 1:1 mapping need in Emergent's approach). Because of this, I said that dealing with axes will be "smoother" here.

Share this post


Link to post
Share on other sites
[quote name='Dwood' timestamp='1298485691' post='4778056']
I just realized something about this method. It works in rotating the pieces together but it doesn't do anything about the requirement that each quad must face opposite one another. I guess I can just do a normals test to be sure that they are opposite one another after that....
[/quote]

Like haegarr noted, my answer hinges entirely on the 1-1 mapping between vertices.

If each quad has vertices,
[code]
A---B
| |
D---C
[/code]
then the mapping (expressed in the ordering of the vertices in my {u1,...un} and {v1,...vn} lists) is,

quad1.A --> quad2.B
quad1.B --> quad2.A
quad1.C --> quad2.D
quad1.D --> quad2.C

Different orderings of the second set of vertices correspond to different ways in which the two pieces could conceivably attach.

Share this post


Link to post
Share on other sites
I guess I can guarantee, at minimum that each golden area will be the same shape. I can also guarantee the order of the vertices- you see, in order to calculate the normals, the verts need to be ordered in counter-clockwise fashion. In my exporter, it exports 3 vertices (even if they have the same data) for every Triangle and includes a [-1, 1] XY T mapping data, so that we can check the order that specific vertice needs to be in, in that triangle... For the ordering of the triangles, from the first to the 3rd vert, if I graph the verts 1 by one on that XY plane then as each point is placed, they get placed in cc order.

This is an incomplete thought right now, but when exporting I can't guarantee that the exporter will place the top Triangle after the first one, but I can guarantee that fhe verts will have some kind of cc order. I'm going to have to do some more testing on that//look over the exporter's source code.

Share this post


Link to post
Share on other sites
Also... I thought in your original post, you described connector regions consisting of two triangles, not both in the same plane. From that, I assumed that the vertex differences would be linearly independent. Now it's starting to sound like your triangles are coplanar, in which case this isn't true. Do you only have the four coplanar vertices that make up the quad? Then you'll also need to throw the normals into the U and V matrices (as additional columns) -- at which point we're approaching something very similar to what haeggar suggested.

Share this post


Link to post
Share on other sites
[quote name='Emergent' timestamp='1298500109' post='4778161']
Also... I thought in your original post, you described connector regions consisting of two triangles, not both in the same plane. From that, I assumed that the vertex differences would be linearly independent.

Now it's starting to sound like your triangles are coplanar, in which case this isn't true.

Do you only have the four coplanar vertices that make up the quad? Then you'll also need to throw the normals into the U and V matrices (as additional columns) -- at which point we're approaching something very similar to what haeggar suggested.
[/quote]

Each connector region (garea, as I call them [g for golden]) consist of 2 triangles that are both parallel to least one axis (i've noted above that I want them to be parallel to the Z axis, but I guess this limitation is arbitrary but meh just assume parallel to Z).

So yes, the triangles and the verts are all coplanar since they like along the same plane. However, Each connector region as you call it, may not be "coplanar"

Share this post


Link to post
Share on other sites
[quote name='Dwood' timestamp='1298501758' post='4778176']
So yes, the triangles and the verts are all coplanar since they like along the same plane. However, Each connector region as you call it, may not be "coplanar"
[/quote]

I'm sorry; I don't understand. At any rate, you'll know if you have a problem if, when you try to do the matrix inversion, you find out that the matrix is singular.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this