Paddle and Ball do now collide. Still glitchy
This commit is contained in:
parent
064ace7bf1
commit
05f523b138
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,6 +8,7 @@
|
||||
sdl2-config
|
||||
*.exclude
|
||||
*.o
|
||||
highscoretest.c
|
||||
*.psd
|
||||
*.exe
|
||||
!bhi.exe
|
||||
|
79
breakout.c
79
breakout.c
@ -22,6 +22,8 @@ Paddle paddle;
|
||||
SDL_Texture * PADDLE_Texture;
|
||||
SDL_Rect * PADDLE_SourceRects;
|
||||
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
||||
double BALL_Speed = 10.0f;
|
||||
int PADDLE_Speed = 10;
|
||||
|
||||
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
||||
printf("Initializing Game...\n");
|
||||
@ -34,7 +36,7 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
||||
}
|
||||
|
||||
void BREAKOUT_Update(Uint8 * keystate){
|
||||
BALL_Update(&ball);
|
||||
BALL_Update(&ball, &paddle);
|
||||
PADDLE_Update(&paddle, keystate);
|
||||
}
|
||||
|
||||
@ -80,18 +82,69 @@ void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
|
||||
SDL_RenderCopyEx(renderer, BALL_Texture, BALL_SourceRects + (obj->TextureIndex), &(obj->TargetRect), obj->Rotation, NULL, SDL_FLIP_NONE);
|
||||
}
|
||||
|
||||
void BALL_Update(Ball * obj){
|
||||
(obj->Location) = vectorAdd((obj->Location), ( obj->Momentum));
|
||||
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) };
|
||||
|
||||
// THIS IS CURRENTLY A RECTANGLE COLLIDE
|
||||
if (((obj->TargetRect).x) + ((obj->TargetRect).w) < (rect->x)) {
|
||||
return false;
|
||||
}
|
||||
if (((obj->TargetRect).x) > (rect->x) + (rect->w)) {
|
||||
return false;
|
||||
}
|
||||
if (((obj->TargetRect).y) + ((obj->TargetRect).w) < (rect->y)) {
|
||||
return false;
|
||||
}
|
||||
if (((obj->TargetRect).y) > (rect->y) + (rect->h)) {
|
||||
return false;
|
||||
}
|
||||
// Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt
|
||||
if ((ballCenter.x) < (rect->x) || (ballCenter.x) > (rect->x) + (rect->w)) {
|
||||
(obj->Momentum).x = -(obj->Momentum).x;
|
||||
}
|
||||
if ((ballCenter.y) < (rect->y) || (ballCenter.y) > (rect->y) + (rect->h)) {
|
||||
(obj->Momentum).y = -(obj->Momentum).y;
|
||||
}
|
||||
return true;
|
||||
} /* BALL_CollideWithRect */
|
||||
|
||||
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2){
|
||||
if ((rect1->x) + (rect1->w) < (rect2->x)) {
|
||||
return false;
|
||||
}
|
||||
if ((rect1->x) > (rect2->x) + (rect2->w)) {
|
||||
return false;
|
||||
}
|
||||
if ((rect1->y) + (rect1->w) < (rect2->y)) {
|
||||
return false;
|
||||
}
|
||||
if ((rect1->y) > (rect2->y) + (rect2->h)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void BALL_Update(Ball * obj, Paddle * paddle){
|
||||
Vector lastMomentum = (obj->Momentum);
|
||||
Vector lastLocation = (obj->Location);
|
||||
|
||||
(obj->Rotation) += (obj->RotationValue);
|
||||
(obj->Location) = vectorAdd((obj->Location), lastMomentum);
|
||||
|
||||
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) {
|
||||
(obj->Location) = vectorSub((obj->Location), lastMomentum); // Maybe remove this
|
||||
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this
|
||||
}
|
||||
|
||||
if ((obj->Location).y > BREAKOUT_BoxHeight)
|
||||
(obj->Location) = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; // Dead
|
||||
if ((obj->Location).y < 0.0f)
|
||||
(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);
|
||||
(obj->Rotation) += (obj->RotationValue);
|
||||
if ((obj->Location).y > BREAKOUT_BoxHeight + (obj->Size))
|
||||
(obj->Location) = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; // Dead
|
||||
if ((obj->Location).y < (obj->Size))
|
||||
(obj->Momentum).y = -(obj->Momentum).y;
|
||||
if ((obj->Location).x < (obj->Size) || (obj->Location).x > BREAKOUT_BoxWidth - (obj->Size))
|
||||
(obj->Momentum).x = -(obj->Momentum).x;
|
||||
}
|
||||
} /* BALL_Update */
|
||||
void BALL_DestroyObject(Ball * obj){
|
||||
}
|
||||
void BALL_Deinitialize(){
|
||||
@ -154,9 +207,9 @@ void PADDLE_Update(Paddle * obj, Uint8 * keystate){
|
||||
bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
|
||||
|
||||
if (leftKeyPressed && (!rightKeyPressed)) {
|
||||
((obj->TargetRect).x) -= 5;
|
||||
((obj->TargetRect).x) -= PADDLE_Speed;
|
||||
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
||||
((obj->TargetRect).x) += 5;
|
||||
((obj->TargetRect).x) += PADDLE_Speed;
|
||||
}
|
||||
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
|
||||
}
|
||||
|
@ -37,18 +37,19 @@ void BREAKOUT_DEINITIALIZE();
|
||||
void BALL_Initialize(SDL_Renderer * renderer);
|
||||
Ball BALL_CreateDefault();
|
||||
void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
|
||||
void BALL_Update(Ball * obj);
|
||||
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect2);
|
||||
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2);
|
||||
void BALL_Update(Ball * obj, Paddle * paddle);
|
||||
void BALL_DestroyObject(Ball * obj);
|
||||
void BALL_Deinitialize();
|
||||
void PADDLE_Initialize(SDL_Renderer * renderer);
|
||||
Paddle PADDLE_CreateDefault();
|
||||
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj);
|
||||
bool KeyPressed(Uint8 * keystate, Uint8 * keyArray);
|
||||
bool KeyPressed(Uint8 * keystate, Uint8 * keyArray);
|
||||
void constrain(int * variable, int min, int max);
|
||||
void PADDLE_Update(Paddle * obj, Uint8 * keystate);
|
||||
void PADDLE_DestroyObject(Paddle * obj);
|
||||
void PADDLE_Deinitialize();
|
||||
|
||||
// End Prototypes
|
||||
|
||||
#endif // __breakout_h__
|
||||
|
Loading…
Reference in New Issue
Block a user