Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //better collision?
- int dirX=(xvel>0 ? 1 : (xvel<0 ? -1 : 0)); //move pixel by pixel through multiple iteration
- int dirY=(yvel>0 ? 1 : (yvel<0 ? -1 : 0));
- int a=10; //a margin of the pixel, basically the border-width.
- int speedX=xvel;
- int speedY=yvel;
- for(int j=0;j<abs(yvel) || j<abs(xvel);j++) //this is done, so when the character goes fast, it doesn't move into the wall (move pixel by picel)
- {
- if(speedX) //if we still need to move on the x, move
- {
- box.x+=dirX;
- speedX-=dirX;
- }
- if(speedY) //same with y
- {
- box.y+=dirY;
- speedY-=dirY;
- }
- for(int i = 0; i < map.size(); i++){
- for(int j = start; j < end; j++){
- if(map[i][j] == 0){
- continue;
- }
- SDL_Rect destrect = {j * 50, i * 50, 50, 50};
- int err=0;
- if(collision(&box,&destrect)){
- noCollision = false;
- //create the 4 inside rect
- SDL_Rect top={box.x,box.y,box.w,a};
- SDL_Rect bottom={box.x,box.y+box.h-a,box.w,a};
- SDL_Rect left={box.x,box.y+a,a,box.h-2*a};
- SDL_Rect right={box.x+box.w-a,box.y+a,a,box.h-2*a};
- SDL_Rect center={box.x+a,box.y+a,box.w-2*a,box.h-2*a};
- //do the right back movement, if we have collision on the left
- if(collision(&destrect,&left))
- {
- box.x++;
- setDirection();
- }else if(collision(&destrect,&right)) //right...
- {
- box.x--;
- setDirection();
- }
- else if(collision(&destrect,&top))
- {
- box.y++;
- }
- else if(collision(&destrect,&bottom))
- {
- box.y--;
- ground=true;
- }
- if(collision(&destrect,¢er)) //we shouldn't get here, there is a block inside the player, try to move out
- {
- box.x--;
- box.y--;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement