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 
35 #include "utils/no_copy.hpp"
36 #include "utils/vec3.hpp"
37 
38 class AbstractKart;
39 class KartProperties;
40 class RenderInfo;
41 class XMLNode;
42 
45 {
47  struct Properties
48  {
49  Properties();
50 
53 
56 
58  core::vector2df m_texture_speed;
59 
60  void loadFromXMLNode(const XMLNode* xml_node);
61 
62  };
63 
64  SpeedWeightedObject() : m_model(NULL), m_node(NULL), m_name() {}
66  scene::IAnimatedMesh * m_model;
67 
69  scene::IAnimatedMeshSceneNode * m_node;
70 
73  core::matrix4 m_location;
74 
76  std::string m_name;
77 
79  std::string m_bone_name;
80 
82  core::vector2df m_texture_cur_offset;
83 
87 };
88 typedef std::vector<SpeedWeightedObject> SpeedWeightedObjectList;
89 
90 // ============================================================================
94 {
95 private:
97  std::string m_filename;
98 
101  core::matrix4 m_location;
102 
104  scene::IMesh* m_model;
105 
107  scene::ISceneNode* m_node;
108 
110  video::SColor m_headlight_color;
111 
113  std::string m_bone_name;
114 
115 public:
116 
118  {
119  m_model = NULL;
120  m_node = NULL;
121  } // HeadlightObject
122  // ------------------------------------------------------------------------
123  HeadlightObject(const std::string& filename, const core::matrix4& location,
124  const std::string& bone_name, const video::SColor& color)
125  {
126  m_filename = filename;
127  m_location = location;
128  m_model = NULL;
129  m_node = NULL;
130  m_bone_name = bone_name;
131  m_headlight_color = color;
132  } // HeadlightObjects
133  // ------------------------------------------------------------------------
134  const std::string& getFilename() const { return m_filename; }
135  // ------------------------------------------------------------------------
137  void setModel(scene::IMesh *mesh) { m_model = mesh; }
138  // ------------------------------------------------------------------------
139  void setLight(scene::ISceneNode* parent, float energy, float radius);
140  // ------------------------------------------------------------------------
141  const scene::ISceneNode *getLightNode() const { return m_node; }
142  // ------------------------------------------------------------------------
143  scene::ISceneNode *getLightNode() { return m_node; }
144  // ------------------------------------------------------------------------
145  const scene::IMesh *getModel() const { return m_model; }
146  // ------------------------------------------------------------------------
147  scene::IMesh *getModel() { return m_model; }
148  // ------------------------------------------------------------------------
149  const core::matrix4& getLocation() const { return m_location; }
150  // ------------------------------------------------------------------------
151  const std::string& getBoneName() const { return m_bone_name; }
152  // ------------------------------------------------------------------------
153 }; // class HeadlightObject
154 
155 // ============================================================================
156 
167 class KartModel : public scene::IAnimationEndCallBack, public NoCopy
168 {
169 public:
170  enum AnimationFrameType
171  {AF_BEGIN, // First animation frame
172  AF_DEFAULT = AF_BEGIN, // Default, i.e. steering animation
173  AF_LEFT, // Steering to the left
174  AF_STRAIGHT, // Going straight
175  AF_RIGHT, // Steering to the right
176  AF_LOSE_START, // Begin losing animation
177  AF_LOSE_LOOP_START, // Begin of the losing loop
178  AF_LOSE_END, // End losing animation
179  AF_LOSE_END_STRAIGHT, // End losing animation to straight frame
180  AF_BEGIN_EXPLOSION, // Begin explosion animation
181  AF_END_EXPLOSION, // End explosion animation
182  AF_JUMP_START, // Begin of jump
183  AF_JUMP_LOOP, // Begin of jump loop
184  AF_JUMP_END, // End of jump
185  AF_WIN_START, // Begin of win animation
186  AF_WIN_LOOP_START, // Begin of win loop animation
187  AF_WIN_END, // End of win animation
188  AF_WIN_END_STRAIGHT, // End of win animation to straight frame
189  AF_SELECTION_START, // Start frame in kart selection screen
190  AF_SELECTION_END, // End frame in kart selection screen
191  AF_BACK_LEFT, // Going back left
192  AF_BACK_STRAIGHT, // Going back straight
193  AF_BACK_RIGHT, // Going back right
194  AF_END=AF_BACK_RIGHT, // Last animation frame
195  AF_COUNT}; // Number of entries here
196 
197 private:
199  int m_animation_frame[AF_COUNT];
200 
203 
205  scene::IAnimatedMesh *m_mesh;
206 
210  scene::IAnimatedMeshSceneNode *m_animated_node;
211 
213  core::matrix4* m_hat_location;
214 
216  std::string m_hat_bone;
217 
219  std::string m_hat_name;
220 
222  static float UNDEFINED;
223 
225  std::string m_model_filename;
226 
228  scene::IMesh *m_wheel_model[4];
229 
231  scene::ISceneNode *m_wheel_node[4];
232 
234  std::string m_wheel_filename[4];
235 
237  Vec3 m_wheel_graphics_position[4];
238 
240  float m_wheel_graphics_radius[4];
241 
243  Vec3 m_nitro_emitter_position[2];
244 
247 
249  SpeedWeightedObjectList m_speed_weighted_objects;
250 
251  std::vector<HeadlightObject> m_headlight_objects;
252 
254  float m_default_physics_suspension[4];
255 
258  float m_min_suspension[4];
259 
263  float m_max_suspension[4];
264 
267  float m_dampen_suspension_amplitude[4];
268 
272  AnimationFrameType m_current_animation;
273 
276 
279 
282 
285 
288 
293 
294  void loadWheelInfo(const XMLNode &node,
295  const std::string &wheel_name, int index);
296 
297  void loadNitroEmitterInfo(const XMLNode &node,
298  const std::string &emitter_name, int index);
299 
300  void loadSpeedWeightedInfo(const XMLNode* speed_weighted_node);
301 
302  void loadHeadlights(const XMLNode &node);
303 
304  void OnAnimationEnd(scene::IAnimatedMeshSceneNode *node);
305 
308 
310  std::shared_ptr<RenderInfo> m_render_info;
311 
315 
318  std::unordered_map<std::string, core::matrix4> m_inverse_bone_matrices;
319 
321  unsigned m_version;
322 
324  std::string m_exhaust_xml;
325 
326  const KartProperties* m_kart_properties;
327  // ------------------------------------------------------------------------
328  void initInverseBoneMatrices();
329  // ------------------------------------------------------------------------
330  void configNode(scene::ISceneNode* node, const core::matrix4& global_mat,
331  const core::matrix4& inv_mat)
332  {
333  const core::matrix4 mat = inv_mat * global_mat;
334  const core::vector3df position = mat.getTranslation();
335  const core::vector3df rotation = mat.getRotationDegrees();
336  const core::vector3df scale = mat.getScale();
337  node->setPosition(position);
338  node->setRotation(rotation);
339  node->setScale(scale);
340  }
341 
342 public:
343  KartModel(bool is_master);
344  ~KartModel();
345  KartModel* makeCopy(std::shared_ptr<RenderInfo> ri);
346  void reset();
347  void loadInfo(const XMLNode &node);
348  bool loadModels(const KartProperties &kart_properties);
349  void setDefaultSuspension();
350  void update(float dt, float distance, float steer, float speed,
351  float current_lean_angle,
352  int gt_replay_index = -1);
353  void finishedRace();
354  void resetVisualWheelPosition();
355  scene::ISceneNode*
356  attachModel(bool animatedModels, bool human_player);
357  // ------------------------------------------------------------------------
359  scene::IAnimatedMesh*
360  getModel() const { return m_mesh; }
361 
362  // ------------------------------------------------------------------------
364  scene::IMesh* getWheelModel(const int i) const
365  { assert(i>=0 && i<4); return m_wheel_model[i]; }
366  // ------------------------------------------------------------------------
369  int getBaseFrame() const { return m_animation_frame[AF_STRAIGHT]; }
370  // ------------------------------------------------------------------------
371  int getFrame(AnimationFrameType f) const { return m_animation_frame[f]; }
372  // ------------------------------------------------------------------------
373  float getAnimationSpeed() const { return m_animation_speed; }
374  // ------------------------------------------------------------------------
378  const Vec3& getWheelGraphicsPosition(unsigned int i) const
379  {assert(i<4); return m_wheel_graphics_position[i];}
380  // ------------------------------------------------------------------------
384  {return m_wheel_graphics_position;}
385  // ------------------------------------------------------------------------
389  float getWheelGraphicsRadius(unsigned int i) const
390  {assert(i<4); return m_wheel_graphics_radius[i]; }
391  // ------------------------------------------------------------------------
395  const Vec3& getNitroEmittersPositon(unsigned int i) const
396  { assert(i<2); return m_nitro_emitter_position[i]; }
397  // ------------------------------------------------------------------------
399  const bool hasNitroEmitters() const
400  {return m_has_nitro_emitter;}
401  // ------------------------------------------------------------------------
404  {return m_speed_weighted_objects.size();}
405  // ------------------------------------------------------------------------
409  {return m_speed_weighted_objects[i];}
410  // ------------------------------------------------------------------------
412  float getLength () const {return m_kart_length; }
413  // ------------------------------------------------------------------------
415  float getWidth () const {return m_kart_width; }
416  // ------------------------------------------------------------------------
418  float getHeight () const {return m_kart_height; }
419  // ------------------------------------------------------------------------
421  float getHighestPoint () const { return m_kart_highest_point; }
422  // ------------------------------------------------------------------------
424  float getLowestPoint () const { return m_kart_lowest_point; }
425  // ------------------------------------------------------------------------
427  AnimationFrameType getAnimation() { return m_current_animation; }
428  // ------------------------------------------------------------------------
430  void setAnimation(AnimationFrameType type, bool play_non_loop = false);
431  // ------------------------------------------------------------------------
433  void setKart(AbstractKart* k) { m_kart = k; }
434  // ------------------------------------------------------------------------
436  void setHatMeshName(const std::string &name) {m_hat_name = name; }
437  // ------------------------------------------------------------------------
439  scene::ISceneNode** getWheelNodes() { return m_wheel_node; }
440  // ------------------------------------------------------------------------
441  scene::IAnimatedMeshSceneNode* getAnimatedNode(){ return m_animated_node; }
442  // ------------------------------------------------------------------------
443  std::shared_ptr<RenderInfo> getRenderInfo();
444  // ------------------------------------------------------------------------
445  bool supportColorization() const { return m_support_colorization; }
446  // ------------------------------------------------------------------------
447  void toggleHeadlights(bool on);
448  // ------------------------------------------------------------------------
449  const core::matrix4&
450  getInverseBoneMatrix(const std::string& bone_name) const;
451  // ------------------------------------------------------------------------
452  const std::string& getExhaustXML() const { return m_exhaust_xml; }
453  // ------------------------------------------------------------------------
454  bool hasWheel() const { return !m_wheel_filename[0].empty(); }
455  // ------------------------------------------------------------------------
456  const KartProperties* getKartProperties() const
457  { return m_kart_properties; }
458 }; // KartModel
459 #endif
static float UNDEFINED
Value used to indicate undefined entries.
Definition: kart_model.hpp:222
SpeedWeightedObjectList m_speed_weighted_objects
The speed weighted objects.
Definition: kart_model.hpp:249
Properties m_properties
Specific properties for this given speed-weighted object, otherwise just a copy of the values from th...
Definition: kart_model.hpp:86
std::shared_ptr< RenderInfo > m_render_info
For our engine to get the desired hue for colorization.
Definition: kart_model.hpp:310
AnimationFrameType m_current_animation
Which animation is currently being played.
Definition: kart_model.hpp:272
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:314
float getWidth() const
Returns the width of the kart model.
Definition: kart_model.hpp:415
const bool hasNitroEmitters() const
Returns true if kart has nitro emitters.
Definition: kart_model.hpp:399
A wrapper around bullets btVector3 to include conventient conversion functions (e.g.
Definition: vec3.hpp:34
float m_speed_factor
Speed factor: how much the kart speed affects the animation&#39;s speed (-1 to disable) ...
Definition: kart_model.hpp:55
core::matrix4 * m_hat_location
Location of hat in object space.
Definition: kart_model.hpp:213
size_t getSpeedWeightedObjectsCount() const
Returns the number of speed weighted objects for this kart.
Definition: kart_model.hpp:403
float m_kart_width
Width of kart.
Definition: kart_model.hpp:275
float m_kart_lowest_point
Smallest coordinate on up axis.
Definition: kart_model.hpp:287
std::string m_filename
The filename of the headlight model.
Definition: kart_model.hpp:97
A speed-weighted object is an object whose characteristics are influenced by the kart&#39;s speed...
Definition: kart_model.hpp:44
float m_animation_speed
Animation speed.
Definition: kart_model.hpp:202
float getWheelGraphicsRadius(unsigned int i) const
Returns the radius of the graphical wheels.
Definition: kart_model.hpp:389
std::string m_model_filename
Name of the 3d model file.
Definition: kart_model.hpp:225
Definition: three_d_animation.hpp:32
float m_kart_height
Height of kart.
Definition: kart_model.hpp:281
std::string m_hat_name
Name of the hat to use for this kart.
Definition: kart_model.hpp:219
core::vector2df m_texture_speed
Texture speed, in UV coordinates.
Definition: kart_model.hpp:58
const Vec3 & getWheelGraphicsPosition(unsigned int i) const
Returns the position of a wheel relative to the kart.
Definition: kart_model.hpp:378
AbstractKart * m_kart
Pointer to the kart object belonging to this kart model.
Definition: kart_model.hpp:307
void setModel(scene::IMesh *mesh)
Sets the mesh for this headlight object.
Definition: kart_model.hpp:137
scene::IAnimatedMesh * getModel() const
Returns the animated mesh of this kart model.
Definition: kart_model.hpp:360
float m_kart_highest_point
Largest coordinate on up axis.
Definition: kart_model.hpp:284
bool m_has_nitro_emitter
True if kart has nitro emitters.
Definition: kart_model.hpp:246
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
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
scene::IMesh * m_model
The mesh for the headlight.
Definition: kart_model.hpp:104
This class stores a 3D kart model.
Definition: kart_model.hpp:167
float getHeight() const
Returns the height of the kart.
Definition: kart_model.hpp:418
video::SColor m_headlight_color
The color of the real light.
Definition: kart_model.hpp:110
scene::IAnimatedMeshSceneNode * m_node
The scene node the speed weighted model is attached to.
Definition: kart_model.hpp:69
void setHatMeshName(const std::string &name)
Name of the hat mesh to use.
Definition: kart_model.hpp:436
std::string m_name
Filename of the "speed weighted" object.
Definition: kart_model.hpp:76
scene::IAnimatedMeshSceneNode * m_animated_node
This is a pointer to the scene node of the kart this model belongs to.
Definition: kart_model.hpp:210
core::vector2df m_texture_cur_offset
Current uv translation in the texture matrix for speed-weighted texture animations.
Definition: kart_model.hpp:82
This class stores the properties of a kart.
Definition: kart_properties.hpp:59
std::string m_exhaust_xml
Exhaust particle file (xml) for the kart, empty if disabled.
Definition: kart_model.hpp:324
utility class used to parse XML files
Definition: xml_node.hpp:47
const Vec3 * getWheelsGraphicsPosition() const
Returns the position of wheels relative to the kart.
Definition: kart_model.hpp:383
scene::IAnimatedMesh * m_model
Model.
Definition: kart_model.hpp:66
float m_kart_length
Length of kart.
Definition: kart_model.hpp:278
float getLength() const
Returns the length of the kart model.
Definition: kart_model.hpp:412
const Vec3 & getNitroEmittersPositon(unsigned int i) const
Returns the position of nitro emitter relative to the kart.
Definition: kart_model.hpp:395
scene::IMesh * getWheelModel(const int i) const
Returns the mesh of the wheel for this kart.
Definition: kart_model.hpp:364
core::matrix4 m_location
The relative matrix to the parent kart scene node where the headlight mesh is attached to...
Definition: kart_model.hpp:101
Definition: render_info.hpp:27
float getLowestPoint() const
Lowest coordinate on up axis.
Definition: kart_model.hpp:424
std::string m_bone_name
Attach to which bone in kart model if not empty.
Definition: kart_model.hpp:113
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:318
float m_strength_factor
Strength factor: how much the kart speed affects the animation&#39;s distance from a static pose (-1 to d...
Definition: kart_model.hpp:52
AnimationFrameType getAnimation()
Returns information about currently played animation.
Definition: kart_model.hpp:427
void setKart(AbstractKart *k)
Sets the kart this model is currently used for.
Definition: kart_model.hpp:433
float getHighestPoint() const
Highest coordinate on up axis.
Definition: kart_model.hpp:421
const SpeedWeightedObject & getSpeedWeightedObject(int i) const
Returns the position of a speed weighted object relative to the kart.
Definition: kart_model.hpp:408
std::string m_bone_name
Attach to which bone in kart model if not empty.
Definition: kart_model.hpp:79
scene::IAnimatedMesh * m_mesh
The mesh of the model.
Definition: kart_model.hpp:205
unsigned m_version
Version of kart model (in kart.xml).
Definition: kart_model.hpp:321
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:61
bool m_is_master
True if this is the master copy, managed by KartProperties.
Definition: kart_model.hpp:292
scene::ISceneNode ** getWheelNodes()
Returns the array of wheel nodes.
Definition: kart_model.hpp:439
std::string m_hat_bone
Name of the bone for hat attachment.
Definition: kart_model.hpp:216
scene::ISceneNode * m_node
The scene node of the headlight (real light).
Definition: kart_model.hpp:107
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:73
A class to store the headlights of a kart.
Definition: kart_model.hpp:93
Parameters for a speed-weighted object.
Definition: kart_model.hpp:47
int getBaseFrame() const
Since karts might be animated, we might need to know which base frame to use.
Definition: kart_model.hpp:369