# [SOLVED]Mario style physics and collision?

This topic is 3744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to make a box collision system similar to the ones used by the Nes Mario Bros. Basically, I want my character to stop moving towards a block when it collides with it. Also, my character can be pushed around by moving blocks. Currently, my program has no problem handling stationary blocks. However, once I put in moving blocks, things start getting a bit weird. For example, if my character tries to jump up towards the bottom of a block moving downwards, he will sometimes get stuck inside the block. Also if the character jumps towards the left of the block while the block is moving right, he will instantly move the the right side of the block. Here my code in C++. It's extremely messy and I've tried my best to make everything as readable as possible: Note: the function move is called every time the program loops
//Returns true if collision happened between the two boxes
int collisionCheck(double x, double y, double sizex, double sizey,
double X, double Y, double sizeX, double sizeY)
{
if(((x+sizex)>X)&&(x<(X+sizeX))&&((y+sizey)>Y)&&(y<(Y+sizeY)))
return 1;
else
return 0;
}

void move(double x, double y)
{
//If collide is true, Man won't move.
bool collide = false;

//Checks whether Man is out of bound
if((Man.posX+x) < 0 || (Man.posX+Man.sizeX+x) > SCREEN_WIDTH || (Man.posY+Man.sizeY+y) > SCREEN_HEIGHT){
collide = true;
}
else
//Checks whether Man is standing on the ground.
if((Man.posY+y) < 0){
collide = true;
Ground = true;
}
else
//Checks collision for all walls
if(WalMAP.begin() != WalMAP.end()){
for(vector<Object>::iterator it = WalMAP.begin(); it < WalMAP.end(); it++){
//Checks if Man collides with wall in the x direction
if(collisionCheck(Man.posX + x, Man.posY, Man.sizeX, Man.sizeY,
(*it).posX-(*it).speedX, (*it).posY-(*it).speedY, (*it).sizeX, (*it).sizeY) == 1)
{
collide = true;
//If Man's speed in x direction is positive, then it is assumed he collides from the left
if(x>0){
//Moves Man to the left side of block
Man.posX += ((*it).posX - (Man.posX+Man.sizeX));
//Stops Man's speed in x direction
Man.speedX = 0;
}
else
//If Man's speed in x direction is positive, then it is assumed he collides from the right
if(x<0){
//Moves Man to the right side of block
Man.posX += (((*it).posX+(*it).sizeX) - Man.posX);
//Stops Man's speed in x direction
Man.speedX = 0;
}
else
//If Man's speed is 0 in x direction, while wall's speed is negative, it is assumed that
//the block is pushing the Man from the right.
if((*it).speedX<0){
//Moves Man to the left side of block
Man.posX += ((*it).posX - (Man.posX+Man.sizeX));
//Stops Man's speed in x direction
Man.speedX = 0;
}
else
if((*it).speedX>0){
//Moves Man to the right side of block
Man.posX += (((*it).posX+(*it).sizeX) - Man.posX);
//Stops Man's speed in x direction
Man.speedX = 0;
}

}
//Checks if Man collides with wall in the y direction
if(collisionCheck(Man.posX, Man.posY + y, Man.sizeX, Man.sizeY,
(*it).posX-(*it).speedX, (*it).posY-(*it).speedY, (*it).sizeX, (*it).sizeY) == 1)
{
collide = true;
//If Man's speed in y direction is positive, then it is assumed he collides from the bottom
if(y>0){
//Moves Man to the bottom of the block
Man.posY += ((*it).posY - (Man.posY+Man.sizeY));
//Stops Man's speed in y direction
Man.speedY = 0;
}
else
//If Man's speed in y direction is negative, then it is assumed he collides from the top
if(y<0){
//Moves Man to the top of the block
Man.posY += (((*it).posY+(*it).sizeY) - Man.posY);
//Stops Man's speed in y direction
Man.speedY = 0;
//Set Man's speed to the speed of the moving block.
Man.speedX = (*it).speedX;
Ground = true;
}
}
}
}

//If no collisions occured, then Man moves in given direction
if( collide == false ){
Man.posX = (Man.posX+x);
Man.posY = (Man.posY+y);
}

}


Any help will be greatly appreciated! [Edited by - CustomDrive on July 23, 2008 10:12:29 AM]

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632994
• Total Posts
3009769
• ### Who's Online (See full list)

There are no registered users currently online

×