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_Rect * PADDLE_SourceRects;
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
double BALL_Speed = 10.0f;
double BALL_Speed = 15.0f;
int PADDLE_Speed = 10;
bool BREAKOUT_IsInit = false;
bool BALL_IsInit = false;
@ -86,7 +86,7 @@ Ball BALL_CreateDefault(){
return (Ball) {
.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 },
.Size = 25.0f,
.Rotation = rotation,
@ -142,16 +142,23 @@ bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2){
return true;
}
void BALL_Update(Ball * obj, Paddle * paddle){
Vector lastMomentum = (obj->Momentum);
Vector lastLocation = (obj->Location);
void BALL_SteerMomentum(Ball * obj, Paddle * paddle){
int paddleHalfLen = ((paddle->TargetRect).w / 2.0f);
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->Location) = vectorAdd((obj->Location), lastMomentum);
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) {
(obj->Location) = vectorSub((obj->Location), lastMomentum); // Maybe remove this
// BALL_SteerMomentum(obj, &(paddle->TargetRect));
(obj->Location) = vectorSub((obj->Location), (obj->Momentum)); // Maybe remove this
BALL_SteerMomentum(obj, paddle);
(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){
// 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){