• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Coordinates of vertex after glRotate.

This topic is 5342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

At first, sorry me for my bad english. I have a function in a c#, that make's a 3d cube (or something that, don't know how to name it correctly, maybe parallelepiped):
		public static void Cube(float x, float y, float z, float d_x, float d_y, float d_z)
{
// licevaja perednjaja
GL.glVertex3f(x-d_x,y-d_y,z-d_z);
GL.glVertex3f(x-d_x,y+d_y,z-d_z);
GL.glVertex3f(x+d_x,y+d_y,z-d_z);
GL.glVertex3f(x+d_x,y-d_y,z-d_z);
GL.glVertex3f(x-d_x,y-d_y,z+d_z);
GL.glVertex3f(x-d_x,y+d_y,z+d_z);
GL.glVertex3f(x+d_x,y+d_y,z+d_z);
GL.glVertex3f(x+d_x,y-d_y,z+d_z);
// bokovaja levaja
GL.glVertex3f(x-d_x,y-d_y,z-d_z);
GL.glVertex3f(x-d_x,y+d_y,z-d_z);
GL.glVertex3f(x-d_x,y+d_y,z+d_z);
GL.glVertex3f(x-d_x,y-d_y,z+d_z);
// bokovaja pravaja
GL.glVertex3f(x+d_x,y-d_y,z-d_z);
GL.glVertex3f(x+d_x,y+d_y,z-d_z);
GL.glVertex3f(x+d_x,y+d_y,z+d_z);
GL.glVertex3f(x+d_x,y-d_y,z+d_z);
// verhnaja niznjaja
GL.glVertex3f(x-d_x,y-d_y,z-d_z);
GL.glVertex3f(x-d_x,y-d_y,z+d_z);
GL.glVertex3f(x+d_x,y-d_y,z+d_z);
GL.glVertex3f(x+d_x,y-d_y,z-d_z);
// verhnaja verhnjaja
GL.glVertex3f(x-d_x,y+d_y,z-d_z);
GL.glVertex3f(x-d_x,y+d_y,z+d_z);
GL.glVertex3f(x+d_x,y+d_y,z+d_z);
GL.glVertex3f(x+d_x,y+d_y,z-d_z);
GL.glEnd();
}

here are 6 arguments in a function: x, y and z - where from is this cube will be draw; d_x, d_y and d_z - distance between x, y and z and -+d_x, -+d_y and -+d_z. So, next i want to rotate this "Cube". How i do that?
GL.glRotatef(angle,0.0f,1.0f,0.0f);
Cube(0,0.2f,0,0.5f,0.2f,1.0f);
Cube(0.6f,0.2f,0,0.1f,0.2f,1.0f);
Cube(-0.6f,0.2f,0,0.1f,0.2f,1.0f);
Cube(0,0.6f,0,0.3f,0.2f,0.5f);
Cube(0,0.7f,-1.0f,0.05f,0.05f,0.5f);

All is OK, but I need to know (for a collision) where all of the vertex in the parallelepiped are be, after the rotate. All x, y, z coordinates i need (maybe without y, because it isn't change). I know that, this question was at this forum, but there are answers are so bad, so i can't understand, what i would to do. Please, help me, and sorry for my bad english. [edited by - sunset ru on June 1, 2004 9:44:04 AM] [edited by - sunset ru on June 1, 2004 9:45:18 AM]

##### Share on other sites
In other words,what you want to do is to be able to transform the vertices in software,so you can store the results.It''s pretty simple,really.I''ll give you an example:

typedef float TVector[4];//4-component vector
typedef TVector TMatrix[4];//4x4 matrix represented by 4 vectors

float Dot4(float v1[],float v2[])
{
return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]+v1[3]*v2[3];
}

void ApplyMatrix(TMatrix &m,TVector &v)
{TVector temp;
for (int i=0;i<4;++i) temp=v[i];
v[0]=Dot4(m[0],temp);
v[1]=Dot4(m[1],temp);
v[2]=Dot4(m[2],temp);
v[3]=Dot4(m[3],temp);
}

You can now use the ApplyMatrix to transform any 4D vector using a 4x4 matrix.The 4th component of the vector is the homogenous coordinate and it''s 1 for vertices,0 for directions(usually normals).

Now if you want to transform vertices using you modelview matrix,so the results you get matches the one you get on the screen,all you have to do is this:

TVector v;
TMatrix mv;

