2013年9月2日星期一(8-4,平移和旋转)

本文介绍了一种通过平移和旋转实现二维多边形变换的方法,并提供了具体的C语言实现代码。文章展示了如何定义多边形的数据结构,以及如何通过平移和旋转函数更新多边形的位置和方向。

首先,坐标从整型换为浮点型,

typedef struct VERTEX2DF_TYP

        {

        int x,y; // the vertex

        } VERTEX2DI, *VERTEX2DF_PTR;

 

// a 2D polygon

typedef struct POLYGON2D_TYP

        {

        int state;        // state of polygon

        int num_verts;    // number of vertices

        int x0,y0;        // position of center of polygon 

        int xv,yv;        // initial velocity

        DWORD color;      // could be index or PALETTENTRY

        VERTEX2DF *vlist; // pointer to vertex list

 

        } POLYGON2D, *POLYGON2D_PTR;

 

平移

int Translate_Polygon2D( POLYGON2D_PTR poly, int dx, int dy )

{

     if( !poly )

         return;

 

     poly->x0           += dx;

     poly->y0           += dy;

     return ( 1 );

}

 

int Rotate_Polygon2D(POLYGON2D_PTR poly, int theta)

{

// this function rotates the local coordinates of the polygon

 

// test for valid pointer

if (!poly)

   return(0);

 

// loop and rotate each point, very crude, no lookup!!!

for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)

    {

 

    // perform rotation

    float xr = (float)poly->vlist[curr_vert].x*cos_look[theta] -

                    (float)poly->vlist[curr_vert].y*sin_look[theta];

 

    float yr = (float)poly->vlist[curr_vert].x*sin_look[theta] +

                    (float)poly->vlist[curr_vert].y*cos_look[theta];

 

    // store result back

    poly->vlist[curr_vert].x = xr;

    poly->vlist[curr_vert].y = yr;

 

    } // end for curr_vert

 

// return success

return(1);

 

} // end Rotate_Polygon2D

在game_init()中,加上角度正弦余弦数值

先各自数组

float                       cos_look[360];

float                       sin_look[360];

     for( int ang = 0; ang < 360; ang ++ )

     {

         float theta                      = ( float ) ang * PI / ( float ) 180;

         cos_look[ang]               = cos( theta );

         sin_look[ang]               = sin( theta );

     }

Game_main()中

     Translate_Polygon2D( & asteroids[curr_index], asteroids[curr_index].xv, asteroids[curr_index].yv );

         Rotate_Polygon2D( & asteroids[cur_index], 5 );

结果如下图所示

现在封装,把那两个函数封装为成员函数即可。

现在看看t3dlib中,没什么好改的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值