Add random ball velocity vector direction in a given angle
This commit is contained in:
parent
c56480a33b
commit
a70d61ca52
@ -9,4 +9,4 @@ pong_SOURCES = \
|
||||
racket.c \
|
||||
racket.h
|
||||
pong_CFLAGS = $(SDL_CFLAGS)
|
||||
pong_LDFLAGS = $(SDL_LIBS)
|
||||
pong_LDFLAGS = $(SDL_LIBS) -lm
|
||||
|
10
src/ball.c
10
src/ball.c
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user