Ball collides with blocks

This commit is contained in:
Michael Chen 2018-01-12 16:49:17 +01:00
parent 61a470f0fb
commit a29178ec55
2 changed files with 22 additions and 10 deletions

View File

@ -150,6 +150,7 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
return false;
}
// Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt im rechteck liegt!
// BUG: Ball glitches through blocks after being pushed by another collision
if ((ballCenter.x) < (rect->x) || (ballCenter.x) > (rect->x) + (rect->w)) {
(obj->Momentum).x = -(obj->Momentum).x;
}
@ -206,7 +207,7 @@ void BALL_Update(Ball * obj, Paddle * paddle){
(obj->Location) = oldLocation; // Maybe remove this
BALL_SteerMomentum(obj, paddle); // Sets it to unit vector!
// Following assumes that the paddle position was udated before the ball was updated
// BUG/GLITCH: Make sure that the postition of the ball is not shited into the borders of the game!
// BUG/GLITCH: Make sure that the postition of the ball is not shifted into the borders of the game!
while (RECT_Collide(&(obj->TargetRect), &(paddle->TargetRect))) { // Move away from rect in small steps
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
@ -214,10 +215,13 @@ void BALL_Update(Ball * obj, Paddle * paddle){
(obj->Momentum) = vectorScaleTo((obj->Momentum), BALL_Speed);
}
for (size_t i = 0; i < BlockCount; i++) {
if (blocks[i].HP <= 0) continue;
oldMomentum = obj->Momentum;
BALL_CollideWithRect(obj, &(blocks[i].TargetRect));
(obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
if (BALL_CollideWithRect(obj, &(blocks[i].TargetRect))) {
BLOCK_DealDamage(blocks + i, 1);
(obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
}
}
if ((obj->Location).y > BREAKOUT_BoxHeight)
@ -226,8 +230,7 @@ void BALL_Update(Ball * obj, Paddle * paddle){
(obj->Momentum).y = -(obj->Momentum).y;
if ((obj->Location).x < 0.0f || (obj->Location).x > BREAKOUT_BoxWidth - (2 * (obj->Size)))
(obj->Momentum).x = -(obj->Momentum).x;
(obj->TargetRect).x = (int)round((obj->Location).x);
(obj->TargetRect).y = (int)round((obj->Location).y);
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
} /* BALL_Update */
void BALL_DestroyObject(Ball * obj){
}
@ -303,6 +306,7 @@ void PADDLE_Update(Paddle * obj, Uint8 * keystate){
}
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
}
void PADDLE_DestroyObject(Paddle * obj){
}
void PADDLE_Deinitialize(){
@ -335,13 +339,20 @@ Block BLOCK_CreateDefault() {
return (Block) {
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = 150, .h = 75 },
.TextureIndex = (rand() % BLOCK_TextureCount)
.TextureIndex = (rand() % BLOCK_TextureCount),
.HP = 1
}; // Objekt für die Eigenschaften des Balls
}
void BLOCK_Draw(SDL_Renderer * renderer, Block * obj){
// printf("Block drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).y);
SDL_RenderCopy(renderer, BLOCK_Texture, (BLOCK_SourceRects + (obj->TextureIndex)), &(obj->TargetRect));
if ((obj->HP) > 0) {
// printf("Block drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).y);
SDL_RenderCopy(renderer, BLOCK_Texture, (BLOCK_SourceRects + (obj->TextureIndex)), &(obj->TargetRect));
}
}
void BLOCK_DealDamage(Block * obj, int dmg){
if (((obj->HP) -= dmg) <= 0) printf("Block was destroyed!\n");
}
void BLOCK_Update(Block * obj){

View File

@ -24,7 +24,7 @@ typedef struct paddleStruct {
typedef struct blockStruct {
SDL_Rect TargetRect;
int TextureIndex;
int TextureIndex, HP;
} Block; // Objekt für die Eigenschaften des Paddles
// End Structs
@ -56,6 +56,7 @@ void PADDLE_Deinitialize();
void BLOCK_Initialize(SDL_Renderer * renderer);
Block BLOCK_CreateDefault() ;
void BLOCK_Draw(SDL_Renderer * renderer, Block * obj);
void BLOCK_DealDamage(Block * obj, int dmg);
void BLOCK_Update(Block * obj);
void BLOCK_DestroyObject(Block * obj);
void BLOCK_Deinitialize();