顶点阵列平铺贴图碰撞

时间:2015-07-03 19:25:53

标签: c++ collision-detection sfml

我正在使用一个使用16x16像素图块的小图块。玩家在1个像素空间中移动,这意味着玩家在多个图块中存在某些点。我当前的方法找到一个单独的图块,但它相当不准确,你不能进入你应该无法通过的图块的16px。

碰撞功能:

void collision(direction dir)
{
    m_Position = m_Sprite.getPosition();
    long x = m_Position.x;
    long y = m_Position.y;

    long current = ((x - (x % TextureSize)) / TextureSize) + (((((y - (y % TextureSize))) / TextureSize) * LevelWidth));


    switch (dir)
    {
    case up:
        if (activelevel[current - LevelWidth] != 0)
            this->move(down);
        break;
    case down:
        if (activelevel[current + LevelWidth] != 0)
            this->move(up);
        break;
    case left:
        if (activelevel[current + 1 ] != 0)
            this->move(right);
        break;
    case right:
        if (activelevel[current - 1] != 0)
            this->move(left);
        break;
    }

}

运动功能:

void move(direction dir)
{


    switch (dir)
    {
    case up:
        m_Sprite.move(0,-1 * speed);
        break;
    case right:
        m_Sprite.move(1 * speed, 0);
        break;
    case down:
        m_Sprite.move(0, 1 * speed);
        break;  
    case left:
        m_Sprite.move(-1 * speed, 0);
        break;  
    default:
        break;  
    }


    if (animated == true && dir != not)
        animate(dir);

    collision(dir);

}

有没有人对方法有任何想法?他们不需要与上述相关。

感谢。

0 个答案:

没有答案