0
\$\begingroup\$

After trying and trying, I still cannot understand why the leg of character exceeds the wall but no clipping issue when I hit the wall from below. How should I fix it to make him stand still on the wall?

http://i453.photobucket.com/albums/qq253/ulti-killer/13-5.png~original

From collideWithBox() function below, it shows that playerDest.Y = boxDest.Y - boxDest.height; will get the position the character should standstill on the wall. Theoretically, the clipping effect won't be happen as the character hit the box from below works with the equation playerDest.Y = boxDest.Y + boxDest.height;.

http://i453.photobucket.com/albums/qq253/ulti-killer/14.png~original

void collideWithBox()
{
    if ( spriteCollide(playerDest, boxDest) && keyArr[VK_UP])
        //playerDest.Y += 50;
        playerDest.Y = boxDest.Y + boxDest.height;
    else if ( spriteCollide(playerDest, boxDest) && !keyArr[VK_UP])
        playerDest.Y = boxDest.Y - boxDest.height;
}



void initPlayer()
{
    //  Create texture.
    hr = D3DXCreateTextureFromFileEx(d3dDevice, "player.png", 169, 44, 
        D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, 
        D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 
        NULL, NULL, &player);

    playerRect.left = playerRect.top = 0;
    playerRect.right = 29; 
    playerRect.bottom = 36;

    playerDest.X  = 0; 
    playerDest.Y  = 564;

    playerDest.length = playerRect.right - playerRect.left;
    playerDest.height = playerRect.bottom - playerRect.top;
}

void initBox()
{
    hr = D3DXCreateTextureFromFileEx(d3dDevice, "brock.png", 330, 132, 
        D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, 
        D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 
        NULL, NULL, &box);

    boxRect.left = 33;
    boxRect.top = 0;
    boxRect.right = 63; 
    boxRect.bottom = 30;

    boxDest.X = boxDest.Y = 300;

    boxDest.length = boxRect.right - boxRect.left;
    boxDest.height = boxRect.bottom - boxRect.top;
}

bool spriteCollide(Entity player, Entity target)
{
    float left1, left2;
    float right1, right2;
    float top1, top2;
    float bottom1, bottom2;

    left1 = player.X;
    left2 = target.X;
    right1 = player.X + player.length;
    right2 = target.X + target.length;

    top1 = player.Y;
    top2 = target.Y;

    bottom1 = player.Y + player.height;
    bottom2 = target.Y + target.height;

    if (bottom1 < top2) return false;
    if (top1 > bottom2) return false;

    if (right1 < left2) return false;
    if (left1 > right2) return false;

    return true;
}
\$\endgroup\$
3
  • \$\begingroup\$ Just something to note: You are passing by value into spriteCollide(). This will do unnecessary copying. You probably don't want that. \$\endgroup\$ Jun 28, 2013 at 17:40
  • \$\begingroup\$ Hey optimisez. This is pretty close to a code dump question. Can you further describe the problem (perhaps with images?) and the debugging steps you've already taken? \$\endgroup\$
    – House
    Jun 28, 2013 at 18:52
  • \$\begingroup\$ I think what he mean is, how to prevent the character from "going down" the floor below him. It's like, the floor's top position is Y 500, character's bottom position is Y 505 when he jumped and land on the floor. He wants it to be landing perfectly at Y 500 \$\endgroup\$
    – Xeon
    Jun 29, 2013 at 4:47

1 Answer 1

2
\$\begingroup\$

playerDest.Y = boxDest.Y - boxDest.height

should be

playerDest.Y = boxDest.Y - playerDest.height

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .