Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualWerner291

Posted 07 July 2013 - 06:53 AM

Good afternoon,
 
I'm currently developing a game for Android using OpenGL ES 2.0 where users fly through a tunnel, the goal being not to hit the walls.
 
The tunnel is randomly generated and it is composed of a series of interconnected rings. The rings are supposed to be perpendicular to the direction of the tunnel. See this screenshot when things go well:
 
 
Spoiler


Things still look good in this screenshot, but as the tunnel drifts away from the Z axis, the rings do not rotate with it correctly, causing this nice round tunnel to gradually flatten out then turn into an unmanageable mess of lines.
 
Currently, here's the code that I use to generate the rings: (Java)
// Constructor
public TunnelSection(GameManager myRenderer, TunnelSection previous) {
        
        sectionTransform = new float[16];
        
        float ring_radius =  RING_MIN_RADIUS + (float)(Math.random()) * (RING_MAX_RADIUS - RING_MIN_RADIUS);
        
        if (previous != null){
            mDirection = previous.mDirection;
            
            Quaternion randomRot = new Quaternion();
            randomRot.fromAngles(((float)Math.random()-0.5f)/5f, ((float)Math.random()-0.5f)/5f, ((float)Math.random()-0.5f)/5f);
            mDirection = mDirection.mult(randomRot);
            mDirection.normalizeLocal();
            
            Vector3f delta = mDirection.mult(Vector3f.UNIT_Z);
            
            mPosition = new float[]{previous.mPosition[0]+delta.x, previous.mPosition[0]+delta.y, previous.mPosition[2]+delta.z};
        }
        else {
            mDirection = Quaternion.DIRECTION_Z;
            mPosition = new float[]{0,0,0};
        }
        
        ringCoords = new float[(COORDS_PER_VERTEX) * RING_VERTICE_COUNT];
        
        Vector3f vertLocal = Vector3f.UNIT_X;
        
        for (int itr=0;itr<RING_VERTICE_COUNT;itr++){
            vertLocal.set(android.util.FloatMath.sin((float)Math.PI * 2f * ((float)itr/(float)RING_VERTICE_COUNT )) * ring_radius,
                          android.util.FloatMath.cos((float)Math.PI * 2f * ((float)itr/(float)RING_VERTICE_COUNT )) * ring_radius,
                          0f);
            
            vertLocal = mDirection.mult(vertLocal);
            
            ringCoords[itr*3]   = vertLocal.x;
            ringCoords[itr*3+1] = vertLocal.y;
            ringCoords[itr*3+2] = vertLocal.z;
        }
// Initialize the rest...
Why is it that the rings do not rotate like they should? It must be something very simple, but I can't find it...

#1Werner291

Posted 07 July 2013 - 06:51 AM

Good afternoon,
 
I'm currently developing a game for Android using OpenGL ES 2.0 where users fly through a tunnel, the goal being not to hit the walls.
 
The tunnel is randomly generated and it is composed of a series of interconnected rings. The rings are supposed to be perpendicular to the direction of the tunnel. See this screenshot when things go well:
 
 

Spoiler


Things still look good in this screenshot, but as the tunnel drifts away from the Z axis, the rings do not rotate with it correctly, causing this nice round tunnel to gradually flatten out then turn into an unmanageable mess of lines.
 
Currently, here's the code that I use to generate the rings: (Java)

// Constructor
public TunnelSection(GameManager myRenderer, TunnelSection previous) {
        
        sectionTransform = new float[16];
        
        float ring_radius =  RING_MIN_RADIUS + (float)(Math.random()) * (RING_MAX_RADIUS - RING_MIN_RADIUS);
        
        if (previous != null){
            mDirection = previous.mDirection;
            
            Quaternion randomRot = new Quaternion();
            randomRot.fromAngles(((float)Math.random()-0.5f)/5f, ((float)Math.random()-0.5f)/5f, ((float)Math.random()-0.5f)/5f);
            mDirection = mDirection.mult(randomRot);
            mDirection.normalizeLocal();
            
            Vector3f delta = mDirection.mult(Vector3f.UNIT_Z);
            
            mPosition = new float[]{previous.mPosition[0]+delta.x, previous.mPosition[0]+delta.y, previous.mPosition[2]+delta.z};
        }
        else {
            mDirection = Quaternion.DIRECTION_Z;
            mPosition = new float[]{0,0,0};
        }
        
        ringCoords = new float[(COORDS_PER_VERTEX) * RING_VERTICE_COUNT];
        
        Vector3f vertLocal = Vector3f.UNIT_X;
        
        for (int itr=0;itr<RING_VERTICE_COUNT;itr++){
            vertLocal.set(android.util.FloatMath.sin((float)Math.PI * 2f * ((float)itr/(float)RING_VERTICE_COUNT )) * ring_radius,
                          android.util.FloatMath.cos((float)Math.PI * 2f * ((float)itr/(float)RING_VERTICE_COUNT )) * ring_radius,
                          0f);
            
            vertLocal = mDirection.mult(vertLocal);
            
            ringCoords[itr*3]   = vertLocal.x;
            ringCoords[itr*3+1] = vertLocal.y;
            ringCoords[itr*3+2] = vertLocal.z;
        }
// Initialize the rest...
Why is it that the rings do not rotate like they should? It must be something very simple, but I can't find it...

PARTNERS