Tinman  1.0
car.h
Go to the documentation of this file.
1 // Copyright (C) 2014 ISAAC LACOBA MOLINA
2 // Tinman author: Isaac Lacoba Molina
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #ifndef CAR_H
17 #define CAR_H
18 #include <tuple>
19 #include <queue>
20 
21 #include "physics.h"
22 #include "scene.h"
23 #include "carcontroller.h"
24 #include "sound.h"
25 #include "objecttextdisplay.h"
26 #include "track.h"
27 
28 #include "tinman.h"
29 
30 bool equals(float sut, float target, float delta);
31 bool equals(btVector3 sut, btVector3 target, float delta);
32 class lower_than {
33  bool reverse_;
34  public:
35  lower_than( const bool& reverse=false){ reverse_ = reverse;}
36  bool operator() (const Tinman::CarInfo& first, const Tinman::CarInfo& second) const {
37  return (reverse_)? (first.seqNumber > second.seqNumber):
38  (first.seqNumber < second.seqNumber);
39  }
40 };
41 
42 class Car {
43  public:
44  typedef std::shared_ptr<Car> shared;
47  Ice::Byte id_;
48  int lap_;
49  Ogre::SceneNode* chassis_node_;
50  btRigidBody* chassis_body_;
52 
54 
55  Car(std::string nick);
56  virtual ~Car();
57 
58  void realize(Scene::shared scene, std::string name, std::string material,
59  Ogre::Vector3 position, int scale);
60  void realize(Physics::shared physics, Scene::shared scene,
61  Sound::shared sound, std::string name, btVector3 position,
62  std::string material, int checkpoints, Ice::Byte id);
63  void realize(Physics::shared physics, Sound::shared sound, std::string name,
64  btVector3 position, Ice::Byte id);
65 
66  void exec(Tinman::Action action);
67  void accelerate();
68  void use_nitro();
69  void stop_accelerating();
70  void brake();
71  void stop_braking();
72  void turn(Direction direction);
73  void turn_right();
74  void turn_left();
75  void stop_turning();
76 
77  void update(float delta);
78  void synchronize(Tinman::CarInfo last_update);
79 
80  int get_nitro();
81  int get_lap();
82  btVector3 get_position();
83  btVector3 get_velocity();
84  btQuaternion get_orientation();
85  btVector3 get_direction(int factor = 1);
86  btScalar get_speed();
88 
89  void set_position(btVector3 position);
90  void set_race_segment_info(Section segment);
91  void reset(int checkpoints, btVector3 position);
92  void collide_with_car();
93  void animation(float delta);
94 
95  bool is_colliding();
96  bool is_stuck();
97  void invert_direction();
98 
99  private:
102  std::map<Tinman::Action, std::function<void()>> action_hooks;
103  const btQuaternion initial_rotation_ = btQuaternion(btVector3(0, 1, 0), btScalar(80));
104  btVector3 initial_position_;
105  unsigned int sections_covered_;
107  Tinman::CarInfo last_update_;
108 
109  std::string nick_;
111 
113 
115  const float invert_direction_delay_ = 0.48f;
116  const int not_moving_delay_ = 0.48f;
117  const int colliding_delay_ = 0.48f;
118 
119  std::priority_queue<Tinman::CarInfo, std::vector<Tinman::CarInfo>, lower_than> sync_buffer_;
120  std::vector<std::pair<int, bool>> checkpoints_;
121 
122  btCompoundShape* compound_;
123  btAlignedObjectArray<btCollisionShape*> collision_shapes_;
124 
125  btRaycastVehicle::btVehicleTuning tuning_;
126  btVehicleRaycaster* vehicle_raycaster_;
127  btRaycastVehicle* vehicle_;
128  btCollisionShape* wheel_shape_;
129 
130  Ogre::Entity* chassis_entity_;
131  std::vector<Ogre::SceneNode*> wheels_nodes_;
132  std::vector<Ogre::Entity*> wheels_entities_;
133 
135  btQuaternion last_orientation_;
136 
137  void init_graphic_bodies(Scene::shared scene, std::string name, std::string,
138  Ogre::Vector3 position = Ogre::Vector3(0,0,0), int scale = 1);
139  void init_physic_bodies(Physics::shared physics, btVector3 position);
140  void init_raycast_car(Physics::shared physics);
141  void add_graphic_wheel(Scene::shared scene, std::string parent, std::string name,
142  int scale = 1);
143  void add_physic_wheel(bool is_front, btVector3 connection_point,
144  int wheel_index);
145  void add_nick_billboard(Scene::shared scene);
146  void add_wheels();
147  void configure_wheels();
148 
149  void turn_wheels(Direction direction);
150  void control_speed();
151 
152  void reset_position();
153 
154  void apply_update(bool delete_update);
155  void update_lap();
156 
157 
158  bool not_moving();
159  bool is_on_meta();
160  bool has_done_a_lap();
161  void add_lap();
162 };
163 
164 #endif
btCompoundShape * compound_
Definition: car.h:122
bool stuck_
Definition: car.h:53
void stop_accelerating()
Definition: car.cpp:209
btVector3 get_velocity()
Definition: car.cpp:331
void add_nick_billboard(Scene::shared scene)
Definition: car.cpp:140
const int not_moving_delay_
Definition: car.h:116
void use_nitro()
Definition: car.cpp:351
void update(float delta)
Definition: car.cpp:250
void add_lap()
Definition: car.cpp:356
Direction
Definition: carcontroller.h:24
void add_wheels()
Definition: car.cpp:172
void animation(float delta)
Definition: car.cpp:425
void set_position(btVector3 position)
Definition: car.cpp:452
void init_graphic_bodies(Scene::shared scene, std::string name, std::string, Ogre::Vector3 position=Ogre::Vector3(0, 0, 0), int scale=1)
Definition: car.cpp:94
CarController::shared controller_
Definition: car.h:45
void reset(int checkpoints, btVector3 position)
Definition: car.cpp:365
std::shared_ptr< Physics > shared
Definition: physics.h:33
int get_lap()
Definition: car.cpp:346
void stop_turning()
Definition: car.cpp:240
void update_lap()
Definition: car.cpp:269
void configure_wheels()
Definition: car.cpp:190
std::shared_ptr< Scene > shared
Definition: scene.h:43
bool accelerating_
Definition: car.h:106
void synchronize(Tinman::CarInfo last_update)
Definition: car.cpp:285
void brake()
Definition: car.cpp:215
bool can_collide_
Definition: car.h:114
bool colliding_
Definition: car.h:53
Ice::Byte id_
Definition: car.h:47
std::vector< Ogre::Entity * > wheels_entities_
Definition: car.h:132
bool reverse_
Definition: car.h:33
void turn(Direction direction)
Definition: car.cpp:225
unsigned int sections_covered_
Definition: car.h:105
Definition: track.h:33
bool not_moving()
Definition: car.cpp:437
Definition: car.h:42
void turn_wheels(Direction direction)
btQuaternion get_orientation()
Definition: car.cpp:336
void exec(Tinman::Action action)
Definition: car.cpp:52
btVehicleRaycaster * vehicle_raycaster_
Definition: car.h:126
btVector3 get_position()
Definition: car.cpp:326
std::shared_ptr< Sound > shared
Definition: sound.h:13
bool has_done_a_lap()
Definition: car.cpp:410
int lap_
Definition: car.h:48
Ogre::SceneNode * chassis_node_
Definition: car.h:49
void init_raycast_car(Physics::shared physics)
Definition: car.cpp:132
const int colliding_delay_
Definition: car.h:117
void add_graphic_wheel(Scene::shared scene, std::string parent, std::string name, int scale=1)
Definition: car.cpp:149
btVector3 initial_position_
Definition: car.h:104
btVector3 get_direction(int factor=1)
Definition: car.cpp:393
void init_physic_bodies(Physics::shared physics, btVector3 position)
Definition: car.cpp:114
Physics::shared physics_
Definition: car.h:101
void add_physic_wheel(bool is_front, btVector3 connection_point, int wheel_index)
Definition: car.cpp:161
std::shared_ptr< Car > shared
Definition: car.h:44
std::vector< Ogre::SceneNode * > wheels_nodes_
Definition: car.h:131
const float invert_direction_delay_
Definition: car.h:115
void turn_left()
Definition: car.cpp:235
Definition: objecttextdisplay.h:5
bool is_on_meta()
Definition: car.cpp:403
void apply_update(bool delete_update)
Definition: car.cpp:304
bool operator()(const Tinman::CarInfo &first, const Tinman::CarInfo &second) const
Definition: car.h:36
bool equals(float sut, float target, float delta)
Definition: car.cpp:313
Tinman::CarInfo last_update_
Definition: car.h:107
std::shared_ptr< CarController > shared
Definition: carcontroller.h:60
btQuaternion last_orientation_
Definition: car.h:135
void set_race_segment_info(Section segment)
Definition: car.cpp:388
btRaycastVehicle * vehicle_
Definition: car.h:127
void invert_direction()
Definition: car.cpp:458
btCollisionShape * wheel_shape_
Definition: car.h:128
void accelerate()
Definition: car.cpp:202
btVector3 last_position_
Definition: car.h:134
btAlignedObjectArray< btCollisionShape * > collision_shapes_
Definition: car.h:123
Section current_segment_
Definition: car.h:51
const btQuaternion initial_rotation_
Definition: car.h:103
std::vector< std::pair< int, bool > > checkpoints_
Definition: car.h:120
int race_position_
Definition: car.h:46
float colliding_time_
Definition: car.h:112
btRigidBody * chassis_body_
Definition: car.h:50
Ogre::Entity * chassis_entity_
Definition: car.h:130
float invert_direction_
Definition: car.h:112
bool is_stuck()
Definition: car.cpp:442
void turn_right()
Definition: car.cpp:230
int get_nitro()
Definition: car.cpp:341
btScalar get_speed()
Definition: car.cpp:415
Car(std::string nick)
Definition: car.cpp:20
Section get_current_section()
Definition: car.cpp:420
bool is_colliding()
Definition: car.cpp:432
void stop_braking()
Definition: car.cpp:220
ObjectTextDisplay * nickname_display_
Definition: car.h:110
void collide_with_car()
Definition: car.cpp:447
btRaycastVehicle::btVehicleTuning tuning_
Definition: car.h:125
void realize(Scene::shared scene, std::string name, std::string material, Ogre::Vector3 position, int scale)
Definition: car.cpp:58
std::priority_queue< Tinman::CarInfo, std::vector< Tinman::CarInfo >, lower_than > sync_buffer_
Definition: car.h:119
Definition: car.h:32
Sound::shared sound_
Definition: car.h:100
virtual ~Car()
Definition: car.cpp:43
btVector3 last_velocity_
Definition: car.h:134
lower_than(const bool &reverse=false)
Definition: car.h:35
std::string nick_
Definition: car.h:109
void reset_position()
Definition: car.cpp:382
float not_moving_
Definition: car.h:112
void control_speed()
Definition: car.cpp:245
std::map< Tinman::Action, std::function< void()> > action_hooks
Definition: car.h:102