Fixed Paddle glitch
This commit is contained in:
parent
cc5211f84f
commit
61a470f0fb
Binary file not shown.
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 11 KiB |
36
breakout.c
36
breakout.c
@ -66,8 +66,8 @@ void BREAKOUT_ChangeSize(int width, int height){
|
||||
}
|
||||
|
||||
void BREAKOUT_Update(Uint8 * keystate){
|
||||
PADDLE_Update(&paddle, keystate); // Update paddle before ball because paddle is not static!
|
||||
BALL_Update(&ball, &paddle);
|
||||
PADDLE_Update(&paddle, keystate);
|
||||
for (size_t i = 0; i < BlockCount; 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){
|
||||
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
|
||||
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)) {
|
||||
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)) {
|
||||
(obj->Momentum).x = -(obj->Momentum).x;
|
||||
}
|
||||
@ -182,26 +182,42 @@ void BALL_SteerMomentum(Ball * obj, Paddle * paddle){
|
||||
offset *= 60.0f;
|
||||
offset /= (double)(paddleHalfLen);
|
||||
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){
|
||||
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);
|
||||
|
||||
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) {
|
||||
(obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this
|
||||
BALL_SteerMomentum(obj, paddle);
|
||||
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this
|
||||
(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!
|
||||
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++) {
|
||||
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));
|
||||
oldMomentum = obj->Momentum;
|
||||
}
|
||||
|
||||
if ((obj->Location).y > BREAKOUT_BoxHeight)
|
||||
|
@ -40,6 +40,8 @@ void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
|
||||
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect);
|
||||
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2);
|
||||
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_DestroyObject(Ball * obj);
|
||||
void BALL_Deinitialize();
|
||||
|
Loading…
Reference in New Issue
Block a user