Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A question on 3d Matrices...
#1
Yeah, I know, thats all I seem to do. However, I seriously need this question answered. I'm working on a 3d matrix engine, and everything works BUT rotation! The tutorial I got my info on said that for rotations, I must use the sine and cosine of the rotation on various locations in the different matrices. Can anyone possibly tell me what I'm doing wrong?
[Image: Signature.png]
Reply
#2
Code Pls....

Rotate from a *copy* not the original model/object.
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#3
Here's the code:
Code:
'3d matrix attempt
'these are the variables
DIM omatrix(4, 4)  'this is the object matrix
DIM rotx(4, 4) 'the x rotation matrix
DIM roty(4, 4) 'the y rotation matrix
DIM rotz(4, 4) 'the z rotation matrix
DIM tran(4, 4) 'the translation matrix
DIM ovect(3)  'the object vector
DIM costable(360)  'the cosine table
DIM sintable(360)  'the sine table
DIM rx
DIM ry
DIM rz

'create the sine/cosine tables
FOR i = 1 TO 360
costable(i) = COS(i)
sintable(i) = SIN(i)
NEXT i
'the object location will be x=10, y=5, z=7
ovect(1) = 10
ovect(2) = 5
ovect(3) = 7

'set all the matrices to identity
y = 1
DO
FOR x = 1 TO 4
omatrix(x, y) = 0
NEXT x
y = y + 1
LOOP UNTIL y = 5
omatrix(1, 1) = 1: omatrix(2, 2) = 1: omatrix(3, 3) = 1: omatrix(4, 4) = 1

y = 1
DO
FOR x = 1 TO 4
rotx(x, y) = 0
NEXT x
y = y + 1
LOOP UNTIL y = 5
rotx(1, 1) = 1: rotx(2, 2) = 1: rotx(3, 3) = 1: rotx(4, 4) = 1

y = 1
DO
FOR x = 1 TO 4
roty(x, y) = 0
NEXT x
y = y + 1
LOOP UNTIL y = 5
roty(1, 1) = 1: roty(2, 2) = 1: roty(3, 3) = 1: roty(4, 4) = 1

y = 1
DO
FOR x = 1 TO 4
rotz(x, y) = 0
NEXT x
y = y + 1
LOOP UNTIL y = 5
rotz(1, 1) = 1: rotz(2, 2) = 1: rotz(3, 3) = 1: rotz(4, 4) = 1

y = 1
DO
FOR x = 1 TO 4
tran(x, y) = 0
NEXT x
y = y + 1
LOOP UNTIL y = 5
tran(1, 1) = 1: tran(2, 2) = 1: tran(3, 3) = 1: tran(4, 4) = 1


'this loop will rotate a point in all axis around ovect
SCREEN 7, 0, 1, 0
rx = 1
ry = 1
rz = 1
tx = 5
ty = 0
tz = 0
DO
'set up the special properties of the rotation and translation matrices
'translation
tran(4, 1) = tx
tran(4, 2) = ty
tran(4, 3) = tz

'x-rotation
rotx(2, 2) = costable(rx)
rotx(2, 3) = sintable(rx)
rotx(3, 2) = -sintable(rx)
rotx(3, 3) = costable(rx)

'y-rotation
roty(1, 1) = costable(ry)
roty(1, 3) = -sintable(ry)
roty(3, 1) = sintable(ry)
roty(3, 3) = costable(ry)

'z-rotation
rotz(1, 1) = costable(rz)
rotz(1, 2) = sintable(rz)
rotz(2, 1) = -sintable(rz)
rotz(2, 2) = costable(rz)

press$ = INKEY$
CLS
y = 1
DO
FOR x = 1 TO 4
omatrix(x, y) = omatrix(x, y) * tran(x, y) * rotx(x, y) * roty(x, y) * rotz(x, y)
NEXT x
y = y + 1
LOOP UNTIL y = 5

'add world coords
omatrix(4, 1) = tx + ovect(1)
omatrix(4, 2) = ty + ovect(2)
omatrix(4, 3) = tz + ovect(3)

