Advertisement

Need help on some mathematics - donuts3D

Started by February 09, 2002 08:50 AM
7 comments, last by LonelyTower 23 years ago
Hi all, I have been looking through the donut3D sourcecode in hope of learning how to program a decent DirectX game and run into a bottleneck trying to understand the maths. Here''s the code snippet:
  
 // Point of ship, on terrain

            D3DXVECTOR3 vShipPt;
            vShipPt.x =  m_pShip->vPos.x;
            vShipPt.z = -m_pShip->vPos.y;
            vShipPt.y = 0.1f + HeightField( vShipPt.x, vShipPt.z );

            // Point ahead of ship, on terrain

            D3DXVECTOR3 vForwardPt;
            vForwardPt.x = vShipPt.x+sinf(m_pShip->fAngle);
            vForwardPt.z = vShipPt.z+cosf(m_pShip->fAngle);
            vForwardPt.y = 0.1f + HeightField( vForwardPt.x, vForwardPt.z );

            // Point to side of ship, on terrain

            D3DXVECTOR3 vSidePt;
            vSidePt.x = vShipPt.x+sinf(m_pShip->fAngle + D3DX_PI/2.0f);
            vSidePt.z = vShipPt.z+cosf(m_pShip->fAngle + D3DX_PI/2.0f);
            vSidePt.y = 0.1f + HeightField( vSidePt.x, vSidePt.z );

            // Compute vectors of the ship''s orientation

            D3DXVECTOR3 vForwardDir = vForwardPt - vShipPt;
            D3DXVECTOR3 vSideDir    = vSidePt - vShipPt;
            D3DXVECTOR3 vNormalDir;
            D3DXVec3Cross( &vNormalDir, &vForwardDir, &vSideDir );

            // Construct matrix to orient ship

            D3DXMATRIX matWorld, matLookAt, matRotateZ;
            D3DXMatrixRotationZ( &matRotateZ, m_pShip->fRoll );
            D3DXMatrixLookAtLH( &matLookAt, &vShipPt, &(vShipPt-vForwardDir), &vNormalDir );
            D3DXMatrixInverse( &matLookAt, NULL, &matLookAt );
            D3DXMatrixIdentity( &matWorld );
            D3DXMatrixMultiply( &matWorld, &matWorld, &matRotateZ );
            D3DXMatrixMultiply( &matWorld, &matWorld, &matLookAt );
  
Basically, m_pShip points to a spaceship object which stores the ship''s speed, x, y and z co-ordinates. The GetHeight() function basically just return how high the ship will be at that point - the function is below:
  
// Simple function to define "hilliness" for terrain

inline FLOAT HeightField( FLOAT x, FLOAT z )
{
    return (cosf(x/2.0f+0.2f)*cosf(z/1.5f-0.2f)+1.0f) - 2.0f;
}
  
The question is - how does the code calculate the "point to side of ship, on terrian" - and what does that means. What is the significane of the sine and cosine. What is it meant by "point ahead of ship"? Basically, I just want to know how this work. Thanks in advance!
"Magic makes the world go round." - Erasmus, Quest For Glory I
I''m not sure but the "point to side of ship, on terrian" probably mean that it found the position of a point (of the ship) next to the center point of the ship so they can change the angle of the ship to have the angle of the terrain. If they use sine and cosine it''s because they generated the field with an algorithm (simple) using sine and cosine and they use it too to find a point on the terrain.
François DagenaisDagenais.f@videotron.ca
Advertisement
Maybe I shall be a bit more specific. I can understand the first part of the code:

      D3DXVECTOR3 vShipPt;     vShipPt.x =  m_pShip->vPos.x;       vShipPt.z = -m_pShip->vPos.y;      vShipPt.y = 0.1f + HeightField( vShipPt.x, vShipPt.z );  



Basically, vShipPt is the vector that defines the position of the ship (wait a second - it is a point, or is it a vector with direction and maginitude?)

Next snippet:


        // Point ahead of ship, on terrain    D3DXVECTOR3 vForwardPt;      vForwardPt.x = vShipPt.x+sinf(m_pShip->fAngle);  vForwardPt.z = vShipPt.z+cosf(m_pShip->fAngle);       vForwardPt.y = 0.1f + HeightField( vForwardPt.x, ForwardPt.z );        


The comment says "Point ahead of ship, on terrian" tells me that maybe it is which direction the ship is pointing to. But then, is not finding the orienation of the ship means finding out which direction the ship is pointing at?

I am having troubles with vForwardPt.x = vShipPt.x + sinf(m_pShip->fAngle) and the line below, vForwardPt.z = vShipPt.z+cosf(m_pShip->fAngle). So basically, the question is:

When you take the x or z coordinate and add it to sine (or cosine, if you are taking the z coordinate), what do you get?

