-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBall.cpp
More file actions
115 lines (105 loc) · 2.68 KB
/
Ball.cpp
File metadata and controls
115 lines (105 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Ball.h"
#include <stdio.h>
#include <math.h>
#include "Helpers.h"
void Ball::drawBall()
{
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(_xPos, _yPos, _zPos);
glutSolidSphere(BallSize, 50, 50);
glPopMatrix();
}
void Ball::Update()
{
//Kod preuzet donekle sa casova vezbi, adaptiran za moj slucaj
if(_xSpeed > 0)
{
_xPos += _xSpeed*_xDirection;
if (_xPos <= -(600 + BallSize / 2)) {
//provera da li je igrac B dao gol
if(_yPos > -200 && _yPos < 100)
{
_PlayerBGoal = true;
}
_xDirection *= -1;
_xPos = -(600 + BallSize / 2);
}
if (_xPos >= 600 + BallSize / 2)
{
//provera da li je igrac A dao gol
if(_yPos > -200 && _yPos < 100)
{
_PlayerAGoal = true;
}
_xDirection *= -1;
_xPos = 600 + BallSize / 2;
}
}
_yPos += _ySpeed;
if (_yPos <= -(200 - BallSize / 2)) {
_ySpeed *= -1;
_yPos = -(200 - BallSize / 2);
}
if (_yPos >= 500 - BallSize / 2) {
_ySpeed *= -1;
_yPos = 500 - BallSize / 2;
}
//podesavano po osecaju
_xSpeed -= 0.06;
_ySpeed -= 5.0;
}
void Ball::CheckHeadCollision(float x, float y)
{
//provera da li je u koliziji sa igracevom glavom cije su koordinate prosledjene
//desavanje nakon kolizije podeseno po osecaju
float distance = sqrt((_xPos - x)*(_xPos - x) + (_yPos - y)*(_yPos - y));
if(distance <= BallSize*2)
{
if(_xSpeed == 0.0)
{
if(x < _xPos)
_xDirection = 1;
else
_xDirection = -1;
}
else
{
_xDirection *= -1;
}
_xSpeed = 25.0;
if(_ySpeed > 0)
{
_ySpeed = -48.1;
};
if(_ySpeed < 0)
{
_ySpeed = 48.1;
};
}
}
void Ball::CheckBodyCollision(float x, float y)
{
//provera da li je u koliziji sa igracevim telom cije su koordinate prosledjene
//desavanje nakon kolizije podeseno po osecaju
//telo je za 100 ispod glave po y osi
y-=100;
float distance = sqrt((_xPos - x)*(_xPos - x) + (_yPos - y)*(_yPos - y));
//racunam koliziju sa unutrasnjim upisanim krugom kvadrata
if(distance <= (BallSize + PlayerBodySize/2))
{
if(_xSpeed == 0.0)
{
if(x < _xPos)
_xDirection = 1;
else
_xDirection = -1;
}
else
{
_xDirection *= -1;
}
_xSpeed = 30.0;
_ySpeed = 48.1;
}
}