'create the final coords of the object
x = ovect(1) * omatrix(1, 1) + ovect(2) * omatrix(2, 1) + ovect(3) * omatrix(3, 1) + omatrix(4, 1)
y = ovect(1) * omatrix(1, 2) + ovect(2) * omatrix(2, 2) + ovect(3) * omatrix(3, 2) + omatrix(4, 2)
z = ovect(1) * omatrix(1, 3) + ovect(2) * omatrix(2, 3) + ovect(3) * omatrix(3, 3) + omatrix(4, 3)
'convert the point(s)
sx = 256 * (x / (z + 256)) + 320 / 2
sy = 256 * (y / (z + 256)) + 200 / 2
'plot the point on the screen
PSET (sx, sy), 15
rx = rx + 1
IF rx > 360 THEN rx = 1
ry = ry + 1
IF ry > 360 THEN ry = 1
rz = rz + 1
IF rz > 360 THEN rz = 1
PCOPY 1, 0

LOOP UNTIL press$ <> ""
[Image: Signature.png]
Reply
#4
Here's some code I got from the "Demo making" column at flipcode (c++)

Code:
#ifndef __MATRIX_H_
#define __MATRIX_H_

#include "vector.h"

class MATRIX
{

    VECTOR m[3];

public:

          VECTOR &operator[](const int i)       { return m[i]; }
    const VECTOR &operator[](const int i) const { return m[i]; }


    MATRIX operator*(const MATRIX &b) const
        {
               MATRIX r;
               for (int i=0; i<3; i++)
                   for (int j=0; j<3; j++)
                   {
                         r[i][j] = m[i][0] * b[0][j]
                                + m[i][1] * b[1][j]
                                + m[i][2] * b[2][j];
                   }
               return r;
        }

    MATRIX(    const double a11, const double a12, const double a13,
        const double a21, const double a22, const double a23,
        const double a31, const double a32, const double a33)
    {
        m[0][0]=a11; m[0][1]=a12; m[0][2]=a13;
        m[1][0]=a21; m[1][1]=a22; m[1][2]=a23;
        m[2][0]=a31; m[2][1]=a32; m[2][2]=a33;
    }

        VECTOR MATRIX::operator*(const VECTOR &v) const
        {
           VECTOR r;
           r[0] = v[0] * m[0][0] + v[1] * m[1][0] + v[2] * m[2][0];
           r[1] = v[0] * m[0][1] + v[1] * m[1][1] + v[2] * m[2][1];
           r[2] = v[0] * m[0][2] + v[1] * m[1][2] + v[2] * m[2][2];
           return r;
        }

     MATRIX() {}
    ~MATRIX() {}
};

MATRIX rotX(const double theta)
{
    const double c = cos(theta);
    const double s = sin(theta);
    return MATRIX(     1, 0, 0,
             0, c, s,
             0,-s, c);
}

MATRIX rotY(const double theta)
{
    const double c = cos(theta);
    const double s = sin(theta);
    return MATRIX(     c, 0,-s,
             0, 1, 0,
             s, 0, c);
}

MATRIX rotZ(const double theta)
{
    const double c = cos(theta);
    const double s = sin(theta);
    return MATRIX(     c, s, 0,
            -s, c, 0,
             0, 0, 1);
}

#endif
am an asshole. Get used to it.
Reply
#5
Well, as jark seems not to be watching this thread, this what i think.

You seem to have coded the matrix cross-product (Is this its name in english?).

each element in the output matrix is the sum -product of a row in the first matrix by a column in the second one
The following code is not tested (written directly in the form)

Code:
dim object(1 to 4,1 to 4)
dim rot(1 to 4,1 to 4)
dim output(1 to 4,1 to 4)

'output = object x rot
for i=1 to 4
for j=1 to 4
   output(i,j)=0
   for k=1 to 4
       output(i,j)=output(i,j)+object(i,k)*rot(k,j)
   next
next

You can't cross product many matrices in a single operation,so you should add all rot matrices plus the translation matrix in a single one.

I have never used the 4th (translation) row- column so here is the code for the rotation components. The code is from the 3d mesh rotator you can find at my site.

Code:
a! = COS(xrot!)
B! = SIN(xrot!)
c! = COS(yrot!)
d! = SIN(yrot!)
E! = COS(zrot!)
F! = SIN(zrot!)
AD! = a! * d!
BD! = B! * d!

rot(1,1) = c! * E!
rot(1,2) = -c! * F!
rot(1,3) = -d!

rot(2,1) = -BD! * E! + a! * F!
rot(2,2) = BD! * F! + a! * E!
rot(2,3) = -B! * c!

rot(3,1) = AD! * E! + B! * F!
rot(3,2) = -AD! * F! + B! * E!
rot(3,3) = a! * c!

Hope it helps....
Antoni
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)