-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdan.cpp
119 lines (90 loc) · 2.75 KB
/
dan.cpp
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
116
117
118
119
#include "dan.h"
#include "manager.h"
#include "equation.h"
#include <math.h>
PEGISTRATE_PLAYER(DanThePlayer);
// it is not very wrong, but don't complete
double DanThePlayer::were()
{
const Ball &ball = Manager::getSingleton().getBall("ball");
double t = 0;
if(ball.get_speed().y > 0)
{
t = ball.get_speed().y / ball.get_accel().y;
double ym = ball.get_pos().y + ball.get_speed().y * t - ball.get_accel().y * t*t / 2;
t += MyUseful::PositiveRoot(1, 0, - 2 * ym / ball.get_accel().y);//????!!!!
}
else //vy <= 0
{
t = MyUseful::PositiveRoot(1, 2 * ball.get_speed().y / ball.get_accel().y, - 2 * ball.get_pos().y / ball.get_accel().y);
}
return ball.get_pos().x + ball.get_speed().x * t + 0.01;
}
//return vector, which means angle, by which we need push the ball
//i suзpouse that x-position of net == 0
// i need have strong think about it
Vector2D DanThePlayer::How()
{
Manager &mng = Manager::getSingleton();
double x1 = 0, ym = 0;
double vy;
double vx;
for (int i = 0; i < 4 && x1 > -1 && ym < 2; ++i)
{
if(side)
{
x1 = (0.05 + x1) / 2;
ym = mng.getNet("twall").b.y - (i + 1) * mng.getBall("ball").get_radius();
}
else
{
x1 = (-0.05 + x1) / 2;
ym = mng.getNet("net").b.y + (i + 1) * mng.getBall("ball").get_radius();
}
vy = sqrt(mng.getBall("ball").get_accel().y * 2 * ym);
vx = x1 * sqrt(mng.getBall("ball").get_accel().y / (ym * 2)) / 2;
if((vx*vx + vy*vy) < getMaxForce() * mng.getStep() / mng.getBall("ball").get_Mass())
return Vector2D(vx, vy);
}
return Vector2D(0., 0.);
}
DanThePlayer::DanThePlayer(const Vector2D &left, const Vector2D &right, bool Side = true)
:
Player(left, right, 0.2, 0.05, 0.5), side(Side)
{
}
DanThePlayer::DanThePlayer()
:
DanThePlayer(Vector2D(-1., 0.), Vector2D(0., 1.), true)
{
}
void DanThePlayer::init(const Vector2D &left, const Vector2D &right,
double height, double max_speed, double max_force,
double curr)
{
initDefault(left, right, height, max_speed, max_force,curr);
}
bool DanThePlayer::move()
{
return Player::move();
}
void DanThePlayer::idle()
{
double x = were() - get_pos().x;
double vx = x / Manager::getSingleton().getStep();
if(MyUseful::absd(vx) < getMaxSpeed())
speed = Vector2D(vx, 0);
else
speed = Vector2D(getMaxSpeed(), 0);
}
Vector2D DanThePlayer::get_force()
{
return How() * (1.0 / Manager::getSingleton().getStep());
// Here:
// bPos -- position vector
// bSpd -- speed vector
// bAcc -- acceleration vector
}
DanThePlayer::~DanThePlayer()
{
}