I'm tring to make 3D rotation of a poligon, I wrote my own functions for that.The problem is when I rotate some points in 2D it works but if I use the same functions for 3D , while rotating the veteces move to the center of the object and remain there.
I also wrote the 3D rendering part.

The relevant code is this:

  • poligon vertex coordonates are stored in "object" array:

              object[1][0][4] = 9;   // number of vertecies
    
              object[1][1][0] = 0;  // x of origin
              object[1][1][1] = 0;  // y of origin
              object[1][1][2] = 150;  // z of origin
    
              object[1][2][0] = 100; // x of first vertex
              object[1][2][1] = 100; // y of first vertex
              object[1][2][2] = 100; // z of first vertex
    
             ...
    
  • 2D rotate functions:

    int rotateX(int Ox, int Oy , int Ox2, int Oy2 , float Ub ){
    float PI = 3.1415926535897;

    float hkj = round( sqrt( (abs(Ox - Ox2 ) * abs(Ox - Ox2)) + (abs(Oy - Oy2) * abs(Oy - Oy2)) ) );
    
    if( Ub > 359 ){ Ub = 0; } 
    if( Ub < 0 ){ Ub = 359; }  
    
    if( ( Ub >=0 )  and ( Ub < 90 ) ){     
        Ox2 = ( sin((Ub*PI)/180) * hkj );
    }else if( ( Ub >=90 )  and ( Ub < 180 ) ){     
        Ox2 = (  cos(((Ub-90)*PI)/180) * hkj );  
    }else if( ( Ub >=180 )  and ( Ub < 270 ) ){     
        Ox2 = ( (sin(((Ub-180)*PI)/180) * hkj )* -1);
    }else if( ( Ub >=270 )  and ( Ub < 360 ) ){     
        Ox2 = ( (cos(((Ub-270)*PI)/180) * hkj )* -1 );
    }  
    

    return Ox2 ;
    }

    int rotateY(int Ox, int Oy , int Ox2, int Oy2 , float Ub ){
    float PI = 3.1415926535897932;

    float hkj = round( sqrt( (abs(Ox - Ox2) * abs(Ox - Ox2)) + (abs(Oy -Oy2) * abs(Oy - Oy2)) ));
    
    if( Ub > 359 ){ Ub = 0; } 
    if( Ub < 0 ){ Ub = 359; }  
    
    if( ( Ub >=0 )  and ( Ub < 90 ) ){  
        Oy2 = cos((Ub*PI)/180) * hkj ;
    }else if( ( Ub >=90 )  and ( Ub < 180 ) ){   
        Oy2 = (sin(((Ub-90)*PI)/180) * hkj)* -1;   
    }else if( ( Ub >=180 )  and ( Ub < 270 ) ){     
        Oy2 = (cos(((Ub-180)*PI)/180) * hkj )* -1;
    }else if( ( Ub >=270 )  and ( Ub < 360 ) ){     
        Oy2 = sin(((Ub-270)*PI)/180) * hkj ;
    }  
    
    return  Oy2 ; 
    

    }

  • do the rotation in 3D:

    if( Ub2 > 359 ){ Ub2 = 0; }  // Ub2 is the number of degrees
    if( Ub2 < 0 ){ Ub2 = 359; }  
    

    // if key A is pressed
    if (KB_A){
    Ub2 += 5;
    // put new x and y values in object array
    object[1][2][0] = rotateX(object[1][1][0],object[1][1][1],object[1][2][0],object[1][2][1],100) + object[1][1][0] ;
    object[1][2][1] = rotateY(object[1][1][0],object[1][1][1],object[1][2][0],object[1][2][1],100) + object[1][1][1] ;

    }
    if (KB_Z){

    Ub2 -= 5;
    object[1][2][0] = rotateX(object[1][1][0],object[1][1][1],object[1][2][0],object[1][2][1],20) + object[1][1][0] ;
    object[1][2][1] = rotateY(object[1][1][0],object[1][1][1],object[1][2][0],object[1][2][1],20) + object[1][1][1] ;

    }

The rotation is made around the objects origin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.