SuperTuxKart
kart_model.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2008-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_KART_MODEL_HPP
20 #define HEADER_KART_MODEL_HPP
21 
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include <IAnimatedMeshSceneNode.h>
28 namespace irr
29 {
30  namespace scene { class IAnimatedMesh; class IMesh;
31  class ISceneNode; class IMeshSceneNode; }
32 }
33 using namespace irr;
34 namespace GE { class GERenderInfo; class GESPM; }
35 namespace SP { class SPMesh; }
36 
37 #include "utils/constants.hpp"
38 #include "utils/no_copy.hpp"
39 #include "utils/vec3.hpp"
40 
41 class AbstractKart;
42 class KartProperties;
43 class MovingTexture;
44 class XMLNode;
45 
48 {
50  struct Properties
51  {
52  Properties();
53 
54  ~Properties();
55 
56  Properties& operator=(const Properties& other);
57 
60 
63 
64  MovingTexture* m_moving_texture;
65 
66  void loadFromXMLNode(const XMLNode* xml_node);
67 
68  };
69 
70  SpeedWeightedObject() : m_model(NULL), m_node(NULL), m_name() {}
72  scene::IAnimatedMesh * m_model;
73 
75  scene::IAnimatedMeshSceneNode * m_node;
76 
79  core::matrix4 m_location;
80 
82  std::string m_name;
83 
85  std::string m_bone_name;
86 
90 };
91 typedef std::vector<SpeedWeightedObject> SpeedWeightedObjectList;
92 
93 
94 // ============================================================================
95 enum HeadlightType : int
96 {
97  HLT_UNKNOWN = -1,
98  HLT_POINT = 0,
99  HLT_SPOT,
100  HLT_MESH,
101  HLT_COUNT,
102 };
103 // ============================================================================
107 {
108 private:
110  std::string m_filename;
111 
114  core::matrix4 m_location;
115 
117  scene::IMesh* m_model;
118 
120  scene::ISceneNode* m_node;
121 
123  video::SColor m_headlight_color;
124 
126  std::string m_bone_name;
127 
128  HeadlightType m_headlight_type;
129 
130  float m_radius;
131 
132  float m_energy;
133 
134  float m_inner_cone;
135 
136  float m_outer_cone;
137 public:
138  // ------------------------------------------------------------------------
139  static HeadlightType getHeadlightTypeFromString(const std::string& s)
140  {
141  if (s == "point")
142  return HLT_POINT;
143  if (s == "spot")
144  return HLT_SPOT;
145  if (s == "mesh")
146  return HLT_MESH;
147  return HLT_UNKNOWN;
148  }
149  // ------------------------------------------------------------------------
151  {
152  m_model = NULL;
153  m_node = NULL;
154  m_headlight_type = HLT_POINT;
155  m_radius = 0.0f;
156  m_energy = 0.0f;
157  m_inner_cone = 0.0f;
158  m_outer_cone = 0.0f;
159  } // HeadlightObject
160  // ------------------------------------------------------------------------
161  HeadlightObject(const std::string& filename, const core::matrix4& location,
162  const std::string& bone_name, const video::SColor& color,
163  HeadlightType headlight_type, float radius, float energy,
164  float inner_cone, float outer_cone)
165  {
166  m_filename = filename;
167  m_location = location;
168  m_model = NULL;
169  m_node = NULL;
170  m_bone_name = bone_name;
171  m_headlight_color = color;
172  m_headlight_type = headlight_type;
173  m_radius = radius;
174  m_energy = energy;
175  m_inner_cone = inner_cone;
176  m_outer_cone = outer_cone;
177  } // HeadlightObjects
178  // ------------------------------------------------------------------------
179  const std::string& getFilename() const { return m_filename; }
180  // ------------------------------------------------------------------------
182  void setModel(scene::IMesh *mesh) { m_model = mesh; }
183  // ------------------------------------------------------------------------
184  void setLight(scene::ISceneNode* parent);
185  // ------------------------------------------------------------------------
186  void setLightNode(scene::ISceneNode* node) { m_node = node; }
187  // ------------------------------------------------------------------------
188  const scene::ISceneNode *getLightNode() const { return m_node; }
189  // ------------------------------------------------------------------------
190  scene::ISceneNode *getLightNode() { return m_node; }
191  // ------------------------------------------------------------------------
192  const scene::IMesh *getModel() const { return m_model; }
193  // ------------------------------------------------------------------------
194  scene::IMesh *getModel() { return m_model; }
195  // ------------------------------------------------------------------------
196  const core::matrix4& getLocation() const { return m_location; }
197  // ------------------------------------------------------------------------
198  const std::string& getBoneName() const { return m_bone_name; }
199  // ------------------------------------------------------------------------
200  HeadlightType getHeadlightType() const { return m_headlight_type; }
201  // ------------------------------------------------------------------------
202  void setRadius(float radius) { m_radius = radius; }
203  // ------------------------------------------------------------------------
204  void setEnergy(float energy) { m_energy = energy; }
205  // ------------------------------------------------------------------------
206  void setDefaultConeValues(unsigned light_count)
207  {
208  float outer_cone_angle = (light_count == 1 ? 45.0f : 50.0f);
209  m_outer_cone = outer_cone_angle / 180.0f * M_PI;
210  m_inner_cone = m_outer_cone / 1.5f;
211  }
212  // ------------------------------------------------------------------------
213  bool useDefaultSettings() const
214  {
215  return m_radius == 0.0f && m_energy == 0.0f && m_inner_cone == 0.0f &&
216  m_outer_cone == 0.0f;
217  }
218 }; // class HeadlightObject
219 
220 // ============================================================================
221 
232 class KartModel : public scene::IAnimationEndCallBack, public NoCopy
233 {
234 public:
235  enum AnimationFrameType
236  {AF_BEGIN, // First animation frame
237  AF_DEFAULT = AF_BEGIN, // Default, i.e. steering animation
238  AF_LEFT, // Steering to the left
239  AF_STRAIGHT, // Going straight
240  AF_RIGHT, // Steering to the right
241  AF_LOSE_START, // Begin losing animation
242  AF_LOSE_LOOP_START, // Begin of the losing loop
243  AF_LOSE_END, // End losing animation
244  AF_LOSE_END_STRAIGHT, // End losing animation to straight frame
245  AF_BEGIN_EXPLOSION, // Begin explosion animation
246  AF_END_EXPLOSION, // End explosion animation
247  AF_JUMP_START, // Begin of jump
248  AF_JUMP_LOOP, // Begin of jump loop
249  AF_JUMP_END, // End of jump
250  AF_WIN_START, // Begin of win animation
251  AF_WIN_LOOP_START, // Begin of win loop animation
252  AF_WIN_END, // End of win animation
253  AF_WIN_END_STRAIGHT, // End of win animation to straight frame
254  AF_SELECTION_START, // Start frame in kart selection screen
255  AF_SELECTION_END, // End frame in kart selection screen
256  AF_BACK_LEFT, // Going back left
257  AF_BACK_STRAIGHT, // Going back straight
258  AF_BACK_RIGHT, // Going back right
259  AF_END=AF_BACK_RIGHT, // Last animation frame
260  AF_COUNT}; // Number of entries here
261 
262 private:
264  int m_animation_frame[AF_COUNT];
265 
268 
270  scene::IAnimatedMesh *m_mesh;
271 
275  scene::IAnimatedMeshSceneNode *m_animated_node;
276 
278  core::matrix4* m_hat_location;
279 
281  std::string m_hat_bone;
282 
284  std::string m_hat_name;
285 
287  static float UNDEFINED;
288 
290  std::string m_model_filename;
291 
293  scene::IMesh *m_wheel_model[4];
294 
296  scene::ISceneNode *m_wheel_node[4];
297 
299  std::string m_wheel_filename[4];
300 
302  Vec3 m_wheel_graphics_position[4];
303 
305  float m_wheel_graphics_radius[4];
306 
308  Vec3 m_nitro_emitter_position[2];
309 
312 
314  SpeedWeightedObjectList m_speed_weighted_objects;
315 
316  std::vector<HeadlightObject> m_headlight_objects;
317 
319  float m_default_physics_suspension[4];
320 
323  float m_min_suspension[4];
324 
328  float m_max_suspension[4];
329 
332  float m_dampen_suspension_amplitude[4];
333 
337  AnimationFrameType m_current_animation;
338 
341 
344 
347 
350 
353 
358 
359  void loadWheelInfo(const XMLNode &node,
360  const std::string &wheel_name, int index);
361 
362  void loadNitroEmitterInfo(const XMLNode &node,
363  const std::string &emitter_name, int index);
364 
365  void loadSpeedWeightedInfo(const XMLNode* speed_weighted_node, int index);
366 
367  void loadHeadlights(const XMLNode &node, const std::string& kart_dir);
368 
369  void OnAnimationEnd(scene::IAnimatedMeshSceneNode *node);
370 
373 
375  std::shared_ptr<GE::GERenderInfo> m_render_info;
376 
380 
383  std::unordered_map<std::string, core::matrix4> m_inverse_bone_matrices;
384 
386  unsigned m_version;
387 
389  std::string m_exhaust_xml;
390 
391  const KartProperties* m_kart_properties;
392  // ------------------------------------------------------------------------
393  void initInverseBoneMatrices();
394  // ------------------------------------------------------------------------
395  void configNode(scene::ISceneNode* node, const core::matrix4& global_mat,
396  const core::matrix4& inv_mat)
397  {
398  const core::matrix4 mat = inv_mat * global_mat;
399  const core::vector3df position = mat.getTranslation();
400  const core::vector3df rotation = mat.getRotationDegrees();
401  const core::vector3df scale = mat.getScale();
402  node->setPosition(position);
403  node->setRotation(rotation);
404  node->setScale(scale);
405  }
406  // ------------------------------------------------------------------------
407  bool handleSpotlight(GE::GESPM* spm);
408  // ------------------------------------------------------------------------
409  bool handleSPSpotlight(SP::SPMesh* spm);
410 
411 public:
412  KartModel(bool is_master);
413  ~KartModel();
414  KartModel* makeCopy(std::shared_ptr<GE::GERenderInfo> ri);
415  void reset();
416  void loadInfo(const XMLNode &node);
417  bool loadModels(const KartProperties &kart_properties);
418  void setDefaultSuspension();
419  void update(float dt, float distance, float steer, float speed,
420  float current_lean_angle,
421  int gt_replay_index = -1);
422  void finishedRace();
423  void resetVisualWheelPosition();
424  scene::ISceneNode*
425  attachModel(bool animatedModels, bool human_player);
426  // ------------------------------------------------------------------------
428  scene::IAnimatedMesh*
429  getModel() const { return m_mesh; }
430 
431  // ------------------------------------------------------------------------
433  scene::IMesh* getWheelModel(const int i) const
434  { assert(i>=0 && i<4); return m_wheel_model[i]; }
435  // ------------------------------------------------------------------------
438  int getBaseFrame() const { return m_animation_frame[AF_STRAIGHT]; }
439  // ------------------------------------------------------------------------
440  int getFrame(AnimationFrameType f) const { return m_animation_frame[f]; }
441  // ------------------------------------------------------------------------
442  float getAnimationSpeed() const { return m_animation_speed; }
443  // ------------------------------------------------------------------------
447  const Vec3& getWheelGraphicsPosition(unsigned int i) const
448  {assert(i<4); return m_wheel_graphics_position[i];}
449  // ------------------------------------------------------------------------
453  {return m_wheel_graphics_position;}
454  // ------------------------------------------------------------------------
458  float getWheelGraphicsRadius(unsigned int i) const
459  {assert(i<4); return m_wheel_graphics_radius[i]; }
460  // ------------------------------------------------------------------------
464  const Vec3& getNitroEmittersPositon(unsigned int i) const
465  { assert(i<2); return m_nitro_emitter_position[i]; }
466  // ------------------------------------------------------------------------
468  const bool hasNitroEmitters() const
469  {return m_has_nitro_emitter;}
470  // ------------------------------------------------------------------------
473  {return m_speed_weighted_objects.size();}
474  // ------------------------------------------------------------------------
478  {return m_speed_weighted_objects[i];}
479  // ------------------------------------------------------------------------
481  float getLength () const {return m_kart_length; }
482  // ------------------------------------------------------------------------
484  float getWidth () const {return m_kart_width; }
485  // ------------------------------------------------------------------------
487  float getHeight () const {return m_kart_height; }
488  // ------------------------------------------------------------------------
490  float getHighestPoint () const { return m_kart_highest_point; }
491  // ------------------------------------------------------------------------
493  float getLowestPoint () const { return m_kart_lowest_point; }
494  // ------------------------------------------------------------------------
496  AnimationFrameType getAnimation() { return m_current_animation; }
497  // ------------------------------------------------------------------------
499  void setAnimation(AnimationFrameType type, bool play_non_loop = false);
500  // ------------------------------------------------------------------------
502  void setKart(AbstractKart* k) { m_kart = k; }
503  // ------------------------------------------------------------------------
505  void setHatMeshName(const std::string &name) {m_hat_name = name; }
506  // ------------------------------------------------------------------------
508  scene::ISceneNode** getWheelNodes() { return m_wheel_node; }
509  // ------------------------------------------------------------------------
510  scene::IAnimatedMeshSceneNode* getAnimatedNode(){ return m_animated_node; }
511  // ------------------------------------------------------------------------
512  std::shared_ptr<GE::GERenderInfo> getRenderInfo();
513  // ------------------------------------------------------------------------
514  bool supportColorization() const { return m_support_colorization; }
515  // ------------------------------------------------------------------------
516  void toggleHeadlights(bool on);
517  // ------------------------------------------------------------------------
518  const core::matrix4&
519  getInverseBoneMatrix(const std::string& bone_name) const;
520  // ------------------------------------------------------------------------
521  const std::string& getExhaustXML() const { return m_exhaust_xml; }
522  // ------------------------------------------------------------------------
523  bool hasWheel() const { return !m_wheel_filename[0].empty(); }
524  // ------------------------------------------------------------------------
525  const KartProperties* getKartProperties() const
526  { return m_kart_properties; }
527 }; // KartModel
528 #endif
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:62
A class to store the headlights of a kart.
Definition: kart_model.hpp:107
video::SColor m_headlight_color
The color of the real light.
Definition: kart_model.hpp:123
scene::IMesh * m_model
The mesh for the headlight.
Definition: kart_model.hpp:117
std::string m_filename
The filename of the headlight model.
Definition: kart_model.hpp:110
core::matrix4 m_location
The relative matrix to the parent kart scene node where the headlight mesh is attached to.
Definition: kart_model.hpp:114
std::string m_bone_name
Attach to which bone in kart model if not empty.
Definition: kart_model.hpp:126
scene::ISceneNode * m_node
The scene node of the headlight (real light).
Definition: kart_model.hpp:120
void setModel(scene::IMesh *mesh)
Sets the mesh for this headlight object.
Definition: kart_model.hpp:182
This class stores a 3D kart model.
Definition: kart_model.hpp:233
scene::IAnimatedMesh * getModel() const
Returns the animated mesh of this kart model.
Definition: kart_model.hpp:429
AnimationFrameType m_current_animation
Which animation is currently being played.
Definition: kart_model.hpp:337
std::string m_model_filename
Name of the 3d model file.
Definition: kart_model.hpp:290
const Vec3 & getNitroEmittersPositon(unsigned int i) const
Returns the position of nitro emitter relative to the kart.
Definition: kart_model.hpp:464
int getBaseFrame() const
Since karts might be animated, we might need to know which base frame to use.
Definition: kart_model.hpp:438
float m_kart_width
Width of kart.
Definition: kart_model.hpp:340
std::unordered_map< std::string, core::matrix4 > m_inverse_bone_matrices
Used to cache inverse bone matrices for each bone in straight frame for attachment.
Definition: kart_model.hpp:383
float getWheelGraphicsRadius(unsigned int i) const
Returns the radius of the graphical wheels.
Definition: kart_model.hpp:458
core::matrix4 * m_hat_location
Location of hat in object space.
Definition: kart_model.hpp:278
SpeedWeightedObjectList m_speed_weighted_objects
The speed weighted objects.
Definition: kart_model.hpp:314
float getHeight() const
Returns the height of the kart.
Definition: kart_model.hpp:487
unsigned m_version
Version of kart model (in kart.xml).
Definition: kart_model.hpp:386
scene::ISceneNode ** getWheelNodes()
Returns the array of wheel nodes.
Definition: kart_model.hpp:508
AbstractKart * m_kart
Pointer to the kart object belonging to this kart model.
Definition: kart_model.hpp:372
size_t getSpeedWeightedObjectsCount() const
Returns the number of speed weighted objects for this kart.
Definition: kart_model.hpp:472
float getWidth() const
Returns the width of the kart model.
Definition: kart_model.hpp:484
bool m_has_nitro_emitter
True if kart has nitro emitters.
Definition: kart_model.hpp:311
float m_kart_lowest_point
Smallest coordinate on up axis.
Definition: kart_model.hpp:352
bool m_is_master
True if this is the master copy, managed by KartProperties.
Definition: kart_model.hpp:357
float m_kart_height
Height of kart.
Definition: kart_model.hpp:346
std::shared_ptr< GE::GERenderInfo > m_render_info
For our engine to get the desired hue for colorization.
Definition: kart_model.hpp:375
const bool hasNitroEmitters() const
Returns true if kart has nitro emitters.
Definition: kart_model.hpp:468
float m_kart_length
Length of kart.
Definition: kart_model.hpp:343
AnimationFrameType getAnimation()
Returns information about currently played animation.
Definition: kart_model.hpp:496
float getHighestPoint() const
Highest coordinate on up axis.
Definition: kart_model.hpp:490
bool m_support_colorization
True if this kart model can be colorization in red / blue (now only used in soccer mode).
Definition: kart_model.hpp:379
scene::IMesh * getWheelModel(const int i) const
Returns the mesh of the wheel for this kart.
Definition: kart_model.hpp:433
void setHatMeshName(const std::string &name)
Name of the hat mesh to use.
Definition: kart_model.hpp:505
const Vec3 & getWheelGraphicsPosition(unsigned int i) const
Returns the position of a wheel relative to the kart.
Definition: kart_model.hpp:447
float getLength() const
Returns the length of the kart model.
Definition: kart_model.hpp:481
static float UNDEFINED
Value used to indicate undefined entries.
Definition: kart_model.hpp:287
void setKart(AbstractKart *k)
Sets the kart this model is currently used for.
Definition: kart_model.hpp:502
std::string m_hat_bone
Name of the bone for hat attachment.
Definition: kart_model.hpp:281
const SpeedWeightedObject & getSpeedWeightedObject(int i) const
Returns the position of a speed weighted object relative to the kart.
Definition: kart_model.hpp:477
std::string m_hat_name
Name of the hat to use for this kart.
Definition: kart_model.hpp:284
const Vec3 * getWheelsGraphicsPosition() const
Returns the position of wheels relative to the kart.
Definition: kart_model.hpp:452
float m_kart_highest_point
Largest coordinate on up axis.
Definition: kart_model.hpp:349
float getLowestPoint() const
Lowest coordinate on up axis.
Definition: kart_model.hpp:493
scene::IAnimatedMesh * m_mesh
The mesh of the model.
Definition: kart_model.hpp:270
scene::IAnimatedMeshSceneNode * m_animated_node
This is a pointer to the scene node of the kart this model belongs to.
Definition: kart_model.hpp:275
float m_animation_speed
Animation speed.
Definition: kart_model.hpp:267
std::string m_exhaust_xml
Exhaust particle file (xml) for the kart, empty if disabled.
Definition: kart_model.hpp:389
This class stores the properties of a kart.
Definition: kart_properties.hpp:60
Handles animated textures (textures that move)
Definition: moving_texture.hpp:34
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Definition: sp_mesh.hpp:44
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
utility class used to parse XML files
Definition: xml_node.hpp:48
SimpleVec3 getLocation(int idKart)
Attempts to project kart to the given 2D location, to the position with height 0, at a 45 degree angl...
Definition: script_kart.cpp:110
Parameters for a speed-weighted object.
Definition: kart_model.hpp:51
float m_strength_factor
Strength factor: how much the kart speed affects the animation's distance from a static pose (-1 to d...
Definition: kart_model.hpp:59
float m_speed_factor
Speed factor: how much the kart speed affects the animation's speed (-1 to disable)
Definition: kart_model.hpp:62
A speed-weighted object is an object whose characteristics are influenced by the kart's speed.
Definition: kart_model.hpp:48
std::string m_name
Filename of the "speed weighted" object.
Definition: kart_model.hpp:82
scene::IAnimatedMesh * m_model
Model.
Definition: kart_model.hpp:72
core::matrix4 m_location
The relative matrix to the parent kart scene node where the speed weighted object is attached to.
Definition: kart_model.hpp:79
std::string m_bone_name
Attach to which bone in kart model if not empty.
Definition: kart_model.hpp:85
Properties m_properties
Specific properties for this given speed-weighted object, otherwise just a copy of the values from th...
Definition: kart_model.hpp:89
scene::IAnimatedMeshSceneNode * m_node
The scene node the speed weighted model is attached to.
Definition: kart_model.hpp:75