Phoenix2D-Library  0.10
Ball.cpp
Go to the documentation of this file.
1 /*
2  * Phoenix2D (RoboCup Soccer Simulation 2D League)
3  * Copyright (c) 2013 Ivan Gonzalez
4  *
5  * This file is part of Phoenix2D.
6  *
7  * Phoenix2D is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Phoenix2D is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Phoenix2D. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "Ball.h"
22 #include <vector>
23 #include <sstream>
24 #include <cstdlib>
25 #include "Self.h"
26 #include <cmath>
27 namespace Phoenix
28 {
30  distance = 100.0;
31  direction = 0.0;
32  distChange = 0.0;
33  dirChange = 0.0;
34  x = 0.0;
35  y = 0.0;
36  vx = 0.0;
37  vy = 0.0;
38  simulation_time = 0;
39  in_sight_range = false;
40  bound = 0;
41 }
42 
43 Ball::Ball(int simulation_time) {
44  distance = 100.0;
45  direction = 0.0;
46  distChange = 0.0;
47  dirChange = 0.0;
48  x = 0.0;
49  y = 0.0;
50  vx = 0.0;
51  vy = 0.0;
52  this->simulation_time = simulation_time;
53  in_sight_range = false;
54  bound = 0;
55 }
56 
57 Ball::Ball(std::string position, int simulation_time) {
58  distance = 100.0;
59  direction = 0.0;
60  distChange = 0.0;
61  dirChange = 0.0;
62  x = 0.0;
63  y = 0.0;
64  vx = 0.0;
65  vy = 0.0;
66  std::vector<std::string> tokens;
67  std::stringstream ss_position(position); // = std::stringstream(name);
68  std::string token;
69  while (std::getline(ss_position, token, ' ')) {
70  tokens.push_back(token);
71  }
72  switch (tokens.size()) {
73  case 4:
74  x = atof(tokens[0].c_str());
75  y = atof(tokens[1].c_str());
76  vx = atof(tokens[2].c_str());
77  vy = atof(tokens[3].c_str());
78  break;
79  default:
80  break;
81  }
82  this->simulation_time = simulation_time;
83  in_sight_range = true;
84  bound = 0;
85 }
86 
87 Ball::Ball(std::string position, int simulation_time, Position player_position, Vector2D player_velocity) {
88  distance = 100.0;
89  direction = 0.0;
90  distChange = 0.0;
91  dirChange = 0.0;
92  x = 0.0;
93  y = 0.0;
94  vx = 0.0;
95  vy = 0.0;
96  std::vector<std::string> tokens;
97  std::stringstream ss_position(position); // = std::stringstream(name);
98  std::string token;
99  while (std::getline(ss_position, token, ' ')) {
100  tokens.push_back(token);
101  }
102  switch (tokens.size()) {
103  case 4:
104  distance = atof(tokens[0].c_str());
105  direction = atof(tokens[1].c_str());
106  distChange = atof(tokens[2].c_str());
107  dirChange = atof(tokens[3].c_str());
108  break;
109  case 3:
110  break;
111  case 2:
112  distance = atof(tokens[0].c_str());
113  direction = atof(tokens[1].c_str());
114  break;
115  case 1:
116  direction = atof(tokens[0].c_str());
117  break;
118  default:
119  break;
120  }
121  double source_direction = player_position.getBodyDirection() + player_position.getHeadDirection() - direction;
122  if (source_direction > 180.0) {
123  source_direction -= 360.0;
124  } else if (source_direction <= 180.0) {
125  source_direction += 360.0;
126  }
127  double erx = cos(Self::PI * source_direction / 180.0);
128  double ery = sin(Self::PI * source_direction / 180.0);
129  x = player_position.getX() + erx * distance;
130  y = player_position.getY() + ery * distance;
131  double erxm = (180.0 * erx) / (Self::PI * distance);
132  double erym = (180.0 * ery) / (Self::PI * distance);
133  double vry = (distChange * erym + dirChange * erx) / (ery * erym + erx * erxm);
134  double vrx = (distChange - ery * vry) / erx;
135  vx = player_velocity.getXComponent() + vrx;
136  vy = player_velocity.getYComponent() + vry;
137  this->simulation_time = simulation_time;
138  in_sight_range = true;
139  bound = 0;
140 }
141 
143 
144 }
145 
147  return Position(x, y);
148 }
149 
152 }
153 
155  return in_sight_range;
156 }
157 
159  bound = ball;
160 }
161 
163  return bound;
164 }
165 }
Ball * getBound()
Definition: Ball.cpp:162
bool in_sight_range
Definition: Ball.h:48
double vx
Definition: Ball.h:45
Ball The Ball lorem Ipsum
Definition: Ball.h:38
Position The Position lorem Ipsum
Definition: Position.h:50
Ball ball
Definition: World.cpp:31
Ball * bound
Definition: Ball.h:49
static double PI
Definition: Self.h:47
void boundTo(Ball *ball)
Definition: Ball.cpp:158
Vector2D getVelocity()
Definition: Ball.cpp:150
static Vector2D getVector2DWithXAndY(double dx, double dy)
Definition: Vector2D.cpp:59
double getYComponent()
Definition: Vector2D.cpp:67
double direction
Definition: Ball.h:40
bool isInSightRange()
Definition: Ball.cpp:154
double x
Definition: Ball.h:43
double distance
Definition: Ball.h:39
double getBodyDirection()
Definition: Position.cpp:73
Position getPosition()
Definition: Ball.cpp:146
int simulation_time
Definition: Ball.h:47
double distChange
Definition: Ball.h:41
double y
Definition: Ball.h:44
double getHeadDirection()
Definition: Position.cpp:77
double dirChange
Definition: Ball.h:42
double getXComponent()
Definition: Vector2D.cpp:63
double vy
Definition: Ball.h:46
Vector2D The Vector2D lorem Ipsum
Definition: Vector2D.h:45