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_BEGIN_EXPLOSION, // Begin explosion animation
180  AF_END_EXPLOSION, // End explosion animation
181  AF_JUMP_START, // Begin of jump
182  AF_JUMP_LOOP, // Begin of jump loop
183  AF_JUMP_END, // End of jump
184  AF_WIN_START, // Begin of win animation
185  AF_WIN_LOOP_START, // Begin of win loop animation
186  AF_WIN_END, // End of win animation
187  AF_SELECTION_START, // Start frame in kart selection screen
188  AF_SELECTION_END, // End frame in kart selection screen
189  AF_BACK_LEFT, // Going back left
190  AF_BACK_STRAIGHT, // Going back straight
191  AF_BACK_RIGHT, // Going back right
192  AF_END=AF_BACK_RIGHT, // Last animation frame
193  AF_COUNT}; // Number of entries here
194 
195 private:
197  int m_animation_frame[AF_COUNT];
198 
201 
203  scene::IAnimatedMesh *m_mesh;
204 
208  scene::IAnimatedMeshSceneNode *m_animated_node;
209 
211  core::matrix4* m_hat_location;
212 
214  std::string m_hat_bone;
215 
217  std::string m_hat_name;
218 
220  static float UNDEFINED;
221 
223  std::string m_model_filename;
224 
226  scene::IMesh *m_wheel_model[4];
227 
229  scene::ISceneNode *m_wheel_node[4];
230 
232  std::string m_wheel_filename[4];
233 
235  Vec3 m_wheel_graphics_position[4];
236 
238  float m_wheel_graphics_radius[4];
239 
241  Vec3 m_nitro_emitter_position[2];
242 
245 
247  SpeedWeightedObjectList m_speed_weighted_objects;
248 
249  std::vector<HeadlightObject> m_headlight_objects;
250 
252  float m_default_physics_suspension[4];
253 
256  float m_min_suspension[4];
257 
261  float m_max_suspension[4];
262 
265  float m_dampen_suspension_amplitude[4];
266 
270  AnimationFrameType m_current_animation;
271 
274 
277 
280 
283 
286 
291 
296 
297  void loadWheelInfo(const XMLNode &node,
298  const std::string &wheel_name, int index);
299 
300  void loadNitroEmitterInfo(const XMLNode &node,
301  const std::string &emitter_name, int index);
302 
303  void loadSpeedWeightedInfo(const XMLNode* speed_weighted_node);
304 
305  void loadHeadlights(const XMLNode &node);
306 
307  void OnAnimationEnd(scene::IAnimatedMeshSceneNode *node);
308 
311 
313  std::shared_ptr<RenderInfo> m_render_info;
314 
318 
321  std::unordered_map<std::string, core::matrix4> m_inverse_bone_matrices;
322 
324  unsigned m_version;
325 
327  std::string m_exhaust_xml;
328 
329  // ------------------------------------------------------------------------
330  void initInverseBoneMatrices();
331  // ------------------------------------------------------------------------
332  void configNode(scene::ISceneNode* node, const core::matrix4& global_mat,
333  const core::matrix4& inv_mat)
334  {
335  const core::matrix4 mat = inv_mat * global_mat;
336  const core::vector3df position = mat.getTranslation();
337  const core::vector3df rotation = mat.getRotationDegrees();
338  const core::vector3df scale = mat.getScale();
339  node->setPosition(position);
340  node->setRotation(rotation);
341  node->setScale(scale);
342  }
343 
344 public:
345  KartModel(bool is_master);
346  ~KartModel();
347  KartModel* makeCopy(std::shared_ptr<RenderInfo> ri);
348  void reset();
349  void loadInfo(const XMLNode &node);
350  bool loadModels(const KartProperties &kart_properties);
351  void setDefaultSuspension();
352  void update(float dt, float distance, float steer, float speed,
353  float current_lean_angle,
354  int gt_replay_index = -1);
355  void finishedRace();
356  void resetVisualWheelPosition();
357  scene::ISceneNode*
358  attachModel(bool animatedModels, bool human_player);
359  // ------------------------------------------------------------------------
361  scene::IAnimatedMesh*
362  getModel() const { return m_mesh; }
363 
364  // ------------------------------------------------------------------------
366  scene::IMesh* getWheelModel(const int i) const
367  { assert(i>=0 && i<4); return m_wheel_model[i]; }
368  // ------------------------------------------------------------------------
371  int getBaseFrame() const { return m_animation_frame[AF_STRAIGHT]; }
372  // ------------------------------------------------------------------------
373  int getFrame(AnimationFrameType f) const { return m_animation_frame[f]; }
374  // ------------------------------------------------------------------------
375  float getAnimationSpeed() const { return m_animation_speed; }
376  // ------------------------------------------------------------------------
380  const Vec3& getWheelGraphicsPosition(unsigned int i) const
381  {assert(i<4); return m_wheel_graphics_position[i];}
382  // ------------------------------------------------------------------------
386  {return m_wheel_graphics_position;}
387  // ------------------------------------------------------------------------
391  float getWheelGraphicsRadius(unsigned int i) const
392  {assert(i<4); return m_wheel_graphics_radius[i]; }
393  // ------------------------------------------------------------------------
397  const Vec3& getNitroEmittersPositon(unsigned int i) const
398  { assert(i<2); return m_nitro_emitter_position[i]; }
399  // ------------------------------------------------------------------------
401  const bool hasNitroEmitters() const
402  {return m_has_nitro_emitter;}
403  // ------------------------------------------------------------------------
406  {return m_speed_weighted_objects.size();}
407  // ------------------------------------------------------------------------
411  {return m_speed_weighted_objects[i];}
412  // ------------------------------------------------------------------------
414  float getLength () const {return m_kart_length; }
415  // ------------------------------------------------------------------------
417  float getWidth () const {return m_kart_width; }
418  // ------------------------------------------------------------------------
420  float getHeight () const {return m_kart_height; }
421  // ------------------------------------------------------------------------
423  float getHighestPoint () const { return m_kart_highest_point; }
424  // ------------------------------------------------------------------------
426  float getLowestPoint () const { return m_kart_lowest_point; }
427  // ------------------------------------------------------------------------
429  AnimationFrameType getAnimation() { return m_current_animation; }
430  // ------------------------------------------------------------------------
432  void setAnimation(AnimationFrameType type, bool play_non_loop = false);
433  // ------------------------------------------------------------------------
435  void setKart(AbstractKart* k) { m_kart = k; }
436  // ------------------------------------------------------------------------
438  void setHatMeshName(const std::string &name) {m_hat_name = name; }
439  // ------------------------------------------------------------------------
441  scene::ISceneNode** getWheelNodes() { return m_wheel_node; }
442  // ------------------------------------------------------------------------
443  scene::IAnimatedMeshSceneNode* getAnimatedNode(){ return m_animated_node; }
444  // ------------------------------------------------------------------------
445  std::shared_ptr<RenderInfo> getRenderInfo();
446  // ------------------------------------------------------------------------
447  bool supportColorization() const { return m_support_colorization; }
448  // ------------------------------------------------------------------------
449  void toggleHeadlights(bool on);
450  // ------------------------------------------------------------------------
451  const core::matrix4&
452  getInverseBoneMatrix(const std::string& bone_name) const;
453  // ------------------------------------------------------------------------
454  const std::string& getExhaustXML() const { return m_exhaust_xml; }
455 
456 }; // KartModel
457 #endif
static float UNDEFINED
Value used to indicate undefined entries.
Definition: kart_model.hpp:220
SpeedWeightedObjectList m_speed_weighted_objects
The speed weighted objects.
Definition: kart_model.hpp:247
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:313
bool m_play_non_loop
True if the animation played is non-loop, which will reset to AF_DEFAULT after first loop ends.
Definition: kart_model.hpp:295
AnimationFrameType m_current_animation
Which animation is currently being played.
Definition: kart_model.hpp:270
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:317
float getWidth() const
Returns the width of the kart model.
Definition: kart_model.hpp:417
const bool hasNitroEmitters() const
Returns true if kart has nitro emitters.
Definition: kart_model.hpp:401
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:34
float m_speed_factor
Speed factor: how much the kart speed affects the animation'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:211
size_t getSpeedWeightedObjectsCount() const
Returns the number of speed weighted objects for this kart.
Definition: kart_model.hpp:405
float m_kart_width
Width of kart.
Definition: kart_model.hpp:273
float m_kart_lowest_point
Smallest coordinate on up axis.
Definition: kart_model.hpp:285
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's speed.
Definition: kart_model.hpp:44
float m_animation_speed
Animation speed.
Definition: kart_model.hpp:200
float getWheelGraphicsRadius(unsigned int i) const
Returns the radius of the graphical wheels.
Definition: kart_model.hpp:391
std::string m_model_filename
Name of the 3d model file.
Definition: kart_model.hpp:223
float m_kart_height
Height of kart.
Definition: kart_model.hpp:279
std::string m_hat_name
Name of the hat to use for this kart.
Definition: kart_model.hpp:217
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:380
AbstractKart * m_kart
Pointer to the kart object belonging to this kart model.
Definition: kart_model.hpp:310
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:362
float m_kart_highest_point
Largest coordinate on up axis.
Definition: kart_model.hpp:282
bool m_has_nitro_emitter
True if kart has nitro emitters.
Definition: kart_model.hpp:244
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:109
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:420
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:438
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:208
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:60
std::string m_exhaust_xml
Exhaust particle file (xml) for the kart, empty if disabled.
Definition: kart_model.hpp:327
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:385
scene::IAnimatedMesh * m_model
Model.
Definition: kart_model.hpp:66
float m_kart_length
Length of kart.
Definition: kart_model.hpp:276
float getLength() const
Returns the length of the kart model.
Definition: kart_model.hpp:414
const Vec3 & getNitroEmittersPositon(unsigned int i) const
Returns the position of nitro emitter relative to the kart.
Definition: kart_model.hpp:397
scene::IMesh * getWheelModel(const int i) const
Returns the mesh of the wheel for this kart.
Definition: kart_model.hpp:366
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:426
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:321
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:52
AnimationFrameType getAnimation()
Returns information about currently played animation.
Definition: kart_model.hpp:429
void setKart(AbstractKart *k)
Sets the kart this model is currently used for.
Definition: kart_model.hpp:435
float getHighestPoint() const
Highest coordinate on up axis.
Definition: kart_model.hpp:423
const SpeedWeightedObject & getSpeedWeightedObject(int i) const
Returns the position of a speed weighted object relative to the kart.
Definition: kart_model.hpp:410
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:203
unsigned m_version
Version of kart model (in kart.xml).
Definition: kart_model.hpp:324
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:290
scene::ISceneNode ** getWheelNodes()
Returns the array of wheel nodes.
Definition: kart_model.hpp:441
std::string m_hat_bone
Name of the bone for hat attachment.
Definition: kart_model.hpp:214
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:371