Collision improvement
This commit is contained in:
parent
fc35d1c9a5
commit
1d85fd6e70
|
@ -2,3 +2,6 @@
|
|||
*.o
|
||||
*.swo
|
||||
*.swn
|
||||
*.swk
|
||||
*.swl
|
||||
*.swm
|
||||
|
|
3
main.cpp
3
main.cpp
|
@ -56,6 +56,7 @@ int main(int argc, char* args[]){
|
|||
Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&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 powerup(13*sz,SCREEN_HEIGHT-sz*4,20,20,&gRenderer);
|
||||
|
||||
posweg.init(100,100,sz,sz,&gRenderer);
|
||||
|
||||
|
@ -77,12 +78,14 @@ int main(int argc, char* args[]){
|
|||
wallC.print(camera.getPosX());
|
||||
ground.print(camera.getPosX());
|
||||
ground2.print(camera.getPosX());
|
||||
powerup.print(camera.getPosX());
|
||||
|
||||
posweg.check(wallA.getRectangle());
|
||||
posweg.check(wallB.getRectangle());
|
||||
posweg.check(wallC.getRectangle());
|
||||
posweg.check(ground.getRectangle());
|
||||
posweg.check(ground2.getRectangle());
|
||||
posweg.check(powerup.getRectangle());
|
||||
|
||||
SDL_RenderPresent(gRenderer);
|
||||
}
|
||||
|
|
23
player.cpp
23
player.cpp
|
@ -51,8 +51,7 @@ bool Player::loadMedia(){
|
|||
plyFrame[frame].x=szW*column;
|
||||
plyFrame[frame].y=szH*row;
|
||||
frame++;
|
||||
}
|
||||
}
|
||||
} }
|
||||
}
|
||||
|
||||
return success;
|
||||
|
@ -153,9 +152,11 @@ int Player::check(SDL_Rect rectA){
|
|||
|
||||
//Move the player out of the rectangle
|
||||
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)
|
||||
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;
|
||||
movY += sin(angle) / 2;
|
||||
collision = 1;
|
||||
|
@ -165,7 +166,8 @@ int Player::check(SDL_Rect rectA){
|
|||
if(collision == 1){
|
||||
//Vertical adjustement
|
||||
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){
|
||||
movY++;
|
||||
velocityY = 0;
|
||||
|
@ -176,7 +178,8 @@ int Player::check(SDL_Rect rectA){
|
|||
}
|
||||
//Horizontal adjustement
|
||||
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){
|
||||
movX++;
|
||||
}
|
||||
|
@ -193,7 +196,8 @@ int Player::check(SDL_Rect rectA){
|
|||
//Check collsion type and reset velocities
|
||||
//Vertical collisions
|
||||
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
|
||||
if(posY + szH == bY){
|
||||
ground = true;
|
||||
|
@ -212,8 +216,9 @@ int Player::check(SDL_Rect rectA){
|
|||
}
|
||||
//Horizontal collisions
|
||||
if((posY >= bY and posY < b2Y)
|
||||
or (posY + szH > bY and posY + szH <= b2Y)){
|
||||
//Left collision
|
||||
or (posY + szH > bY and posY + szH <= b2Y)
|
||||
or (posY < bY and posY + szH > b2Y)){
|
||||
//Left collision
|
||||
if(posX + szW == bX){
|
||||
collision = 4;
|
||||
currentFrame = &plyFrame[2];
|
||||
|
|
Loading…
Reference in New Issue