Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > SDK Help
  New Posts New Posts
  FAQ FAQ  Forum Search   Register Register  Login Login

Polygon Position

 Post Reply Post Reply Page  <123>
Author
Message
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 03 at 6:21pm
Do I need the world coordinates of the points I am using for this to work properly?

~Shawn
Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 09 at 5:20pm
I feel like I have done the rotation matrix correctly here.  But my object is not rotating to the normal of the polygon.  Does anyone see anything that I am doing wrong creating the rotation matrix? 

I would GREATLY, appreciate any help anyone could offer.  This one is irritating me because everything I have read about rotation matrices says that I am doing it right.  I am using a vector that represents the center of the polygon (polygonLocation)   I am using another point in the polygon to determine the plane upon which the polygon rests.  I am then using the cross product of those vectors to determine the up vector that is perpendicular to the plane.  So, if my understanding of rotation matrices is correct, this should create a rotation matrix that can then be adapted by an object and that object should rotate to the normal of the polygon. 

However, that is not happening with what I have pasted above.   I am at a loss.  Does anyone see anything that I am doing wrong?

Thanks so much for anyone who has the time to help me out.

~Shawn
Back to Top
Matthias Bober View Drop Down
Forum Moderator
Forum Moderator


Joined: 2006 Oct 16
Location: Germany
Online Status: Offline
Posts: 1647
Post Options Post Options   Quote Matthias Bober Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 1:25am
Originally posted by emberintherain

I am using a vector that represents the center of the polygon (polygonLocation)   I am using another point in the polygon to determine the plane upon which the polygon rests.  I am then using the cross product of those vectors to determine the up vector that is perpendicular to the plane.


This part is wrong.

Assume that A is the midpoint of your polygon and B is one of the polygon points.
N is the polygon normal.

You have to build the cross product of (B-A) and N for one of the matrix axes, let's say for V1.
Then build the cross product of this new axis and the normal for the second matrix axis, V2.
The normal is the third axis of the matrix, V3.
The polygon midpoint is the matrix offset, V0 or off.

cheers,
Matthias

MAXON
developer support
Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 2:41am
Thanks Matthias,     

I must not fully understand how to get the normal then.   I thought that the normal was A - B.   How to I get the value for N then?  

THanks,

~Shawn
Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 2:47am
Would I use 

CalcFaceNormal()


~Shawn
Back to Top
Matthias Bober View Drop Down
Forum Moderator
Forum Moderator


Joined: 2006 Oct 16
Location: Germany
Online Status: Offline
Posts: 1647
Post Options Post Options   Quote Matthias Bober Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 2:49am
For triangles the normal is the cross product of two edges of the triangle. For quadrangles it's not exactly defined. The easiest way which gives a good result is too use the cross product of the diagonales.

PS. please read up on 3D geometry basics, it will help you in the long run. Most of it can be found through Google and Wikipedia.

cheers,
Matthias

MAXON
developer support
Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 3:06am
So to calculate the normal of a quad would I do something like this...  Assuming that the points in a quad are A, B, C, and D

QUAD

N = (A-C)%(D-B)

TRIANGLE

N = (A-C)%(B-C)

SOmething like that?

Does CalcFaceNormal() do this for you?  or is that something completely different?

Thanks  a lot for your help,

~ Shawn


Back to Top
Matthias Bober View Drop Down
Forum Moderator
Forum Moderator


Joined: 2006 Oct 16
Location: Germany
Online Status: Offline
Posts: 1647
Post Options Post Options   Quote Matthias Bober Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 3:23am
yes and yes :)

source code of CalcFaceNormal()

inline Vector CalcFaceNormal(const Vector *padr, const CPolygon &v)
{
    if (v.c==v.d)
        return !((padr[v.b]-padr[v.a])%(padr[v.c]-padr[v.a]));
    else
        return !((padr[v.b]-padr[v.d])%(padr[v.c]-padr[v.a]));
}


cheers,
Matthias

MAXON
developer support
Back to Top
ello View Drop Down
Member
Member


Joined: 2005 Feb 04
Location: Germany
Online Status: Offline
Posts: 382
Post Options Post Options   Quote ello Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 12:00pm
does CalcFaceNormal work for you?? when i use it i get only nonsense rotations.

