# Determining X Coordinate Based On Angles...

Fayetteville, NC, USA
OK, I have a simple clipping function up that also sets values based on where the vertex is based on the player's view. Now I can get z (the distance form the player) and y (above/below the player) easily, but how do I calculate x? I also found out that i cannot use indirection floating point values, so how can I point x, y, and z in the function to the memory location of the three actual variables being clipped and drawn?
[code]
bool CheckClipPlane(float x, float y, float z)
{
int distance;

distance = sqrt(((x - player.xpos) * (x - player.xpos)) + ((y - player.ypos) * (y - player.ypos)) + ((z - player.zpos) * (z - player.zpos)));

if(distance > 512)
return false;

if(x < (player.xpos + cos((player.heading - 45) * pi / 180.0) * distance) || z < (player.zpos - sin((player.heading - 45) * pi / 180.0) * distance))
return false;

if(x > (player.xpos + cos((player.heading + 45) * pi / 180.0) * distance) || z > (player.zpos + sin((player.heading + 45) * pi / 180.0) * distance))
return false;

//Now set x y z to how they appear in the FOV
z = distance; //How far out the vertex is
y -= player.ypos; //How far above or below the player the vertex is
// x = ?; //How do you do x???

return true;
}
[/code]
Now you would call the function with say "if(CheckClipPlane(x_m, y_m, z_m))". Now if it is too far to be seen, or the vertex is outside the player's FOV, it will return false and go on to examine the next triangle. If the vertex IS in the FOV, I need to have x_m, y_m, and z_m modified so that when I send the data to OpenGL it'll draw it as if the player really was moving around. Thanks for the help!

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

• : OK, I have a simple clipping function up that also sets values based on where the vertex is based on the player's view. Now I can get z (the distance form the player) and y (above/below the player) easily, but how do I calculate x? I also found out that i cannot use indirection floating point values, so how can I point x, y, and z in the function to the memory location of the three actual variables being clipped and drawn?
: [code]
: bool CheckClipPlane(float x, float y, float z)
: {
: int distance;
:
: distance = sqrt(((x - player.xpos) * (x - player.xpos)) + ((y - player.ypos) * (y - player.ypos)) + ((z - player.zpos) * (z - player.zpos)));
:
: if(distance > 512)
: return false;
:
: if(x < (player.xpos + cos((player.heading - 45) * pi / 180.0) * distance) || z < (player.zpos - sin((player.heading - 45) * pi / 180.0) * distance))
: return false;
:
: if(x > (player.xpos + cos((player.heading + 45) * pi / 180.0) * distance) || z > (player.zpos + sin((player.heading + 45) * pi / 180.0) * distance))
: return false;
:
: //Now set x y z to how they appear in the FOV
: z = distance; //How far out the vertex is
: y -= player.ypos; //How far above or below the player the vertex is
: // x = ?; //How do you do x???
:
: return true;
: }
: [/code]
: Now you would call the function with say "if(CheckClipPlane(x_m, y_m, z_m))". Now if it is too far to be seen, or the vertex is outside the player's FOV, it will return false and go on to examine the next triangle. If the vertex IS in the FOV, I need to have x_m, y_m, and z_m modified so that when I send the data to OpenGL it'll draw it as if the player really was moving around. Thanks for the help!
: -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]
:

If you're planning to do something serious that is not a good approach.Matrices is what you should use(almost every 3d game uses them).The position and orientation of an object in 3d space can be represented either by a 3x3 matrix and a position vector, or a 4x4 matrix(last row it's the position vector). I've chosen the 3x3 matrix and the pos. vector.The rows of the matrix represent in order: the RIGHT vector, the UP vector and the last row is the FORWARD vector. But keep in mind that theese vector's must be normalized(ie. their length must be 1) and there must be 90 deg between them.Now let's say the camera orientation matrix is M ,the camera pos is P ,the world vertex is V and the camera space vertex(the one you need to find out) is cV.You'll have:
cV.x=M.RIGHT*(V-P)
cV.y=M.UP*(V-P)
cV.z=M.FORWARD*(V-P)
where v1*v2 =v1.x*v2.x+v1.y*v2.y+v1.z*v2.z , v1, v2 vectors
And that's all folks, I hope I didn't write this for nothing

• Fayetteville, NC, USA
: If you're planning to do something serious that is not a good approach.Matrices is what you should use(almost every 3d game uses them).The position and orientation of an object in 3d space can be represented either by a 3x3 matrix and a position vector, or a 4x4 matrix(last row it's the position vector). I've chosen the 3x3 matrix and the pos. vector.The rows of the matrix represent in order: the RIGHT vector, the UP vector and the last row is the FORWARD vector. But keep in mind that theese vector's must be normalized(ie. their length must be 1) and there must be 90 deg between them.Now let's say the camera orientation matrix is M ,the camera pos is P ,the world vertex is V and the camera space vertex(the one you need to find out) is cV.You'll have:
: cV.x=M.RIGHT*(V-P)
: cV.y=M.UP*(V-P)
: cV.z=M.FORWARD*(V-P)
: where v1*v2 =v1.x*v2.x+v1.y*v2.y+v1.z*v2.z , v1, v2 vectors
: And that's all folks, I hope I didn't write this for nothing
:
Hey I REALLY appreciate the info. I knew I had to use matrices somehow but wasn't sure exactly how they were used. However, i am still not clear on a few things. What do you mean by "normalized"? Also, what's with the 90deg deal? You mean 90deg between the traingle's vertices? And finally, is there any advantage to using a 4x4 or 3x3 matrix? I mean, is one faster than the other? I would like my engine to run on slightly older machines (hopefully down to p2/300) because a lot of people I know still have 200mhz-400mhz machines which are relatively new, but technology has just sky-rocketed this year! Thanks again for the help and I'll see what I can do with what I have here. Oh and if you could direct me to a site where I can actually see some source using the matrices or show me some yourself I would be indebted to you.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

