[Equaciones 3D] Alguien entiende del tema?

Discusión en general sobre Gemix.

[Equaciones 3D] Alguien entiende del tema?

Postby erkosone » Tue Oct 12, 2010 3:15 pm

Hola buenas, lo cuelgo en el general porque creo que es un tema de interes general, no se trata de pedir ayuda ni nada asi, se trata de pasaros esta información que he encontrado ya que se explica de una forma muy clara y concreta para la programación de 3d.

No se trata de ningun engine, ni programa ni nada parecido, es el sistema de ecuaficones "formulas" para calcular la posicion de un pixel con cordenadas (x,y,x) 3d reales en una pantalla 2d (x,y).

Estoy liado con ello desde hace un tiempo, y he consegido alguna cosilla.. pero no termino de pillarle el royo bien jeje, si alguno quiere hacer alguna prueba aqui le dejo la información que necesita. La primera parte es la mas interesante, pues explica como posicionar un punto 3d en la pantalla, y con varios puntos tenemos un casi objeto, y uniendo los putnos con draw tenemos un objeto.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
By Matt Bross - March 15th, 1997



3D Programming: An Explanation of the Basics as Well as Some Advanced



We have all seen the effects of 3D animation, from games like DOOM to the

movie Toy Story. Wouldn't it be cool to be able to make your own 3D stuff! In this little

paper I hope to explain how it is possible to do this on your own home computer.



Converting 3D to 2D



This is the easy part and the first thing you need to know. To convert 3D

coordinates to a 2D screen use this: X, Y, and Z are 3D coordinates on a Cartesian plane,

and SX and SY are the final screen coordinates.



SX = X / Z

SY = Y / Z



This is very logical and easy to understand, but not something that you would just

think of. The farther the Z, the closer the points will be together. With different

variables, if the viewer is at the origin, you must give each of the variables its own

coordinate system, relate them with variables and, then, modify the equation. So, do it

like this: The screen coordinates of programming language are not oriented around (0, 0,

0), the origin, so you must add half the screen resolution to the end of the equation to

center the object on the screen. Using the same variables, VX, VY, VZ as the position of

the viewer, and SRX and SRY as the screen's resolution.



SX = ((X - VX) / (Z - VZ)) + (SRX / 2)

SY = ((Y - VY) / (Z - VZ)) + (SRY / 2)



Now, if you define points and then draw lines between them, this will draw a 3d

