Fixed Paddle glitch

This commit is contained in:
Michael Chen 2018-01-12 14:04:44 +01:00
parent cc5211f84f
commit 61a470f0fb
3 changed files with 28 additions and 10 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -66,8 +66,8 @@ void BREAKOUT_ChangeSize(int width, int height){
} }
void BREAKOUT_Update(Uint8 * keystate){ void BREAKOUT_Update(Uint8 * keystate){
PADDLE_Update(&paddle, keystate); // Update paddle before ball because paddle is not static!
BALL_Update(&ball, &paddle); BALL_Update(&ball, &paddle);
PADDLE_Update(&paddle, keystate);
for (size_t i = 0; i < BlockCount; i++) { for (size_t i = 0; i < BlockCount; i++) {
BLOCK_Update(blocks + i); BLOCK_Update(blocks + i);
} }
@ -134,7 +134,7 @@ void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
} }
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
SDL_Point ballCenter = (SDL_Point) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) }; SDL_Point ballCenter = BALL_GetCenter(obj);
// THIS IS CURRENTLY A RECTANGLE COLLIDE // THIS IS CURRENTLY A RECTANGLE COLLIDE
if (((obj->TargetRect).x) + ((obj->TargetRect).w) < (rect->x)) { if (((obj->TargetRect).x) + ((obj->TargetRect).w) < (rect->x)) {
@ -149,7 +149,7 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
if (((obj->TargetRect).y) > (rect->y) + (rect->h)) { if (((obj->TargetRect).y) > (rect->y) + (rect->h)) {
return false; return false;
} }
// Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt // Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt im rechteck liegt!
if ((ballCenter.x) < (rect->x) || (ballCenter.x) > (rect->x) + (rect->w)) { if ((ballCenter.x) < (rect->x) || (ballCenter.x) > (rect->x) + (rect->w)) {
(obj->Momentum).x = -(obj->Momentum).x; (obj->Momentum).x = -(obj->Momentum).x;
} }
@ -182,26 +182,42 @@ void BALL_SteerMomentum(Ball * obj, Paddle * paddle){
offset *= 60.0f; offset *= 60.0f;
offset /= (double)(paddleHalfLen); offset /= (double)(paddleHalfLen);
printf("Offset = %.2f\n", offset); printf("Offset = %.2f\n", offset);
(obj->Momentum) = getScaledDirectionalUnitVector(offset, BALL_Speed); (obj->Momentum) = getDirectionalUnitVector(offset);
}
void RECT_SetTargetPos(SDL_Rect * rect, Vector * Location){
rect->x = (int)round(Location->x);
rect->y = (int)round(Location->y);
}
SDL_Point BALL_GetCenter(Ball * obj){
return (SDL_Point) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) };
} }
void BALL_Update(Ball * obj, Paddle * paddle){ void BALL_Update(Ball * obj, Paddle * paddle){
Vector oldMomentum = obj->Momentum; Vector oldMomentum = obj->Momentum;
Vector oldLocation = obj->Location;
SDL_Point ballCenter = BALL_GetCenter(obj);
(obj->Rotation) += (obj->RotationValue); (obj->Rotation) += (obj->RotationValue); // No effect on physics
(obj->Location) = vectorAdd((obj->Location), oldMomentum); (obj->Location) = vectorAdd((obj->Location), oldMomentum);
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) { if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) {
(obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this (obj->Location) = oldLocation; // Maybe remove this
BALL_SteerMomentum(obj, paddle); BALL_SteerMomentum(obj, paddle); // Sets it to unit vector!
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this // 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!
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));
}
(obj->Momentum) = vectorScaleTo((obj->Momentum), BALL_Speed);
} }
oldMomentum = obj->Momentum;
for (size_t i = 0; i < BlockCount; i++) { for (size_t i = 0; i < BlockCount; i++) {
oldMomentum = obj->Momentum;
BALL_CollideWithRect(obj, &(blocks[i].TargetRect)); BALL_CollideWithRect(obj, &(blocks[i].TargetRect));
(obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this (obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); (obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
oldMomentum = obj->Momentum;
} }
if ((obj->Location).y > BREAKOUT_BoxHeight) if ((obj->Location).y > BREAKOUT_BoxHeight)

View File

@ -40,6 +40,8 @@ void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect); bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect);
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2); bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2);
void BALL_SteerMomentum(Ball * obj, Paddle * paddle); void BALL_SteerMomentum(Ball * obj, Paddle * paddle);
void RECT_SetTargetPos(SDL_Rect * rect, Vector * Location);
SDL_Point BALL_GetCenter(Ball * obj);
void BALL_Update(Ball * obj, Paddle * paddle); void BALL_Update(Ball * obj, Paddle * paddle);
void BALL_DestroyObject(Ball * obj); void BALL_DestroyObject(Ball * obj);
void BALL_Deinitialize(); void BALL_Deinitialize();