glGetFloatv(GL_MODELVIEW_MATRIX,&mv[0][0]);//Get the current modelview matrix

ApplyMatrix(mv,v);

##### Share on other sites
Here is another problem (with matrix didn't understand how-to):
private static void Cube(float x, float y, float z, float d_x, float d_y, float d_z, double angle) {	float cos = (float)Math.Cos(degrees(angle));	float sin = (float)Math.Sin(degrees(angle));	GL.glBegin(GL.GL_QUADS); 	GL.glVertex3f((x-d_x)*cos,y-d_y,(z-d_z)*sin);	GL.glVertex3f((x-d_x)*cos,y+d_y,(z-d_z)*sin);	GL.glVertex3f((x+d_x)*sin,y+d_y,(z-d_z)*cos);	GL.glVertex3f((x+d_x)*sin,y-d_y,(z-d_z)*cos); 	GL.glVertex3f((x-d_x)*sin,y-d_y,(z+d_z)*cos);	GL.glVertex3f((x-d_x)*sin,y+d_y,(z+d_z)*cos);	GL.glVertex3f((x+d_x)*cos,y+d_y,(z+d_z)*sin);	GL.glVertex3f((x+d_x)*cos,y-d_y,(z+d_z)*sin); 	GL.glVertex3f((x-d_x)*cos,y-d_y,(z-d_z)*sin);	GL.glVertex3f((x-d_x)*cos,y+d_y,(z-d_z)*sin);	GL.glVertex3f((x-d_x)*sin,y+d_y,(z+d_z)*cos);	GL.glVertex3f((x-d_x)*sin,y-d_y,(z+d_z)*cos); 	GL.glVertex3f((x+d_x)*sin,y-d_y,(z-d_z)*cos);	GL.glVertex3f((x+d_x)*sin,y+d_y,(z-d_z)*cos);	GL.glVertex3f((x+d_x)*cos,y+d_y,(z+d_z)*sin);	GL.glVertex3f((x+d_x)*cos,y-d_y,(z+d_z)*sin); 	GL.glVertex3f((x-d_x)*cos,y-d_y,(z-d_z)*sin);	GL.glVertex3f((x-d_x)*sin,y-d_y,(z+d_z)*cos);	GL.glVertex3f((x+d_x)*cos,y-d_y,(z+d_z)*sin);	GL.glVertex3f((x+d_x)*sin,y-d_y,(z-d_z)*cos); 	GL.glVertex3f((x-d_x)*cos,y+d_y,(z-d_z)*sin);	GL.glVertex3f((x-d_x)*sin,y+d_y,(z+d_z)*cos);	GL.glVertex3f((x+d_x)*cos,y+d_y,(z+d_z)*sin);	GL.glVertex3f((x+d_x)*sin,y+d_y,(z-d_z)*cos);		GL.glEnd();}

Something wrong. And I know what is, but can't fix it. This formulas are good for cube, but for parallelepiped isn't.
How to fix it, or try to help me with this matrixes..

[edited by - sunset ru on June 1, 2004 1:05:58 PM]

##### Share on other sites

I hope this helps you. If some of the english in here is tough, let me know (you can do it via email if you like, just click on the email link above this post to get my e-mail) and I'll try and word it better for you. In any case, I really recommend that you learn the Linear Algebra of vectors, vertices, matrices, etc if you haven't already. Good luck!

~Urayami

[edited by - urayami on June 1, 2004 2:32:55 PM]

##### Share on other sites
Although the OP seems to be a bit lost, APs post helped me quite a bit. I started another thread on how to access the matrix stack to retrieve x, y, z positions, and the answer was right here all along. Thanks.

Just to be sure I got it here.. All I do is use the matrix stack for transformations, ie;

glPushMatrix();
glRotatef(...);
glTranslatef(...);

TVector v;
TMatrix mv;

glGetFloatv(GL_MODELVIEW_MATRIX,&mv[0][0]);
// TVector v now contains x, y, z position of the transformed matrix?

glPopMatrix();

I'm rusty at math and not too great at it, so I'm just making sure I understand what's going on with the calculations. Basically you're multiplying vector by the matrix to get a new vector containing the x, y, z values?

It seems for this to work properly I'd have to load the vector (v) with all 1's first, right?

Meh, like I said, my math sucks. Maybe I'm missing something here.

[edited by - wyrd on June 2, 2004 6:03:05 PM]

[edited by - wyrd on June 2, 2004 6:03:56 PM]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634059
• Total Posts
3015296
×