[Algorithms] Lots of problems...
Brought to you by:
vexxed72
From: dga <Dio...@mi...> - 2000-08-26 15:33:14
|
Hi all! I have some problems with a work I have to make for school... Please, please, please, please (ad nauseum) take the time to read this mail, since I'm pulling hairs already (spent the last 17 hours trying to get this right!! And I need to deliver this in the end of the next week... And lots of work still needing to be done) I'm trying to do a space game (simple one), but I have problems with several of it's components, that I've been unable to understand, let alone correct. 1) How to obtain the position of an object, based on it's transformation matrix ? I'm using DirectX, so a vector is transformed like this: Wv=v' I think I must use the (4,1), (4,2) and (4,3) positions of the matrix (line,column), to get the x, y and z of the object... Is this correct ? 2) How do I rotate an object around an arbitrary axis ? More exactly, given a matrix W that represents the objects current position, how do I built the matrix A such as WA gives me the transformation matrix for the object that rotates the object at it's current position around axis a=(ax,ay,az) ? Currently, I'm using the following code: D3DMATRIX mat; float c=float(cos(Deg2Rad(ang))); float s=float(sin(Deg2Rad(ang))); float c1=(1.0f-c); mat._11=(ax*ax)*c1+c; mat._12=(ax*ay)*c1-(az*s); mat._13=(ax*az)*c1+(ay*s); mat._21=(ay*ax)*c1+(az*s); mat._22=(ay*ay)*c1+c ; mat._23=(ay*az)*c1-(ax*s); mat._31=(az*ax)*c1-(ay*s); mat._32=(az*ay)*c1+(ax*s); mat._33=(az*az)*c1+c; mat._14=mat._24=mat._34=0.0f; mat._41=mat._42=mat._43=0.0f; mat._44=1.0f; but I think it fails in certain cases... :( 3) How do I project a 3d point into 2d ? I need to have the projection to get my lens flare to work properly... It sometimes work, in simple cases, but when I attach an lens flare to an object and use it's coordinates, it sometimes go off by a long way... I'm using the following code: x=GetXPos(); y=GetYPos(); z=GetZPos(); // v'=Cv, where v=(x,y,z) and C is the camera transformation matrix cameraTrans->PreMult(x,y,z,&vx,&vy,&vz); if (vz<=0) return; // v=Pv', where v'=(vx,vy,vz) and P is the perpective transformation matrix perspTrans->PreMult(vx,vy,vz,&x,&y,&z); // resX=resolution in X divided by 2, resY=resolution in Y divided by 2 x=resX*x; y=resY*y; x=resX+x; y=resY-y; I think this maybe has something to do with the first two questions, since if I set the flare position manually, it gives me the correct position, but if I let the other object "drag" the flare around, it doesn't, although the coordinates for the two objects are reported correctly (but again, it can be connected to the first). 4) How do I make an object follow another (like a missile) ? What I'm doing is SCPoint3d rotAxis; float x,y,z,norm; float dx,dy,dz; float sAng,cAng,ang; float ox=GetOrientX(),oy=GetOrientY(),oz=GetOrientZ(); // 1. Calc orientation to axis x=target->GetXPos()-GetXPos(); y=target->GetYPos()-GetYPos(); z=target->GetZPos()-GetZPos(); // 2. Get intended orientation projected on the orientation vector dx=x*ox; dy=y*oy; dz=z*oz; // 3. Get the sine of the angle sAng=sqrtf(Sqr(x-dx)+Sqr(y-dy)+Sqr(z-dz)); norm=sqrtf(Sqr(x)+Sqr(y)+Sqr(z)); sAng=sAng/norm; // 4. Normalize vector x=x/norm; y=y/norm; z=z/norm; // 5. Get rotation axis rotAxis.CrossProduct(ox,oy,oz,x,y,z); rotAxis.Normalize(); // 6. Get the cosine of the angle cAng=DotProduct(x,y,z,ox,oy,oz); // 7. Get the angle ang=GetAngleWithSinAndCos(sAng,cAng); if (ang>MISSILE_TURN_SPEED) ang=MISSILE_TURN_SPEED; else if (ang<-MISSILE_TURN_SPEED) ang=-MISSILE_TURN_SPEED; // 5. Rotate missile RotateAroundAxis(ang,rotAxis.x,rotAxis.y,rotAxis.z); Again, this can be connected to the first two problems, since it relies heavily on the position of the objects and the rotation around an arbitrary axis. Basically, what I'm doing is, given the current orientation (normalized) (ox,oy,oz) and the intended orientation ((x,y,z) in the above example), I calculate the sine (using standart trigonometry, that's the reason for 2. in the above example) and the cosine (using the dotproduct of the normalized intended orientation and the current orientation). Then, I use the crossproduct between those two angles and normalize it to obtain the rotation axis. Then I calculate the angle using the sine and the cosine of it, and finally, I limit the angle (to keep the missile to turn too fast). Then I rotate it... On paper, this works fine (as far as I can visualize it), but when I use the above code, the missile only hits the target when our ship and the target are on the Y=0 plane. The track code is the most important one, since I will use it to drive the AI of the enemies (they'll try to track our ship, all guns blazing, nothing too fancy, the teacher it's more interested in the visual aspect of the game). Thanks in advance Diogo de Andrade Dio...@mi... |