# 3d Coordinates To 2d Screen Coordinates

4 replies to this topic

### #1 biggoron

biggoron

dura lex sed lex

• New Member
• 652 posts

Posted 04 December 2006 - 09:31 PM

Hi, I'm looking for a method to convert 3D coordinates to a point on the screen where they would be drawn. I had a good search, but I couldn't find anything useful.

So sorry if this has been asked before.

Thanks.
• 0

### #2 flexaplex

flexaplex

GMC Member

• Global Moderators
• 4809 posts
• Version:GM8

Posted 04 December 2006 - 09:37 PM

I am having exactly the same problem:

i am trying to draw a 3D cuboid (not using gm's built in 3D functions)

i want to get the coords of the 8 vertices of the cuboid and draw it on the screen.

i have the x,y,z,width,height,lenght of the cube then i apply this rotation matrix on the coordinates:

```Ma[1,1] = cos(zt1)*cos(yt1);
Ma[1,2] = -sin(zt1)*cos(xt1)+cos(zt1)*sin(yt1)*sin(xt1);
Ma[1,3] = sin(zt1)*sin(xt1)+cos(zt1)*sin(yt1)*cos(xt1);
Ma[2,1] = sin(zt1)*cos(yt1);
Ma[2,2] = cos(zt1)*cos(xt1)+sin(zt1)*sin(yt1)*sin(xt1);
Ma[2,3] = -cos(zt1)*sin(xt1)+sin(zt1)*sin(yt1)*cos(xt1);
Ma[3,1] = sin(yt1);
Ma[3,2] = -cos(yt1)*sin(xt1);
Ma[3,3] = -cos(yt1)*cos(xt1);```

where xt1,yt1,zt1 are the angle's of rotation around the x,y,z axis.

then i have used:

```X[1] = (x1+w1)*Ma[1,1]+(y1   )*Ma[1,2]+(z1   )*Ma[1,3];
Y[1] = (x1+w1)*Ma[2,1]+(y1   )*Ma[2,2]+(z1   )*Ma[2,3];
Z[1] = (x1+w1)*Ma[3,1]+(y1   )*Ma[3,2]+(z1   )*Ma[3,3];
X[2] = (x1   )*Ma[1,1]+(y1   )*Ma[1,2]+(z1   )*Ma[1,3];
Y[2] = (x1   )*Ma[2,1]+(y1   )*Ma[2,2]+(z1   )*Ma[2,3];
Z[2] = (x1   )*Ma[3,1]+(y1   )*Ma[3,2]+(z1   )*Ma[3,3];
X[3] = (x1   )*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inates of the 8 vertices.

//where x1,y1,z1 or the coords for the cube and w1,h1,l1 are width, height, lenght
of the cube.
and x2,y2,z2 is the center of rotation

But this is ignoring the Z component of the coordinates at the end. The problem is i don't know how to get these 3-D coords into 2D coords to be drawn onto the screen.
It seems to partially work still about from the y-rotation doesn't really work.

That might give you some indication of how to go about it, but i don't really know, you need to add perception to it somehow or something,do you mind if i tag along with this topic??

Edited by flexaplex, 04 December 2006 - 09:42 PM.

• 0

### #3 Yourself

Yourself

The Ultimate Pronoun

• Retired Staff
• 7341 posts
• Version:Unknown

Posted 05 December 2006 - 03:24 AM

• 0

### #4 flexaplex

flexaplex

GMC Member

• Global Moderators
• 4809 posts
• Version:GM8

Posted 05 December 2006 - 04:36 AM

ok thankyou yourself,

however i have very limited knowledge of what the arguments of:
d3d_set_projection_ext(xfrom,yfrom,zfrom,xto,yto,z
to,xup,yup,zup,angle,aspect,znear,zfar)

actually do, what exactly are "angle" and "aspect"?

in the other topic you said to set "angle" to 0...
however in your script you have put:

tFOV = tan(argument9*pi/360); //where argument9 is equivalent "angle"

this thus returns 0 when "angle" is set to 0.

then later you have used:

mm = (pX*vX+pY*vY+pZ*vZ)/sqr(argument10*tFOV);

however tFOV will equal 0 so this will return C/0 thus will not compute...

i am currently using:

set_XY_array7(0,0,0,global.roomlenght,global.roomw
idth,global.roomheight,xt1,yt1,zt1,0,1,x1+w1,y1,z1)
;

have i got the arguments mixed up or something?? I don't know.

Edited by flexaplex, 05 December 2006 - 04:37 AM.

• 0

### #5 Yourself

Yourself

The Ultimate Pronoun

• Retired Staff
• 7341 posts
• Version:Unknown

Posted 05 December 2006 - 05:59 AM

in the other topic you said to set "angle" to 0...

That was in reference to a completely different function. Please read what I'm replying to so you actually know what question that is an answer to.

The angle and aspect arguments should just be set to 45 and room_width/room_height (or view_wview[i]/view_hview[i]) respectively. In 3D the camera forms a rectangular pyramid. Anything iside this pyramid is visible. The angle argument is the angle between the top and bottom planes of the pyramid (ie. the angle between the top and bottom of your screen). The aspect argument is simply the aspect ratio of the screen. It's the ratio of width to height of the window. This tells the projection formulas how wide our pyramid has to be.
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users