使用 dda 算法调试光线投射 地图

问题描述

我正在使用爸爸算法编写重铸游戏。我花了几个小时,但我无法调试显然返回错误值的代码。有时它会错过碰撞,或者为十几个不同的值返回相同的输出。如果有人可以查看我的代码并帮助我修复它,我会很高兴。

重铸代码

i - 光线索引(从 0 到 screen_width )

override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
        super.touchesEnded(touches,with:event)
        var delta = CGPoint(x: 0,y: 0)
        guard touches.first != nil else { return }
        if let touch = touches.first,let node = myGV.currentGem,node.isMoving == true {
            let touchLocation = touch.location(in: myGV.guessBar!)
            let touchLocation2 = touch.location(in: self)

地图

数字代表颜色(0为空格)

void Raycast::ray( int i,Player &player ) {
    double x = player.position.x,y = player.position.y;

    double theta = ( (double)player.angle - 100.0f * (double)i / (double)SCREEN_WIDTH ) * DEG2RAD;
    double sinus = sin(theta),cosinus = cos(theta);

    double deltaX = 1 / sinus,deltaY = 1 / cosinus ;
    if( sinus == 0 ) deltaX = 0; if( cosinus == 0 ) deltaY = 0;
    

    double distX,distY;
    float stepX,stepY;

    if( sinus > 0 ) {
        stepX = 1;
        distX = ( (double)floor(y) - y + 1.0f ) * deltaX;
    } else {
        stepX = -1;
        distX = ( (double)floor(y) - y ) * deltaX;
    }

    if( cosinus > 0 ) {
        stepY = 1;
        distY = ( (double)floor(x) - x + 1.0f ) * deltaY;
    } else {
        stepY = -1;
        distY = ( (double)floor(x) - x ) * deltaY;
    }

    bool hit = true;
    while( hit ) {
        if( distX < distY ) {
            distX += deltaX;
            y += stepX;
        } else {
            distY += deltaY;
            x += stepY;
        }
        if( mapa[(int)x][(int)y] > 0 ) hit = 0;
    }

    double dist = sqrt( pow(player.position.x-x,2) + pow(player.position.y-y,2) );
    double len = SCREEN_HEIGHT / (2 * dist  );
    drawSegment( i,len,mapa[(int)y][(int)x] );
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...