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
|
sdl2-config
|
||||||
*.exclude
|
*.exclude
|
||||||
*.o
|
*.o
|
||||||
|
highscoretest.c
|
||||||
*.psd
|
*.psd
|
||||||
*.exe
|
*.exe
|
||||||
!bhi.exe
|
!bhi.exe
|
||||||
|
79
breakout.c
79
breakout.c
@ -22,6 +22,8 @@ Paddle paddle;
|
|||||||
SDL_Texture * PADDLE_Texture;
|
SDL_Texture * PADDLE_Texture;
|
||||||
SDL_Rect * PADDLE_SourceRects;
|
SDL_Rect * PADDLE_SourceRects;
|
||||||
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
||||||
|
double BALL_Speed = 10.0f;
|
||||||
|
int PADDLE_Speed = 10;
|
||||||
|
|
||||||
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
||||||
printf("Initializing Game...\n");
|
printf("Initializing Game...\n");
|
||||||
@ -34,7 +36,7 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BREAKOUT_Update(Uint8 * keystate){
|
void BREAKOUT_Update(Uint8 * keystate){
|
||||||
BALL_Update(&ball);
|
BALL_Update(&ball, &paddle);
|
||||||
PADDLE_Update(&paddle, keystate);
|
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);
|
SDL_RenderCopyEx(renderer, BALL_Texture, BALL_SourceRects + (obj->TextureIndex), &(obj->TargetRect), obj->Rotation, NULL, SDL_FLIP_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BALL_Update(Ball * obj){
|
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
|
||||||
(obj->Location) = vectorAdd((obj->Location), ( obj->Momentum));
|
SDL_Point ballCenter = (SDL_Point) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) };
|
||||||
(obj->TargetRect).x = (int)round((obj->Location).x);
|
|
||||||
(obj->TargetRect).y = (int)round((obj->Location).y);
|
// THIS IS CURRENTLY A RECTANGLE COLLIDE
|
||||||
(obj->Rotation) += (obj->RotationValue);
|
if (((obj->TargetRect).x) + ((obj->TargetRect).w) < (rect->x)) {
|
||||||
if ((obj->Location).y > BREAKOUT_BoxHeight + (obj->Size))
|
return false;
|
||||||
(obj->Location) = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; // Dead
|
}
|
||||||
if ((obj->Location).y < (obj->Size))
|
if (((obj->TargetRect).x) > (rect->x) + (rect->w)) {
|
||||||
(obj->Momentum).y = -(obj->Momentum).y;
|
return false;
|
||||||
if ((obj->Location).x < (obj->Size) || (obj->Location).x > BREAKOUT_BoxWidth - (obj->Size))
|
}
|
||||||
|
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;
|
(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);
|
||||||
|
} /* BALL_Update */
|
||||||
void BALL_DestroyObject(Ball * obj){
|
void BALL_DestroyObject(Ball * obj){
|
||||||
}
|
}
|
||||||
void BALL_Deinitialize(){
|
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);
|
bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
|
||||||
|
|
||||||
if (leftKeyPressed && (!rightKeyPressed)) {
|
if (leftKeyPressed && (!rightKeyPressed)) {
|
||||||
((obj->TargetRect).x) -= 5;
|
((obj->TargetRect).x) -= PADDLE_Speed;
|
||||||
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
||||||
((obj->TargetRect).x) += 5;
|
((obj->TargetRect).x) += PADDLE_Speed;
|
||||||
}
|
}
|
||||||
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
|
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,9 @@ void BREAKOUT_DEINITIALIZE();
|
|||||||
void BALL_Initialize(SDL_Renderer * renderer);
|
void BALL_Initialize(SDL_Renderer * renderer);
|
||||||
Ball BALL_CreateDefault();
|
Ball BALL_CreateDefault();
|
||||||
void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
|
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_DestroyObject(Ball * obj);
|
||||||
void BALL_Deinitialize();
|
void BALL_Deinitialize();
|
||||||
void PADDLE_Initialize(SDL_Renderer * renderer);
|
void PADDLE_Initialize(SDL_Renderer * renderer);
|
||||||
@ -48,7 +50,6 @@ void constrain(int * variable, int min, int max);
|
|||||||
void PADDLE_Update(Paddle * obj, Uint8 * keystate);
|
void PADDLE_Update(Paddle * obj, Uint8 * keystate);
|
||||||
void PADDLE_DestroyObject(Paddle * obj);
|
void PADDLE_DestroyObject(Paddle * obj);
|
||||||
void PADDLE_Deinitialize();
|
void PADDLE_Deinitialize();
|
||||||
|
|
||||||
// End Prototypes
|
// End Prototypes
|
||||||
|
|
||||||
#endif // __breakout_h__
|
#endif // __breakout_h__
|
||||||
|
Loading…
Reference in New Issue
Block a user