Programming 3D shape traversal

Discussion in 'Technical Discussion' started by Hodgy, Oct 20, 2013.

  1. Hodgy

    Hodgy

    Member
    797
    0
    16
    UK
    Games programming :)
    Hey. Im starting up a game project soon and have been mulling over ideas for making a twin stick shooter similar to super stardust HD and Nano Assult Neo. Only im trying to brainstorm ideas on how the terrain traversal in Nano Assult works, where your ship traverses around a non regular shape (See here)

    Now I believe I can understand how super stardust HD does it as you only ever traverse a perfect sphere, it would be a simple case of rotating all the objects on the x,y,z axis around the origin of the world.

    But the only way I can think of traversing an irregular shape like in nano assult neo would be to use a lower poly collision mesh and have all objects raycast below themselves till they hit the collision mesh, then get the 3 vertex normals of the polygon that the raycast hits and calculate a weighted average normal depending on where the raycast hits the polygon and the position of the object will be set along that vector. But this sounds like a horribly slow and brute-force approach as I could potentially have say 10 objects (one player 9 enemies) then say each thing has fired 10 bullets each) so now I have 100 objects that need this raycast for postitioning and say my collision mesh is 500 polys, thats potentially 50k raycast collision checks. unless im going about my raycasting incorrectly.

    Just wandering if any techies here have other solutions other than my raycasting?
     
  2. winterhell

    winterhell

    Member
    1,165
    7
    18
    50K ray-triangle checks is not much and works fine on Xbox 360 which is very outdated.
    As a rule of the thumb make something playable and if you have slowdowns you can profile. You probably don't want to optimize something that takes 0.1% cpu usage.
    As for optimizations, you can try quad trees for the mesh, but keep in mind that in some cases it can backfire due to cache misses.
     
  3. Hodgy

    Hodgy

    Member
    797
    0
    16
    UK
    Games programming :)
    cool. just seemed like a lot to me, but if it isnt then there isnt a problem. Thanks for the advice.
     
  4. Hodgy

    Hodgy

    Member
    797
    0
    16
    UK
    Games programming :)
    I'm back to quiz / annoy you guys!


    So I got the raycasting working, using the results of the raycast to rotate and place my player on the surface of a 3d object (sphere for now) but my method seems to have some errors here is a quick screenshot representing the unwanted behavior:

    [​IMG]

    When I press the right key on the keyboard I want the ship to go in the direction of the green arrow, however it goes in the direction of the red arrow instead, also due to this, when I reach the top or bottom of the sphere the ship rotates on the spot.
    here is a bit of code I am using to set up my matrix to rotate my ship and forward vector to:

    Code (Text):
    1.  
    2.     RaycastResult results;
    3.     if(cGame::GetGameWorld()->GetPhysicsWorld()->castRay(m_vPos,Vector3(0,0,0),results))
    4.     {
    5.         //snap to point
    6.         m_vPos = results.m_contactPoint.getXYZ() + (results.m_contactNormal.getXYZ()*2.f);
    7.  
    8.         Vector3 normal(results.m_contactNormal.getXYZ());
    9.         Vector3 upVector(0.f,1.f,0.f);
    10.         Vector3 sideVector;
    11.         sideVector = cross(upVector,-normal);
    12.         upVector = cross(sideVector,-normal);
    13.         sideVector = normalize(sideVector);
    14.         upVector = normalize(upVector);
    15.  
    16.         Vector4 col0( sideVector.getX() ,sideVector.getY()  ,sideVector.getZ()  ,0.f);
    17.         Vector4 col1( -upVector.getX()  ,-upVector.getY()   ,-upVector.getZ()   ,0.f);
    18.         Vector4 col2( -normal.getX()    ,-normal.getY()     ,-normal.getZ()     ,0.f );
    19.         Vector4 col3( 0.f               , 0.f               , 0.f               ,1.f );
    20.         m_pNormalMatrix = Matrix4(col0,col1,col2,col3);
    21.  
    22.         m_pObjectMatrix = Matrix4::translation(m_vPos) * m_pNormalMatrix * Matrix4::rotationZ(m_fStickAngle + HALF_PI ) *  Matrix4::rotationX(-HALF_PI);
    23.     }
    24.  
    my forward vector which is made up for the xy directions of a xbox 360 controller ( or arrow keys on a keyboard) is then rotated by "m_pNormalMatrix " and I add the resulting vector onto m_vPos to move the player to its next position before performing the raycast and matrix calculation again.
    m_fStickAngle is the angle made up my the vector of the analogue stick this just rotates my model to face the direction its moving and the rotation in the x axis is to make the bottom of the ship face the surface of the sphere;

    I'm pretty sure this matrix isnt the matrix I want to transform my forward vector by, but I'm stumped for solutions, so any suggestion is welcome.
     
  5. Aerosol

    Aerosol

    Not here. Moderator
    10,952
    306
    63
    Not where I want to be.
    Sonic (?): Coming summer of 2055...?
    Gah. I know what your issue is but I can't articulate it. I ran into something similar a little while ago.
     
  6. Hodgy

    Hodgy

    Member
    797
    0
    16
    UK
    Games programming :)
    The only thing I can describe it as is gimbal lock, but I can't for the life of me think of a solution that works for this particular application.
     
  7. winterhell

    winterhell

    Member
    1,165
    7
    18
    One popular solution for the gimbal lock is using Quaternions for the rotations of the object.

    First result from Google: http://www.cprogramming.com/tutorial/3d/quaternions.html