cheers,
ello
Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 12:32pm
What are you trying to do?  

~Shawn
Back to Top
ello View Drop Down
Member
Member


Joined: 2005 Feb 04
Location: Germany
Online Status: Offline
Posts: 382
Post Options Post Options   Quote ello Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 12:41pm
i am just trying to rotate some clones according to a surface normal.  and using CalcFaceNormal results in this:
http://tempfiles.earthcontrol.de/nm01.jpg


Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 12:56pm
hmmmm...   what are you using for the  "padr"  and for "v"...  ?

in

CalcFaceNormal(padr, v)
Back to Top
ello View Drop Down
Member
Member


Joined: 2005 Feb 04
Location: Germany
Online Status: Offline
Posts: 382
Post Options Post Options   Quote ello Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 1:05pm
i am using this:

        padr = ToPoint(baseMesh)->GetPointR();
        v = ToPoly(baseMesh)->GetPolygonR();

Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 1:12pm
are you creating a rotation matrix based on a specific polygon.  ?  When I get home I'll show you how I am doing it.  I'm driving home right now.



Edited by emberintherain - 2010 Mar 15 at 1:14pm
Back to Top
ello View Drop Down
Member
Member


Joined: 2005 Feb 04
Location: Germany
Online Status: Offline
Posts: 382
Post Options Post Options   Quote ello Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 1:17pm
no, i am just using clone->SetRot(normal);
is this wrong??
Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 1:41pm
I'm pretty sure that if you are trying to rotate objects based on a polygon then you will need to create a rotation matrix for that polygon and then make the matrix of the object you want to rotate equal to the rotation matrix.   That's how I understand it.
Back to Top
ello View Drop Down
Member
Member


Joined: 2005 Feb 04
Location: Germany
Online Status: Offline
Posts: 382
Post Options Post Options   Quote ello Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 1:51pm
well, what is so strange is that it works for spheres, but not for terrain objects:
http://tempfiles.earthcontrol.de/nm02.jpg
Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 2:20pm
Here's how I am doing it.   

[CODE]

//ROTATION MATRIX
//GetPoints
Vector c = polyLocation; //midpoint
Vector p = points[lngA]; //polygon point
Vector n = CalcFaceNormal(points, selectedPoly[lngI]); //normal

Vector scale = Vector(Len(opMatrix.v1), Len(opMatrix.v2), Len(opMatrix.v3)); //Get Scale
//Contruct Matrix
Matrix rotMatrix;

rotMatrix.off = p; //The base of the matrix
rotMatrix.v1 = !((p - c)%n); //X axis points toward the second point
rotMatrix.v2 = !(rotMatrix.v1 % n);//Y Axis is perpendicular to the X axis
rotMatrix.v3 = !(n); //Z Axis is along the normal
rotMatrix.v1 = !(rotMatrix.v1 * scale.x);
rotMatrix.v2 = !(rotMatrix.v2 * scale.y);
rotMatrix.v3 = !(rotMatrix.v3 * scale.z);
//place the first object within the rotation matrix 
firstSelection->SetMg(rotMatrix);
firstSelection->SetPos((opMatrix * polyLocation));// + firstSelection->GetRad().x);
[/code]

This is working quite well now..    creating a rotation matrix gives me the exact rotation of that specific polygon.

Hope this helps you.

~Shawn


Edited by emberintherain - 2010 Mar 15 at 2:41pm
Back to Top
ello View Drop Down
Member
Member


Joined: 2005 Feb 04
Location: Germany
Online Status: Offline
Posts: 382
Post Options Post Options   Quote ello Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 2:34pm
thank you so much! now after incorporating the CalcFaceNormal like you did, everything works fine.

cheers,
Ello

Back to Top
emberintherain View Drop Down
Member
Member
Avatar

Joined: 2008 Dec 23
Location: United States
Online Status: Offline
Posts: 487
Post Options Post Options   Quote emberintherain Quote  Post ReplyReply Direct Link To This Post Posted: 2010 Mar 15 at 2:59pm
Great.  I'm glad it worked for you.   I have got it now so everything is working 100% for me too....   Good day for us both!  :)


~Shawn
Back to Top
 Post Reply Post Reply Page  <123>

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.125 seconds.