Path: chuka.playstation.co.uk!news From: sceetech Newsgroups: scee.yaroze.programming.libraries Subject: Re: Rotation Matrix Functions : The Solution? Date: Tue, 15 Apr 1997 09:46:00 +0100 Organization: SCEE Lines: 209 Message-ID: <33534048.2B79@interactive.sony.com> References: <01bc4074$bd79ee80$d603e8c3@fred> <334A0C83.5959@micronetics.com> <3348D96D.289@interactive.sony.com> <334b2016.21064909@news.playstation.co.uk> <01bc4448$a37ddc80$0b16e8c3@fred> <334BA817.4A3C@interactive.sony.com> <01bc454b$1c8e8fe0$5214e8c3@fred> <334CA29A.539A@interactive.sony.com> <01bc45ea$2a00f1a0$c209e8c3@fred> <01bc46b0$08c8d860$ce08e8c3@fred> <3351F803.EB8@interactive.sony.com> <01bc4928$a4dd0940$f61ae8c3@fred> Reply-To: ps_yaroze@interactive.sony.com NNTP-Posting-Host: 194.203.13.10 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 3.01 (Win95; I) Looking at the code, it seems fine, except that there's no need to use the sin and cos functions (which have small inaccuracies) to get x, y and z matrices; instead of xMatrix=xrotmatrix(rotation->vx); yMatrix=yrotmatrix(rotation->vy); zMatrix=zrotmatrix(rotation->vz); use setVECTOR( &xVector, rotation->vx, 0, 0); RotMatrix( &xVector, &xMatrix); this will reduce the error. To sort this I will need to see printouts of the matrix (that was the thing in your earlier posting that made it look like the normalisation wasn't working). Good luck Lewis Michael Enoch wrote: > > > Two things: > > > > firstly, your earlier postings gave printouts of a matrix; > > after some rotation, the matrix columns were blatantly not of > > size 4096, which the normalisation in code should guarantee. > > > > secondly, object stretching is a Definite sign of matrix > > distortion; to sort this, please post your code for maintaining/ > > re-normalising the matrix, AND that for doing the basic rotation. > > > > Lewis > > > > OK, I've reformated the code slightly to make it more readable. It starts > at the comment with asterixes and ends at another. If it seems familiar > then thats because I borrowed the basics from a demo program. The matrix > fixing code I wrote is in the middle somewhere with a label so you can't > miss it. You'll notice it fixes an object called ship[0] every time, this > is because I'm only working with one object at the moment and I just wanted > to see if it worked before making a final alteration to the function. The > rsin() and rcos() functions are also borrowed from a demo, they just supply > sine and cosine values from tables(in the correct range to 4096). I'm > fairly sure they are not faulty but I'm going to rewrite them anyway after > posting this just to be sure. > Thanks, Mike. > > //************************************************************************** > *************** > > void CalculateObjectPosition(MATRIX* GlobalMatrix, > VECTOR* rotation, > VECTOR* position, > VECTOR* velocity, > GsCOORDINATE2* coordSystem) > { > int i=0; > u_long length; > VECTOR realMovement; > MATRIX xMatrix, yMatrix, zMatrix; > MATRIX copy; > > // velocity is object-relative, is firstly converted to > // world-relative terms > // eg > // find the object-local velocity in super coordinate terms > ApplyMatrixLV(GlobalMatrix, velocity, &realMovement); > > // update position by super-relative movement > position->vx += realMovement.vx; > position->vy += realMovement.vy; > position->vz += realMovement.vz; > > xMatrix=xrotmatrix(rotation->vx); > yMatrix=yrotmatrix(rotation->vy); > zMatrix=zrotmatrix(rotation->vz); > > // to get world-relative rotations instead: > // use > MulMatrix0(GlobalMatrix, &xMatrix, ©); > MulMatrix0(©, &yMatrix, GlobalMatrix); > MulMatrix0(GlobalMatrix, &zMatrix, ©); > > *GlobalMatrix = copy; > > //--------------------------Start of Matrix fix code > // This function is in main(), ship[0] is a global structure, > // containing an object handler, a coord system and some > // vectors which I pass through as the function arguments. > if(ship[0].moved==1) > { > ship[0].moved=0; > i=0; > length=sqrt( > (ship[0].GlobalMatrix.m[i][0]*ship[0].GlobalMatrix.m[i][0])+ > (ship[0].GlobalMatrix.m[i][1]*ship[0].GlobalMatrix.m[i][1])+ > (ship[0].GlobalMatrix.m[i][2]*ship[0].GlobalMatrix.m[i][2])); > > ship[0].GlobalMatrix.m[i][0]= > (ship[0].GlobalMatrix.m[i][0]*ONE)/length; > ship[0].GlobalMatrix.m[i][1]= > (ship[0].GlobalMatrix.m[i][1]*ONE)/length; > ship[0].GlobalMatrix.m[i][2]= > (ship[0].GlobalMatrix.m[i][2]*ONE)/length; > > i=1; > length=sqrt( > (ship[0].GlobalMatrix.m[i][0]*ship[0].GlobalMatrix.m[i][0])+ > (ship[0].GlobalMatrix.m[i][1]*ship[0].GlobalMatrix.m[i][1])+ > (ship[0].GlobalMatrix.m[i][2]*ship[0].GlobalMatrix.m[i][2])); > > ship[0].GlobalMatrix.m[i][0]= > (ship[0].GlobalMatrix.m[i][0]*ONE)/length; > ship[0].GlobalMatrix.m[i][1]= > (ship[0].GlobalMatrix.m[i][1]*ONE)/length; > ship[0].GlobalMatrix.m[i][2]= > (ship[0].GlobalMatrix.m[i][2]*ONE)/length; > > i=2; > length=sqrt( > (ship[0].GlobalMatrix.m[i][0]*ship[0].GlobalMatrix.m[i][0])+ > (ship[0].GlobalMatrix.m[i][1]*ship[0].GlobalMatrix.m[i][1])+ > (ship[0].GlobalMatrix.m[i][2]*ship[0].GlobalMatrix.m[i][2])); > > ship[0].GlobalMatrix.m[i][0]= > (ship[0].GlobalMatrix.m[i][0]*ONE)/length; > ship[0].GlobalMatrix.m[i][1]= > (ship[0].GlobalMatrix.m[i][1]*ONE)/length; > ship[0].GlobalMatrix.m[i][2]= > (ship[0].GlobalMatrix.m[i][2]*ONE)/length; > } > //------------------End of Matrix fix code > > // copy GlobalMatrix > coordSystem->coord = *GlobalMatrix; > > // set position absolutely > coordSystem->coord.t[0] = position->vx; > coordSystem->coord.t[1] = position->vy; > coordSystem->coord.t[2] = position->vz; > > // tell GTE that coordinate system has been updated > coordSystem->flg = 0; > } > > MATRIX xrotmatrix(int r) > { > MATRIX tmp; > > tmp.m[0][0]=ONE; > tmp.m[0][1]=0; > tmp.m[0][2]=0; > tmp.m[1][0]=0; > tmp.m[1][1]=rcos(r); > tmp.m[1][2]=-rsin(r); > tmp.m[2][0]=0; > tmp.m[2][1]=rsin(r); > tmp.m[2][2]=rcos(r); > > return tmp; > } > > MATRIX yrotmatrix(int r) > { > MATRIX tmp; > > tmp.m[0][0]=rcos(r); > tmp.m[0][1]=0; > tmp.m[0][2]=rsin(r); > tmp.m[1][0]=0; > tmp.m[1][1]=ONE; > tmp.m[1][2]=0; > tmp.m[2][0]=-rsin(r); > tmp.m[2][1]=0; > tmp.m[2][2]=rcos(r); > > return tmp; > } > > MATRIX zrotmatrix(int r) > { > MATRIX tmp; > > tmp.m[0][0]=rcos(r); > tmp.m[0][1]=-rsin(r); > tmp.m[0][2]=0; > tmp.m[1][0]=rsin(r); > tmp.m[1][1]=rcos(r); > tmp.m[1][2]=0; > tmp.m[2][0]=0; > tmp.m[2][1]=0; > tmp.m[2][2]=ONE; > > return tmp; > } > > //************************************************************************* > *******************