Hi I'm having trouble resolving "Collision detection between axis-aligned bounding box and bitmap (pixel perfect) terrain on a Worms type game". (Read previous question to find more information about the game).
The player has got properties such as velocity, acceleration, gravity applied to it on its update method. I have controls that allow the player to "land" on a surface by detecting the pixel immediately below it and then stopping the velocity if that object is solid. I am having trouble with moving the character.
Currently when I move the character left or right the code should work in my head to move the character up, but it seams to still want to go into the ground. Could you help me to resolve the collisions for moving?
Below is the code I implement for Walking left or right. It is called immediately before the update method for sprite. This method is called from the Player class update method.
boolean solidPixel;
//Create a bitmask from top to bottom of the pixels
for(int i=0;i<boundHeight;i++){
//If true then collision
solidPixel = TerrainObj.isPixelSolid(x+(getBound().halfWidth*direction),y-getBound().halfHeight+i);
//if any of the pixels are solid then we've hit a wall
//so act accordingly and exit the application
if(solidPixel && i < getBound().halfHeight/4){
//
//Cannot move in the direction
velocity.x = 0;
break;
}
//if the bottom (left/right)quater of the sprite is
//solid then its a slope
if(solidPixel && i >= getBound().halfHeight/4){
//Position the item -(up) by the amount of pixels the
//gradient is
position.y+=(getBound().halfHeight*2)-i;
break;
}
}
Any help is much appreciated how I could approach the issue to stop the player ever moving into the ground to provide prefect collision detection.
Sorry for the long post, wanted to put as much information in as possible.