在碰撞检测方面需要帮助 [英] Need help in collision detection

查看:71
本文介绍了在碰撞检测方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个迷宫.它的墙壁是棕色,地板是白色.播放器是一个由箭头键控制的矩形框.在按箭头键时,我要首先检查我的对象将占据的区域是否为地板,如果它是地板,则允许其移动,否则不允许移动.
我通过
检查 1.使用glReadPixels读取下一个占用区域的像素
2.将读取的像素与白色进行比较,如果在对象方向上为白色,则为白色,否则为

问题是当我按左键时,即使物体在地板上,物体也不会移动

我的一些全局变量

i build a maze. it has walls of brown colour and floor is of white colour. player is a rectangular box controlled by arrows keys. on pressing arrow keys i want to first check tht the area my object will occupy is a floor or not, if it is a floor i allow it to move otherwise not.
i check this by
1. reading pixels of the next occupied area using glReadPixels
2. compare the readed pixels with white color, if it is white increment in the object direction otherwise not

the problem is this when i press left key, the object does not move even if it is on the floor

some of my global variables

float objectx, objecty, objectSize = 20;
RGBApixmap block(10,20);
bool isFloorColor = false;


objectx和objecty在main
中的generateMaze()函数中初始化
这是我的目标代码


objectx and objecty is initialzed in generateMaze()function which is in main

this is my object code

void Object(float objX, float objY)
{
    glColor3f(64/255.0,0,64/255.0);
    glRectf(objX,objY,objX+objectSize,objY+objectSize);
}



箭头键处理程序(我只提到了左箭头键)



arrow keys handler(i just mention left arrow key)

void mySpecialKeyFn(int key, int mx, int my)
{
    switch(key)
    {

        case GLUT_KEY_LEFT:
            glReadPixels(objectx-10, objecty, block.nCols, block.nRows, GL_RGBA, GL_UNSIGNED_BYTE,block.getPixmap());
            isFloorColor = block.checkfloor();
            if(objectx-10>95 && isFloorColor==true)
                objectx-=10;
            glutPostRedisplay();
            break;
}







void display(void)
{
....
           Object(objectx,objecty);
           glFlush();
}



头文件RGBA.h中的函数



function in headerfile RGBA.h

bool checkfloor()
{
           int count = 0;
           for(int i =0 ; i<10; i++)
           {
               if(this->pixel[i].r ==(uchar)255.0 && this->pixel[i].g ==(uchar)255.0 && this->pixel[i].b ==(uchar)255.0)
                   count++;
           }
           if(count>=5)
               return true;
           else
               return false;
}

推荐答案

更改
if(this->pixel[i].r ==(uchar)255.0 && this->pixel[i].g ==(uchar)255.0 && this->pixel[i].b ==(uchar)255.0)




to

if(this->pixel[i].r ==255 && this->pixel[i].g ==255 && this->pixel[i].b ==255)


您的错误:
您对不兼容类型(在这种情况下为float)的常量使用了C风格的类型转换.请注意,强制类型转换不会转换二进制表示形式,它只是重新解释它-在这种情况下会产生垃圾.

遵循的最佳做法:
1. 从不在C ++程序中不使用C样式类型转换.在大多数情况下,强制转换只是使用错误类型的标志-相应地更改变量类型!

2.即使确实需要强制转换,也可以使用C ++强制转换,例如dynamic_castreinterpret_cast.并正确使用它们:请参见 http://www.cplusplus.com/doc/tutorial/typecasting/ [ ^ ]

此外,检查您的输出并将其解释回模型中效率低下且容易出错,而您只需检查用于生成该输出的模型即可.


Your mistake:
You used a C-style typecast on constants of an incompatible type (float, in this case). Note that a cast does not convert the binary representation, it just reinterprets it - which in this case produces garbage.

Best practices to follow:
1. NEVER use C-style type casts in a C++ program. Most of the time a cast is just a sign of using the wrong types - change your variable types accordingly!

2. Even if you do really need a cast, use the C++ casts such as dynamic_cast or reinterpret_cast. And use them appropriately: see http://www.cplusplus.com/doc/tutorial/typecasting/[^]

Besides, it is inefficient and error prone to check your output and interpret it back into the model, when instead you could just check the model you used to produce that output.


这篇关于在碰撞检测方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