Add random ball velocity vector direction in a given angle

This commit is contained in:
Pradana AUMARS 2021-06-28 14:00:57 +02:00
parent c56480a33b
commit a70d61ca52
2 changed files with 9 additions and 3 deletions

View File

@ -9,4 +9,4 @@ pong_SOURCES = \
racket.c \
racket.h
pong_CFLAGS = $(SDL_CFLAGS)
pong_LDFLAGS = $(SDL_LIBS)
pong_LDFLAGS = $(SDL_LIBS) -lm

View File

@ -1,9 +1,13 @@
#include <stdlib.h> // srand, rand
#include <time.h> // time
#include <math.h> // sin, cos, M_PI
#include "ball.h"
#include "racket.h"
Ball*
Ball_init(int screen_width, int screen_height)
{
srand(time(NULL));
Ball *b = malloc(sizeof(Ball));
*(int *)&b->BALL_WIDTH = 20;
*(int *)&b->BALL_HEIGHT = 20;
@ -16,10 +20,12 @@ Ball_init(int screen_width, int screen_height)
void
Ball_reset(Ball *b, int direction) {
// 120 deg angle with norm horizontal vector
const int angle = rand() % 60 + 60;
b->posX = b->SCREEN_WIDTH/2;
b->posY = b->SCREEN_HEIGHT/2;
b->velX = direction * 7;
b->velY = 5;
b->velX = direction * b->BALL_VELOCITY * cos(angle * 2 * M_PI / 180);
b->velY = b->BALL_VELOCITY * sin(angle * 2 * M_PI / 180);
}
void