Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
}
share|improve this question
    
Just something to note: You are passing by value into spriteCollide(). This will do unnecessary copying. You probably don't want that. –  Dean Knight Jun 28 '13 at 17:40
    
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? –  Byte56 Jun 28 '13 at 18:52
    
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 –  Xeon Jun 29 '13 at 4:47

1 Answer 1

up vote 2 down vote accepted

playerDest.Y = boxDest.Y - boxDest.height

should be

playerDest.Y = boxDest.Y - playerDest.height

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.