SuperTuxKart
Loading...
Searching...
No Matches
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>
28namespace irr
29{
30 namespace scene { class IAnimatedMesh; class IMesh;
31 class ISceneNode; class IMeshSceneNode; }
32}
33using namespace irr;
34
35#include "utils/no_copy.hpp"
36#include "utils/vec3.hpp"
37
38class AbstractKart;
39class KartProperties;
40namespace GE { class GERenderInfo; }
41class XMLNode;
42
45{
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};
88typedef std::vector<SpeedWeightedObject> SpeedWeightedObjectList;
89
90// ============================================================================
94{
95private:
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
115public:
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
167class KartModel : public scene::IAnimationEndCallBack, public NoCopy
168{
169public:
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
197private:
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
238
241
244
247
249 SpeedWeightedObjectList m_speed_weighted_objects;
250
251 std::vector<HeadlightObject> m_headlight_objects;
252
255
259
264
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<GE::GERenderInfo> 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 // ------------------------------------------------------------------------
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
342public:
343 KartModel(bool is_master);
344 ~KartModel();
345 KartModel* makeCopy(std::shared_ptr<GE::GERenderInfo> 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();
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 // ------------------------------------------------------------------------
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<GE::GERenderInfo> 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
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:94
video::SColor m_headlight_color
The color of the real light.
Definition: kart_model.hpp:110
scene::IMesh * m_model
The mesh for the headlight.
Definition: kart_model.hpp:104
std::string m_filename
The filename of the headlight model.
Definition: kart_model.hpp:97
void setLight(scene::ISceneNode *parent, float energy, float radius)
Add a light node emitted from the center mass the headlight.
Definition: kart_model.cpp:554
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
std::string m_bone_name
Attach to which bone in kart model if not empty.
Definition: kart_model.hpp:113
scene::ISceneNode * m_node
The scene node of the headlight (real light).
Definition: kart_model.hpp:107
void setModel(scene::IMesh *mesh)
Sets the mesh for this headlight object.
Definition: kart_model.hpp:137
This class stores a 3D kart model.
Definition: kart_model.hpp:168
const Vec3 * getWheelsGraphicsPosition() const
Returns the position of wheels relative to the kart.
Definition: kart_model.hpp:383
AnimationFrameType m_current_animation
Which animation is currently being played.
Definition: kart_model.hpp:272
void loadNitroEmitterInfo(const XMLNode &node, const std::string &emitter_name, int index)
Loads a single nitro emitter node.
Definition: kart_model.cpp:765
std::string m_model_filename
Name of the 3d model file.
Definition: kart_model.hpp:225
Vec3 m_wheel_graphics_position[4]
The position of all four wheels in the 3d model.
Definition: kart_model.hpp:237
void reset()
Resets the kart model.
Definition: kart_model.cpp:874
const SpeedWeightedObject & getSpeedWeightedObject(int i) const
Returns the position of a speed weighted object relative to the kart.
Definition: kart_model.hpp:408
int getBaseFrame() const
Since karts might be animated, we might need to know which base frame to use.
Definition: kart_model.hpp:369
float m_default_physics_suspension[4]
Length of the physics suspension when the kart is at rest.
Definition: kart_model.hpp:254
float m_kart_width
Width of kart.
Definition: kart_model.hpp:275
void initInverseBoneMatrices()
Called when a kart is load.
Definition: kart_model.cpp:1290
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
void update(float dt, float distance, float steer, float speed, float current_lean_angle, int gt_replay_index=-1)
Rotates and turns the wheels appropriately, and adjust for suspension updates the speed-weighted obje...
Definition: kart_model.cpp:1083
float getWheelGraphicsRadius(unsigned int i) const
Returns the radius of the graphical wheels.
Definition: kart_model.hpp:389
core::matrix4 * m_hat_location
Location of hat in object space.
Definition: kart_model.hpp:213
SpeedWeightedObjectList m_speed_weighted_objects
The speed weighted objects.
Definition: kart_model.hpp:249
scene::IMesh * getWheelModel(const int i) const
Returns the mesh of the wheel for this kart.
Definition: kart_model.hpp:364
float getHeight() const
Returns the height of the kart.
Definition: kart_model.hpp:418
float m_dampen_suspension_amplitude[4]
value used to divide the visual movement of wheels (because the actual movement of wheels in bullet i...
Definition: kart_model.hpp:267
unsigned m_version
Version of kart model (in kart.xml).
Definition: kart_model.hpp:321
bool loadModels(const KartProperties &kart_properties)
Loads the 3d model and all wheels.
Definition: kart_model.cpp:567
int m_animation_frame[AF_COUNT]
Which frame number starts/end which animation.
Definition: kart_model.hpp:199
AbstractKart * m_kart
Pointer to the kart object belonging to this kart model.
Definition: kart_model.hpp:307
size_t getSpeedWeightedObjectsCount() const
Returns the number of speed weighted objects for this kart.
Definition: kart_model.hpp:403
float getWidth() const
Returns the width of the kart model.
Definition: kart_model.hpp:415
scene::IAnimatedMesh * getModel() const
Returns the animated mesh of this kart model.
Definition: kart_model.hpp:360
std::string m_wheel_filename[4]
Filename of the wheel models.
Definition: kart_model.hpp:234
bool m_has_nitro_emitter
True if kart has nitro emitters.
Definition: kart_model.hpp:246
float m_kart_lowest_point
Smallest coordinate on up axis.
Definition: kart_model.hpp:287
bool m_is_master
True if this is the master copy, managed by KartProperties.
Definition: kart_model.hpp:292
float m_kart_height
Height of kart.
Definition: kart_model.hpp:281
std::shared_ptr< GE::GERenderInfo > m_render_info
For our engine to get the desired hue for colorization.
Definition: kart_model.hpp:310
const bool hasNitroEmitters() const
Returns true if kart has nitro emitters.
Definition: kart_model.hpp:399
void resetVisualWheelPosition()
Called when a kart is rescued to reset all visual wheels to their default position to avoid that some...
Definition: kart_model.cpp:1253
float m_kart_length
Length of kart.
Definition: kart_model.hpp:278
AnimationFrameType getAnimation()
Returns information about currently played animation.
Definition: kart_model.hpp:427
float m_min_suspension[4]
Minimum suspension length (i.e.
Definition: kart_model.hpp:258
float getHighestPoint() const
Highest coordinate on up axis.
Definition: kart_model.hpp:421
void setAnimation(AnimationFrameType type, bool play_non_loop=false)
Enables- or disables the end animation.
Definition: kart_model.cpp:914
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
void setHatMeshName(const std::string &name)
Name of the hat mesh to use.
Definition: kart_model.hpp:436
void loadWheelInfo(const XMLNode &node, const std::string &wheel_name, int index)
Loads a single wheel node.
Definition: kart_model.cpp:822
scene::IMesh * m_wheel_model[4]
The four wheel models.
Definition: kart_model.hpp:228
float m_max_suspension[4]
Maximum suspension length (i.e.
Definition: kart_model.hpp:263
float getLength() const
Returns the length of the kart model.
Definition: kart_model.hpp:412
Vec3 m_nitro_emitter_position[2]
The position of the nitro emitters.
Definition: kart_model.hpp:243
static float UNDEFINED
Value used to indicate undefined entries.
Definition: kart_model.hpp:222
float m_wheel_graphics_radius[4]
Radius of the graphical wheels.
Definition: kart_model.hpp:240
void loadInfo(const XMLNode &node)
This function loads the information about the kart from a xml file.
Definition: kart_model.cpp:145
KartModel * makeCopy(std::shared_ptr< GE::GERenderInfo > ri)
This function returns a copy of this object.
Definition: kart_model.cpp:322
void setKart(AbstractKart *k)
Sets the kart this model is currently used for.
Definition: kart_model.hpp:433
std::string m_hat_bone
Name of the bone for hat attachment.
Definition: kart_model.hpp:216
void finishedRace()
Called when the kart finished the race.
Definition: kart_model.cpp:902
scene::ISceneNode * m_wheel_node[4]
The four scene nodes the wheels are attached to.
Definition: kart_model.hpp:231
void OnAnimationEnd(scene::IAnimatedMeshSceneNode *node)
Called from irrlicht when a non-looped animation ends.
Definition: kart_model.cpp:1024
const Vec3 & getWheelGraphicsPosition(unsigned int i) const
Returns the position of a wheel relative to the kart.
Definition: kart_model.hpp:378
void loadSpeedWeightedInfo(const XMLNode *speed_weighted_node)
Loads a single speed weighted node.
Definition: kart_model.cpp:787
std::string m_hat_name
Name of the hat to use for this kart.
Definition: kart_model.hpp:219
float m_kart_highest_point
Largest coordinate on up axis.
Definition: kart_model.hpp:284
float getLowestPoint() const
Lowest coordinate on up axis.
Definition: kart_model.hpp:424
scene::ISceneNode ** getWheelNodes()
Returns the array of wheel nodes.
Definition: kart_model.hpp:439
scene::IAnimatedMesh * m_mesh
The mesh of the model.
Definition: kart_model.hpp:205
const Vec3 & getNitroEmittersPositon(unsigned int i) const
Returns the position of nitro emitter relative to the kart.
Definition: kart_model.hpp:395
scene::ISceneNode * attachModel(bool animatedModels, bool human_player)
Attach the kart model and wheels to the scene node.
Definition: kart_model.cpp:400
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
float m_animation_speed
Animation speed.
Definition: kart_model.hpp:202
std::string m_exhaust_xml
Exhaust particle file (xml) for the kart, empty if disabled.
Definition: kart_model.hpp:324
~KartModel()
Destructor.
Definition: kart_model.cpp:233
This class stores the properties of a kart.
Definition: kart_properties.hpp:60
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
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
Parameters for a speed-weighted object.
Definition: kart_model.hpp:48
core::vector2df m_texture_speed
Texture speed, in UV coordinates.
Definition: kart_model.hpp:58
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
float m_speed_factor
Speed factor: how much the kart speed affects the animation's speed (-1 to disable)
Definition: kart_model.hpp:55
A speed-weighted object is an object whose characteristics are influenced by the kart's speed.
Definition: kart_model.hpp:45
std::string m_name
Filename of the "speed weighted" object.
Definition: kart_model.hpp:76
scene::IAnimatedMesh * m_model
Model.
Definition: kart_model.hpp:66
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
std::string m_bone_name
Attach to which bone in kart model if not empty.
Definition: kart_model.hpp:79
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
core::vector2df m_texture_cur_offset
Current uv translation in the texture matrix for speed-weighted texture animations.
Definition: kart_model.hpp:82
scene::IAnimatedMeshSceneNode * m_node
The scene node the speed weighted model is attached to.
Definition: kart_model.hpp:69