Paddle collision works

This commit is contained in:
Michael Chen 2018-01-11 19:03:12 +01:00
parent 77bd1ed996
commit bb164e51d8

View File

@ -22,7 +22,7 @@ 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; double BALL_Speed = 15.0f;
int PADDLE_Speed = 10; int PADDLE_Speed = 10;
bool BREAKOUT_IsInit = false; bool BREAKOUT_IsInit = false;
bool BALL_IsInit = false; bool BALL_IsInit = false;
@ -86,7 +86,7 @@ Ball BALL_CreateDefault(){
return (Ball) { return (Ball) {
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }, .Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 },
.Momentum = (Vector) {.x = 0.0f, .y = 6.0f }, .Momentum = (Vector) {.x = 0.0f, .y = BALL_Speed },
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 }, .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 },
.Size = 25.0f, .Size = 25.0f,
.Rotation = rotation, .Rotation = rotation,
@ -142,16 +142,23 @@ bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2){
return true; return true;
} }
void BALL_Update(Ball * obj, Paddle * paddle){ void BALL_SteerMomentum(Ball * obj, Paddle * paddle){
Vector lastMomentum = (obj->Momentum); int paddleHalfLen = ((paddle->TargetRect).w / 2.0f);
Vector lastLocation = (obj->Location); double offset = (((obj->TargetRect).x) + (obj->Size)) - ((paddle->TargetRect).x + paddleHalfLen);
offset *= 60.0f;
offset /= (double)(paddleHalfLen);
printf("Offset = %.2f\n", offset);
(obj->Momentum) = getScaledDirectionalUnitVector(offset, BALL_Speed);
}
void BALL_Update(Ball * obj, Paddle * paddle){
(obj->Rotation) += (obj->RotationValue); (obj->Rotation) += (obj->RotationValue);
(obj->Location) = vectorAdd((obj->Location), lastMomentum); (obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) { if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) {
(obj->Location) = vectorSub((obj->Location), lastMomentum); // Maybe remove this (obj->Location) = vectorSub((obj->Location), (obj->Momentum)); // Maybe remove this
// BALL_SteerMomentum(obj, &(paddle->TargetRect)); BALL_SteerMomentum(obj, paddle);
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this (obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this
} }
@ -211,7 +218,9 @@ Paddle PADDLE_CreateDefault(){
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){ void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){
// printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x); // printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect)); // SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect));
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(renderer, &(obj->TargetRect));
} }
bool KeyPressed(Uint8 * keystate, Uint8 * keyArray){ bool KeyPressed(Uint8 * keystate, Uint8 * keyArray){