shape; not very impressive but definitely a start. (There are other ways of converting 3D

to 2D (ortho, where coordinates (X, Y, Z) become (X,Y), and parallel projection, where

(X, Y, Z) becomes (X + Z, Y + Z), are two) but the way I showed you, perspective

projection, is the best. The more impressive things are rotation and translation.



Rotation and Translation



Now, for rotation, Everything up until now has been pretty easy if you think about

it, unless math really isn't your thing. Now comes the first, relatively speaking, hard part.

A rotation rotates all the points, and a translation moves all points over in the same

direction and the same amount. Translations are simpler, so I'll describe them first.

Simply add a translation value to every point's respective X, Y, or Z coordinate,

depending on what direction you are translating. When programming, you will want to

store this in a separate variable to make the object's own coordinate system. If you don't

understand how this gives the object its own coordinate system, then think about this: if

you don't use the translation values, then don't add the amounts of translation and you are

left with an object surrounding the origin. In order to save space, SX equals screen x; SY

equals screen y; X, Y, Z means virtual coordinates x, y, z; TX, TY, TZ are translations

along the x, y, and z axes; and SRX, SRY are the screen's resolution; and VX, VY, VZ

are the coordinates of the viewer.



SX = ((TX + X -VX) / (TZ + Z - VZ)) + (SRX / 2)

SY = ((TY + Y - VY) / (TZ + Z - VZ)) + (SRY / 2)



Translations can also be done with matrices following, but it is more complicated. This

also gives an introduction to matrix mathematics (see appendix D), which is used for

rotation.



( 1, 0, 0, 0) = T1

( 0, 1, 0, 0)

( 0, 0, 1, 0)

(-TX, -TY, -TZ, 1)



The same matrix can also be expressed in spherical coordinates (which are used for

rotation and are explained next) to keep everything in the same form.



( 1, 0, 0, 0) = T1

( 0, 1, 0, 0)

( 0, 0, 1, 0)

(-roe(COS theta)(SIN phi), -roe(SIN theta)(SIN phi), -roe(COS phi), 1)



For rotation you must use spherical coordinates. Spherical coordinates are coordinates

that are expressed as distances from the origin and in angles, rather than in distances from

the X, Y, and Z axes. Spherical coordinates are noted (theta, phi, roe) as roe(a Greek

letter) describes the distance from the origin, theta (another Greek letter) is the angle from

the XY plane and phi (yet another Greek letter) is the angle from the Z axis. The

conversions between the standard Cartesian plane and the spherical coordinates are:



X = roe (SIN theta)(COS phi) theta = arcTAN (X / Y)

Y = roe (SIN theta)(SIN phi) phi = arcCOS (Z / Roe)

Z = roe (COS theta) roe = SQR(X ^ 2 + Y ^ 2 + Z ^ 2)



For 3D animation, the distance from the origin never changes, so the only variables are

theta and phi. So, for rotation around just one axis you must first find each point's

distance from the origin. Using the equation above, which is derived from the

Pythagorean theorem (in the case that a and b are legs of a right triangle and c is the

hypotenuse of the triangle, a ^ 2 + b ^ 2 = c ^ 2, but since there is another variable - depth

- you must add another variable to the equation that I will call "e" (this is not official). So,

the equation becomes (a ^ 2 + b ^ 2 + e ^ 2) = c2. If this is on a 2D plane e = 0 and e ^ 2

= 0. So, it has no use, but in 3D, e ¹ 0, all the time.) Using this theorem, convert the point

to spherical coordinates, once again using the equations above, add the amount of rotation

to phi and then convert it back to normal Cartesian coordinates and plot the point on the

screen. This can be done with the following 4 * 4 matrices.



Z axis clockwise rotation



( SIN theta, COS phi, 0, 0) = T2

(-COS theta, SIN theta, 0, 0)

( 0, 0, 1, 0)

( 0, 0, 0, 1)



X axis counter-clockwise rotation



(1, 0, 0, 0)

(0, -COS phi, SIN phi, 0)

(0, SIN phi, COS phi, 0)

(0, 0, 0, 1)



To get the final matrix that will be used, and converted into 2D rectangular and plotted on

the screen, multiply all the preceding matrices together. F = T1 * T2 * T3.



F = ( -SIN theta, -(COS theta)(COS phi), -(COS theta)(SIN phi), 0)

( COS theta, -(SIN theta)(COS phi), -(SIN theta)(SIN phi), 0)

( 0, SIN phi, -COS phi, 0)

( 0, 0, roe, 1)



So the original (X, Y, Z, 1) translated and rotated = F * (X, Y, Z,1) = (Xf, Yf, Zf, 1).



Xf = -X(SIN theta) + COS theta

Yf = -X(COS theta)(COS phi) - Y(SIN theta)(COS phi) + Z(SIN phi)

Zf = -X(COS theta)(SIN phi) - Y(SIN theta)(SIN phi) - Z(COS phi) + roe



A simpler matrix might be



Rotation around the Y axis where Yan is the angle of rotation around the Y axis

(COS Yan, 0, SIN Yan) = T1

( 0, 1, 0)

(-SIN Yan, 0, COS Yan)



Rotation around the Z axis where An is the angle of rotation around the Z axis

(1, 0, 0) = T2

(0, COS An, -SIN An)

(0, SIN An, COS An)



Rotation around the X axis where Xan is the angle of rotation around the X axis

(COS Xan, -SIN Xan, 0) = T3

(-SIN Yan, COS Yan, 0)

( 0, 0, 1)



The final Matrix (T1 * T2 * T3) where S1 = SIN Yan, C2 = COS An, etc. is

( C1C3 + S1S2S3, -C1S3 + C3S1S2, C2S1) = F

( C2S3, C2C3, -S2)

(-C3S1 + C1S2S3, S1S3 + C1C3S2, C1C2)



So



Xf = x(s1s2s3 + c1c3) + y(c2s3) + z(c1s2s3 - c3s1)

Yf = x(c3s1s2 - c1s3) + y(c2c3) + z(c1c3s2 + s1s3)

Zf = x(c1s2s3 - c3s1) + y(-s2) + z(c1c2)



or simpler and faster (some operations are repeated) where R1 = the angle of rotation

around the X axis, R2 is around the Y, and R3 is the Z.



Rotation around the Y axis

TEMPX = X * COS R2 - Z * SIN R2

TEMPZ = X * SIN R2 + Z * COS R2



Rotation around the X axis

Zf = TEMPZ * COS R1 - Y * SIN R1

TEMPY = TEMPZ * SIN R1 + Y * COS R1



Rotation around the Z axis

Xf = TEMPX * COS R3 + TEMPY * SIN R3

Yf = TEMPY * COS R3 - TEMPX * SIN R3
User avatar
erkosone
 
Posts: 10654
Joined: Tue Feb 24, 2009 2:13 pm
Location: Barcelona.


Re: [Equaciones 3D] Alguien entiende del tema?

Postby erkosone » Tue Oct 12, 2010 11:33 pm

Es un documento estupendo y fantastico Nower, si no fuera porque no entiendo ni papa jeje..

Este tema normalmente lo explican con matrices o directamente con equaciones mas representativas para el programador, tu documento es estupendo para un matematico, pero para mi no me sirve de mucho jeje..

Iwalmente tengo varios codigos en Qbasic que hasta texturizan objetos en 3d programado en basic, es muy muy facil, lo que pasa es que al no tener float hay que hacer mucha formula internedia para calcular angulos, senos y cosenos con decimales..

En cuantito salga la Beta nueva supongo que todo esto va a mejorar mucho, haber si soy capaz de hacer el proyecto que tengo en mente desde la era de la SNES, "STAR FOX" en WIRELINES jeje, osea, todos los graficos con vectores y a base de tirar lineas.
User avatar
erkosone
 
Posts: 10654
Joined: Tue Feb 24, 2009 2:13 pm
Location: Barcelona.


Return to General

Who is online

Users browsing this forum: No registered users and 5 guests