• A nomalized vector is vector wich has the length 1, that is v.x*v.x+v.y*v.y+v.z*v.z=sqrt(1)=1. The 90 deg angle must be between the matrix's direction vectors( m(M.RIGHT, M.UP)=m(M.UP,M.FORWARD)=m(M.RIGHT, M.FORWARD)=90 deg). I've already created a small 3d engine and i've used 3x3 matrices( they are faster). You can download a small demo at www.geocities.com/ionuttro , but there aren't any sources in it. I've learned to use matrices from a small 3d engine called the "Frog engine" by Thierry Tremblay, but that was 2 years ago. I see there is some information you could use at www.gamedev.net/reference/articles/article673.asp

• Fayetteville, NC, USA
: A nomalized vector is vector wich has the length 1, that is v.x*v.x+v.y*v.y+v.z*v.z=sqrt(1)=1. The 90 deg angle must be between the matrix's direction vectors( m(M.RIGHT, M.UP)=m(M.UP,M.FORWARD)=m(M.RIGHT, M.FORWARD)=90 deg). I've already created a small 3d engine and i've used 3x3 matrices( they are faster). You can download a small demo at www.geocities.com/ionuttro , but there aren't any sources in it. I've learned to use matrices from a small 3d engine called the "Frog engine" by Thierry Tremblay, but that was 2 years ago. I see there is some information you could use at www.gamedev.net/reference/articles/article673.asp
:
The tutorial showed me basically what I know, but I am still unclear on how to get what goes into the matrix. After that, what do I do with the matrix? I am a visual learner and math was my weakest subject in HS and now in college. I took algebra three times in HS, although i later found out that one of the first two teachers I had was fired due to lack of her own knowledge in the subject... haha! Is there a site where I can actually [b]see[/b] some of this source? I have looked for this "Frog Engine" breifly but cannot seem to find it. Oh, and your engine isn't half bad! I've figured out how to draw polys/objects and rotate, zoom, and flip them, but the 3D first person perspective is still throwing me. Do you yourself have any tutorials on how to do all this that show some of the actual calculations? Thanks for your help, E_on, I REALLY appreciate it.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

• Fayetteville, NC, USA
OK, I've been to gamedev.net and read through a ton of tutorials on this, but the formula does NOT work. Take this example:
x=10
y=5
z=7
sqrt(x * x + y * y + z * z) = 13.something

The only way i can get that formula to work is to plug 0's and 1's into the x, y, and z values. And as much reading as I have done (almost two and a half hours), I am not any closer to figuring this out. A lot of people can write tutorials, but I need to SEE the code to understand it. One other thing is that I don't think I've seen anything on calculating what's in view. If this math is the actual drawing stuff, i do NOT need it. I simply pass 3 vertices to OpenGL and IT z-buffers and draws them. All I need to do is find out what vertices are in the player's FOV to send to OpenGL. Here is how this works:
[code]
glBegin(GL_TRIANGLES);
glNormal3f(0.0f, 0.0f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(5, 5, 5);

glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0, 0, 5);

glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(10, 0, 5);
glEnd();
[/code]
This example will draw a triangle with the top being at 5, 5, 5, the left bottom being at 0, 0, 5, and the right bottom being at 10, 0, 5. As you can see, all I need are the traingles that are in the player's sight, no matter what angle he is looking, and the proper degree of rotation to send to glRotate3f(). Then glRotate3f can properly rotate the vertices and I can send that data to glVertex3f! How do I do that? Thanks for the help, E_on.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

• I said that the direction vectors MUST BE normalized, never said that they are already.For your example:
x=10
y=5
z=7
sqrt(x * x + y * y + z * z) = sqrt(174) = 13.something
1 / 13.some = 0.076...
x*0.076...=0.76...
y*0.076...=0.381...
z*0.076...=0.533...
and now we have sqrt(x * x + y * y + z * z) = sqrt(1) = 1
I'm sorry you had such "untalented" math teachers. I don't know any OpenGL stuff so I can't help you with it.

• Fayetteville, NC, USA
: I said that the direction vectors MUST BE normalized, never said that they are already.For your example:
: x=10
: y=5
: z=7
: sqrt(x * x + y * y + z * z) = sqrt(174) = 13.something
: 1 / 13.some = 0.076...
: x*0.076...=0.76...
: y*0.076...=0.381...
: z*0.076...=0.533...
: and now we have sqrt(x * x + y * y + z * z) = sqrt(1) = 1
: I'm sorry you had such "untalented" math teachers. I don't know any OpenGL stuff so I can't help you with it.
:
Ah so I had to multiply the results by the coordinates! That's why I kept getting all kinds of screwy errors. As for rotation, I've figured out how to rotate stuff easily. Now with this maybe I can also make it relative to the camera. Thanks for all of your help E_on, I owe ya' one.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]