Collision improvement

This commit is contained in:
posweg 2017-05-02 20:56:32 +02:00
parent fc35d1c9a5
commit 1d85fd6e70
No known key found for this signature in database
GPG Key ID: 6F5B526E5F8D81DB
4 changed files with 20 additions and 9 deletions

3
.gitignore vendored
View File

@ -2,3 +2,6 @@
*.o *.o
*.swo *.swo
*.swn *.swn
*.swk
*.swl
*.swm

BIN
main

Binary file not shown.

View File

@ -56,6 +56,7 @@ int main(int argc, char* args[]){
Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer); Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer);
Block ground(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer); Block ground(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer);
Block ground2(SCREEN_WIDTH+sz*4,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer); Block ground2(SCREEN_WIDTH+sz*4,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer);
Block powerup(13*sz,SCREEN_HEIGHT-sz*4,20,20,&gRenderer);
posweg.init(100,100,sz,sz,&gRenderer); posweg.init(100,100,sz,sz,&gRenderer);
@ -77,12 +78,14 @@ int main(int argc, char* args[]){
wallC.print(camera.getPosX()); wallC.print(camera.getPosX());
ground.print(camera.getPosX()); ground.print(camera.getPosX());
ground2.print(camera.getPosX()); ground2.print(camera.getPosX());
powerup.print(camera.getPosX());
posweg.check(wallA.getRectangle()); posweg.check(wallA.getRectangle());
posweg.check(wallB.getRectangle()); posweg.check(wallB.getRectangle());
posweg.check(wallC.getRectangle()); posweg.check(wallC.getRectangle());
posweg.check(ground.getRectangle()); posweg.check(ground.getRectangle());
posweg.check(ground2.getRectangle()); posweg.check(ground2.getRectangle());
posweg.check(powerup.getRectangle());
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
} }

View File

@ -51,8 +51,7 @@ bool Player::loadMedia(){
plyFrame[frame].x=szW*column; plyFrame[frame].x=szW*column;
plyFrame[frame].y=szH*row; plyFrame[frame].y=szH*row;
frame++; frame++;
} } }
}
} }
return success; return success;
@ -153,9 +152,11 @@ int Player::check(SDL_Rect rectA){
//Move the player out of the rectangle //Move the player out of the rectangle
while(((movX >= bX and movX < b2X) while(((movX >= bX and movX < b2X)
or (movX + szW > bX and movX + szW <= b2X)) or (movX + szW > bX and movX + szW <= b2X)
or (movX < bX and movX + szW > b2X))
and ((movY >= bY and movY < b2Y) and ((movY >= bY and movY < b2Y)
or (movY + szH > bY and movY + szH <= b2Y))){ or (movY + szH > bY and movY + szH <= b2Y)
or (movY < bY and movY + szH > b2Y))){
movX += cos(angle) / 2; movX += cos(angle) / 2;
movY += sin(angle) / 2; movY += sin(angle) / 2;
collision = 1; collision = 1;
@ -165,7 +166,8 @@ int Player::check(SDL_Rect rectA){
if(collision == 1){ if(collision == 1){
//Vertical adjustement //Vertical adjustement
if((movX > bX and movX < b2X) if((movX > bX and movX < b2X)
or (movX + szW > bX and movX + szW < b2X)){ or (movX + szW > bX and movX + szW < b2X)
or (movX < bX and movX + szW > b2X)){
while(movY + szH < bY){ while(movY + szH < bY){
movY++; movY++;
velocityY = 0; velocityY = 0;
@ -176,7 +178,8 @@ int Player::check(SDL_Rect rectA){
} }
//Horizontal adjustement //Horizontal adjustement
if((movY > bY and movY < b2Y) if((movY > bY and movY < b2Y)
or (movY + szH > bY and movY + szH < b2Y)){ or (movY + szH > bY and movY + szH < b2Y)
or (movY < bY and movY + szH > b2Y)){
while(movX + szW < bX){ while(movX + szW < bX){
movX++; movX++;
} }
@ -193,7 +196,8 @@ int Player::check(SDL_Rect rectA){
//Check collsion type and reset velocities //Check collsion type and reset velocities
//Vertical collisions //Vertical collisions
if((posX >= bX and posX < b2X) if((posX >= bX and posX < b2X)
or (posX + szW > bX and posX + szW <= b2X)){ or (posX + szW > bX and posX + szW <= b2X)
or (posX < bX and posX + szW > b2X)){
//Top collision //Top collision
if(posY + szH == bY){ if(posY + szH == bY){
ground = true; ground = true;
@ -212,8 +216,9 @@ int Player::check(SDL_Rect rectA){
} }
//Horizontal collisions //Horizontal collisions
if((posY >= bY and posY < b2Y) if((posY >= bY and posY < b2Y)
or (posY + szH > bY and posY + szH <= b2Y)){ or (posY + szH > bY and posY + szH <= b2Y)
//Left collision or (posY < bY and posY + szH > b2Y)){
//Left collision
if(posX + szW == bX){ if(posX + szW == bX){
collision = 4; collision = 4;
currentFrame = &plyFrame[2]; currentFrame = &plyFrame[2];