View Full Version : Vector Math Help
Iconoklast 09-10-2008, 07:55 AM Hi all,
I'm hoping someone can help answer what probably is a very simple question.
I'm trying to offset the following set of faces:
http://img124.imageshack.us/img124/3821/offsetthisgc2.th.jpg (http://img124.imageshack.us/my.php?image=offsetthisgc2.jpg)
The numbers define each vertex that is going to be moved.
The problem is that I'm having difficulty grasping the math behind moving vertex 1 in the right direction.
The direction vertex 1 moves should be at a 45 degree angle to one of the border edges it's connected to. Instead, it's moving slightly off 45 degrees (around 55 or so).
Essentially, I thought I could get the correct direction the vertex needs to move by adding the vector direction of each edge (x1 + x2 + x3, y1 + y2 + y3, z1 + z2 + z3) and normalizing.
But I guess it's not as simple as that :)
What I'm assuming I need to do is use the angle between the 3 edges, which equal 90 degrees, and somehow use this to determine the correct direction the vertex needs to move.
My math is pretty rough though, so the several resources I've found, I've not been able to apply the logic to.
If someone could point me in the right direction, that would be appreciated.
|
|
NaughtyNathan
09-10-2008, 09:01 AM
Hi Iconoklast.. actually, it should be exactly that simple. In fact it's even simpler.
MEL handles full vector maths very well, so you usually don't need to do any component manipulation for simple vector math. i.e. if you want to add two vectors together:
vector $sum = ($V1+$V2);
What you have explained you have done sounds correct. but obviously somethings not working for you so I'll go over the process from the beginning in case you've missed something.
I'm also going to presume you're using MEL, as python is great, but has no native vector support which I find horrifying for use in Maya (yeah, I know pymel has it)
first you need to get all the vertex positions to create the vectors. I find that doing everything in vectors is a lot easier to handle.
vector $P1 = `pointPosition -w $vtx1`;
vector $P2 = `pointPosition -w $vtx2`;
vector $P4 = `pointPosition -w $vtx4`;
now you have your three points you can calculate two vectors:
vector $V1_2 = ($P2-$P1);
vector $V1_4 = ($P4-$P1);
now, the average vector between these two vectors is the same as with regular numbers: (A+B)/2. However, the LENGTH of these vectors is important as you've discovered. if you only want their direction average, and not take their magnitude into consideration, you need to normalise the original vectors:
vector $V1_2 = unit($P2-$P1);
vector $V1_4 = unit($P4-$P1);
NOW the average will be exactly 45 degrees (for your example case)
vector $VAver = (($V1_2 + $V1_4)/2);
This vector doesn't strictly need to be normalised as it will still be 45 degrees, but you probably do want to normalise it because you'll be moving something along it (your vertex).
vector $VAver = unit($V1_2 + $V1_4);
because we're normalising it, we can omit the /2 (though it might be better to leave it in so it's clearer what's happening?)
Hope that's some help.
:nathaN
Iconoklast
09-10-2008, 06:26 PM
Thanks for the reply Nathan.
This is more or less how I've currently been calculating direction. It works great on a vertex connected to two related edges. but anymore than two and I get unexpected results.
Here's a screenshot of how the edges move using the above logic:
http://img300.imageshack.us/img300/2352/offset2gm8.th.jpg (http://img300.imageshack.us/my.php?image=offset2gm8.jpg)
My calculation flow goes as follow:
1) Get the edges connected to the vertex that relate to the faces from the original selection
2) For each edge, calculate the vector, normalize
3) Add the vectors together and normalise the resulting vector
4) Move along this vector.
I also attempted dividing the resulting vector by the number of edges, but this equals the same as the normalized vector.
You can see in the above screenshot that the resulting vector for the circled vertex is not moving at a 45 degree angle.
I can understand why it's not. If I were to add the vectors manually, adding edges 1 + 2 will give me the desired direction (45 degree angle), but when I add edge 3, it'll skew the direction.
I could exclude edge 3 from the calculation and all will be well, but I don't think that'll work the same on more complex geometry.
NaughtyNathan
09-11-2008, 10:43 AM
I'm not sure what the solution is, as I'm not entirely sure how you've arrived at that mesh, where edge 3 has come from and what, ultimately, you're trying to do..:surprised um.. :D
would it be valid to simply always ignore any additional edges that fall between the two outermost edges..?
I'm guessing you're writing something to solve the issues with Maya's skanky extrusion (actually I don't need to guess, you've already said as much). Well, I'm toying with something too and I'm *this close* to admitting defeat (for now!) as it's turning out to be a lot more complicated that I thought it would be... :D
Maya's extrusion problems are SO bad in all the the most basic of applications that generically post-fixing them is a real ball-ache...
I'd be interested to hear your progress on this, and if I come up with anything useful I'll keep you posted too.
:nathaN
Iconoklast
09-11-2008, 11:47 PM
That edge I added is just a scenario I'm trying to work through. I'm not sure how often that kind of topology would appear in real-world modeling, but I was hoping there would be an algorithm that would be a 'one ring to rule them all' solution.
I've come to the conclusion that there isn't, though :[
If I were to add the logic to exclude the middle (or all internal) edge, then on faces that are not planar, the resulting vertex direction would not be along the correct plane.
So what I've decided is to first determine if the related faces connected to the vertex are planar. If they are, just care about the two outer edges.
If the faces aren't planar, I'm assuming a sum of the vectors will work out fine (untested).
Couple other special case scenarios I'd need to work through. At this moment I have a script that works pretty well for flat planes, or planes that don't have wacky edge cuts like in my above example.
I agree that this is certainly a ball-ache :).
CGTalk Moderation
09-11-2008, 11:47 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.