The 3rd piece of code:

  // Point to side of ship, on terrain          D3DXVECTOR3 vSidePt;vSidePt.x = vShipPt.x+sinf(m_pShip->fAngle + D3DX_PI/2.0f);           vSidePt.z = vShipPt.z+cosf(m_pShip->fAngle + D3DX_PI/2.0f);           vSidePt.y = 0.1f + HeightField( vSidePt.x, vSidePt.z );            D3DXVECTOR3 vNormalDir;                  

I absolutely I have to clue about what this does. The comment says "Point to side of ship" -- what does that means?

Any tips or advices will be helpful. Thanks!



Edited by - LonelyTower on February 9, 2002 11:53:29 AM

Edited by - LonelyTower on February 9, 2002 11:54:31 AM
"Magic makes the world go round." - Erasmus, Quest For Glory I
I''m no 3d expert but it seems to me that FAngle is the bank/roll angle of the ship, so a point in front of the ship will be at x+sin(fangle), z+cos(fangle), height, taking into account that the ship will roll up/down and left/right around its centerpoint. Likewise the point to the side of the ship will always be at a right angle to the point in front of the ship thus:

x+sin(fangle +90 degrees), z+(fangle+90degrees), height


The height is calculated from the x, z points of the ship and a terain algorithm.

I''m guessing a bit and I hav''nt used direct3d but this might be worth looking into.

Regards, Jay

I''m no 3d expert but it seems to me that FAngle is the bank/roll angle of the ship, so a point in front of the ship will be at x+sin(fangle), z+cos(fangle), height, taking into account that the ship will roll up/down and left/right around its centerpoint. Likewise the point to the side of the ship will always be at a right angle to the point in front of the ship thus:

x+sin(fangle +90 degrees), z+(fangle+90degrees), height


The height is calculated from the x, z points of the ship and a terain algorithm.

I''m guessing a bit and I hav''nt used direct3d but this might be worth looking into.

Regards, Jay (passing visiter)

quote:
Original post by Jason Zelos
I''m no 3d expert but it seems to me that FAngle is the bank/roll angle of the ship, so a point in front of the ship will be at x+sin(fangle), z+cos(fangle), height, taking into account that the ship will roll up/down and left/right around its centerpoint. Likewise the point to the side of the ship will always be at a right angle to the point in front of the ship thus:

x+sin(fangle +90 degrees), z+(fangle+90degrees), height


The height is calculated from the x, z points of the ship and a terain algorithm.

I''m guessing a bit and I hav''nt used direct3d but this might be worth looking into.

Regards, Jay (passing visiter)




Hey, thanks. It does clear up something.

Now the one I have trouble imagining is how the orienation matrix is set finally. I am quite tempted to take out graph paper and go through the steps myself to see what it does...
"Magic makes the world go round." - Erasmus, Quest For Glory I
Advertisement
The world Matrix is the origin point, The RotateZ Matriz refers to the ship point up/down as it gos over the terrain and the LookAtLH is to do with the viewport or camera position. I think the LH refers to which coordinate (x,z,y or x,y,z) system your using, LeftHand or RightHand.

I''m trying to remember stuff I read somewhere, so I may have got it wrong. There should be some tutorials that go through all the basics avalible at the DirectX site ( or perhaps here) that might be more helpfull.

Good luck , Jay
If you''re at the center of a 2D unit sphere, and assume x+ is to the right,
and y+ is up, then the point to the right is cos(0), sin(0) or 1,0.
The point up is cos(90), sin(90) or 0,1. 90 degrees is also expressed
as PI/2, the same in your equation, D3DX_PI/2.0f.

So, that is vForwardPt and vSidePt is doing, on the x and z axes.
You are on the right path. Everything in that original code sample is used to orientate the the ship in respects to the definition of a 4X4 matrix. This is very important if you plan on going further with any kind of 3D game programming because every third party graphics library I have seen uses that type of matrix to align and render objects in 3D space.

I suggest you find some tutorials on Matrices. GameDev has quite a few.

As for this...
quote:

Basically, vShipPt is the vector that defines the position of the ship (wait a second - it is a point, or is it a vector with direction and maginitude?)


... I can tell you that a Vector is both a point and a "direction and magnitude". It all depends on what context you are looking at. eg;

Vector( 2, 3) could represent a point that is 2 units away from the origin along the x-axis and 3 unit from the origin along the y-axis.
        Y    5-|      |    3-|.* <- pt(2,3)      | .      | .    0-+----------X      0 2   5  

But it could also indicate a velocity...
        Y    3-|...* <- pt(2,3)      |  /.      |   .      | / .      |   .                // Sorry about the art      |/  .    0-+----------X      0   2  

There are tutorials that will explain all of this better. I suggest you look for those too.

Good Luck,
-m



Edited by - lefty2shoes on February 13, 2002 9:29:58 AM

This topic is closed to new replies.

Advertisement