SuperTuxKart
physical_object.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2006-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_PHYSICAL_OBJECT_HPP
20 #define HEADER_PHYSICAL_OBJECT_HPP
21 
22 #include <string>
23 
24 #include "btBulletDynamicsCommon.h"
25 
26 #include "network/rewinder.hpp"
28 #include "physics/user_pointer.hpp"
29 #include "utils/vec3.hpp"
30 
31 class Material;
32 class TrackObject;
33 class XMLNode;
34 
38 class PhysicalObject : public Rewinder,
39  public SmoothNetworkBody
40 {
41 public:
43  enum BodyTypes {MP_NONE,
44  MP_CONE_Y, MP_CONE_X, MP_CONE_Z,
45  MP_CYLINDER_Y, MP_CYLINDER_X, MP_CYLINDER_Z,
46  MP_BOX, MP_SPHERE, MP_EXACT};
47 
48  class Settings
49  {
50  public:
52  std::string m_id;
54  float m_mass;
56  float m_radius;
58  float m_height;
64  float m_friction;
73 
89  std::string m_on_kart_collision;
93  std::string m_on_item_collision;
94  private:
95  void init();
96  public:
97  Settings(BodyTypes type, float radius, float mass);
98  Settings(const XMLNode &xml_node);
99  }; // Settings
100 
101 private:
102 
104  core::vector3df m_init_xyz;
105 
107  core::vector3df m_init_hpr;
108 
110  core::vector3df m_init_scale;
111 
112  TrackObject *m_object;
113 
116 
118  btCollisionShape *m_shape;
119 
121  std::string m_id;
122 
124  btRigidBody *m_body;
125 
127  btDefaultMotionState *m_motion_state;
128 
130  float m_mass;
131 
132  bool m_body_added;
133 
137 
139  btTransform m_init_pos;
140 
143  btTransform m_current_transform;
144 
149 
156  float m_radius;
157 
160 
163 
164  bool m_flatten_kart;
165 
169 
176  std::string m_on_kart_collision;
180  std::string m_on_item_collision;
185 
188 
189  /* Last transform and velocities recieved or saved for networking */
190  btTransform m_last_transform;
191  Vec3 m_last_lv;
192  Vec3 m_last_av;
193 
194  /* Used to determine if local state should be used, which is true
195  * when the object is not moving */
196  bool m_no_server_state;
197 
198  void copyFromMainProcess(TrackObject* track_obj);
199 public:
200  PhysicalObject(bool is_dynamic, const Settings& settings,
201  TrackObject* object);
202 
203  static std::shared_ptr<PhysicalObject> fromXML
204  (bool is_dynamic, const XMLNode &node, TrackObject* object);
205 
206  virtual ~PhysicalObject ();
207  virtual void reset ();
208  virtual void handleExplosion(const Vec3& pos, bool directHit);
209  void update (float dt);
210  void updateGraphics (float dt);
211  void init (const Settings &settings);
212  void move (const Vec3& xyz, const core::vector3df& hpr);
213  void hit (const Material *m, const Vec3 &normal);
214  bool isSoccerBall () const;
215  bool castRay(const btVector3 &from,
216  const btVector3 &to, btVector3 *hit_point,
217  const Material **material, btVector3 *normal,
218  bool interpolate_normal) const;
219 
220  // ------------------------------------------------------------------------
221  bool isDynamic() const { return m_is_dynamic; }
222  // ------------------------------------------------------------------------
224  std::string getID() { return m_id; }
225  // ------------------------------------------------------------------------
226  btDefaultMotionState* getMotionState() const { return m_motion_state; }
227  // ------------------------------------------------------------------------
229  btRigidBody* getBody() const { return m_body; }
230  // ------------------------------------------------------------------------
233  bool isCrashReset() const { return m_crash_reset; }
234  // ------------------------------------------------------------------------
237  bool isExplodeKartObject () const { return m_explode_kart; }
238  // ------------------------------------------------------------------------
240  void setInteraction(std::string interaction);
241  // ------------------------------------------------------------------------
243  void removeBody();
244  // ------------------------------------------------------------------------
246  void addBody();
247  // ------------------------------------------------------------------------
248  float getRadius() const { return m_radius; }
249  // ------------------------------------------------------------------------
250  const std::string& getOnKartCollisionFunction() const { return m_on_kart_collision; }
251  // ------------------------------------------------------------------------
252  const std::string& getOnItemCollisionFunction() const { return m_on_item_collision; }
253  // ------------------------------------------------------------------------
254  TrackObject* getTrackObject() { return m_object; }
255 
256  // Methods usable by scripts
257 
269  bool isFlattenKartObject() const { return m_flatten_kart; }
270  void disable(void *memory)
271  {
272  ((PhysicalObject*)(memory))->removeBody();
273  }
274 
275  //enables track object passed from the script
276  void enable(void *memory)
277  {
278  ((PhysicalObject*)(memory))->addBody();
279  }
284  void addForRewind();
285  virtual void saveTransform();
286  virtual void computeError();
287  virtual BareNetworkString* saveState(std::vector<std::string>* ru);
288  virtual void undoEvent(BareNetworkString *buffer) {}
289  virtual void rewindToEvent(BareNetworkString *buffer) {}
290  virtual void restoreState(BareNetworkString *buffer, int count);
291  virtual void undoState(BareNetworkString *buffer) {}
292  virtual std::function<void()> getLocalStateRestoreFunction();
293  bool hasTriangleMesh() const { return m_triangle_mesh != NULL; }
294  void joinToMainTrack();
295  std::shared_ptr<PhysicalObject> clone(TrackObject* track_obj)
296  {
297  PhysicalObject* obj = new PhysicalObject(*this);
298  obj->copyFromMainProcess(track_obj);
299  return std::shared_ptr<PhysicalObject>(obj);
300  }
301 }; // PhysicalObject
302 
303 #endif
304 /* EOF */
305 
PhysicalObject::Settings::m_restitution
float m_restitution
Restitution of the physical object.
Definition: physical_object.hpp:62
PhysicalObject::m_is_dynamic
bool m_is_dynamic
If this body is a bullet dynamic body, i.e.
Definition: physical_object.hpp:184
PhysicalObject::computeError
virtual void computeError()
Called when a rewind is finished, and is used to compute the error caused by the rewind (which is the...
Definition: physical_object.cpp:808
PhysicalObject::Settings::init
void init()
Initialises a Settings object.
Definition: physical_object.cpp:109
PhysicalObject::m_on_kart_collision
std::string m_on_kart_collision
If non-empty, the name of the scripting function to call when a kart collides with this object.
Definition: physical_object.hpp:176
PhysicalObject::m_mass
float m_mass
The mass of this object.
Definition: physical_object.hpp:130
PhysicalObject::rewindToEvent
virtual void rewindToEvent(BareNetworkString *buffer)
Called when an event needs to be replayed.
Definition: physical_object.hpp:289
PhysicalObject::m_user_pointer
UserPointer m_user_pointer
The pointer that is stored in the bullet rigid body back to this object.
Definition: physical_object.hpp:136
PhysicalObject::Settings::m_knock_kart
bool m_knock_kart
Knock the kart around.
Definition: physical_object.hpp:77
PhysicalObject::m_init_pos
btTransform m_init_pos
This is the initial position of the object for the physics.
Definition: physical_object.hpp:139
PhysicalObject::setInteraction
void setInteraction(std::string interaction)
Sets the interaction type.
Definition: physical_object.cpp:734
BareNetworkString
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
TriangleMesh
A special class to store a triangle mesh with a separate material per triangle.
Definition: triangle_mesh.hpp:35
PhysicalObject::m_body
btRigidBody * m_body
The corresponding bullet rigid body.
Definition: physical_object.hpp:124
PhysicalObject::Settings::m_on_kart_collision
std::string m_on_kart_collision
If non-empty, the name of the scripting function to call when a kart collides with this object.
Definition: physical_object.hpp:89
Material
Definition: material.hpp:47
PhysicalObject::reset
virtual void reset()
Nothing to do here.
Definition: physical_object.cpp:682
PhysicalObject::Settings::m_angular_damping
float m_angular_damping
Bullet's angular damping factor.
Definition: physical_object.hpp:72
PhysicalObject::m_radius
float m_radius
Radius of the object - this obviously depends on the actual shape.
Definition: physical_object.hpp:156
PhysicalObject::undoEvent
virtual void undoEvent(BareNetworkString *buffer)
Called when an event needs to be undone.
Definition: physical_object.hpp:288
PhysicalObject::m_crash_reset
bool m_crash_reset
True if a kart colliding with this object should be rescued.
Definition: physical_object.hpp:159
PhysicalObject::m_motion_state
btDefaultMotionState * m_motion_state
Bullet's motion state for this object.
Definition: physical_object.hpp:127
PhysicalObject::m_triangle_mesh
TriangleMesh * m_triangle_mesh
Non-null only if the shape is exact.
Definition: physical_object.hpp:187
PhysicalObject::Settings::m_mass
float m_mass
Mass of the object.
Definition: physical_object.hpp:54
PhysicalObject::castRay
bool castRay(const btVector3 &from, const btVector3 &to, btVector3 *hit_point, const Material **material, btVector3 *normal, bool interpolate_normal) const
Does a raycast against this physical object.
Definition: physical_object.cpp:666
PhysicalObject::isCrashReset
bool isCrashReset() const
Returns true if this object should trigger a rescue in a kart that hits it.
Definition: physical_object.hpp:233
PhysicalObject::Settings
Definition: physical_object.hpp:49
PhysicalObject::m_reset_when_too_low
bool m_reset_when_too_low
True if object should be reset to its initial position if it's too low (see m_reset_height).
Definition: physical_object.hpp:168
UserPointer
A UserPointer is stored as a user pointer in all bullet bodies.
Definition: user_pointer.hpp:36
PhysicalObject::Settings::Settings
Settings(BodyTypes type, float radius, float mass)
Creates a physical Settings object with the given type, radius and mass.
Definition: physical_object.cpp:53
PhysicalObject::isExplodeKartObject
bool isExplodeKartObject() const
Returns true if this object should cause an explosion if a kart hits it.
Definition: physical_object.hpp:237
PhysicalObject::Settings::m_height
float m_height
Height of an object, to overwrite the graphical dimension.
Definition: physical_object.hpp:58
PhysicalObject::m_explode_kart
bool m_explode_kart
True if kart should "explode" when touching this.
Definition: physical_object.hpp:162
PhysicalObject::saveTransform
virtual void saveTransform()
Called before a rewind.
Definition: physical_object.cpp:800
PhysicalObject
Definition: physical_object.hpp:40
PhysicalObject::undoState
virtual void undoState(BareNetworkString *buffer)
Undo the effects of the given state, but do not rewind to that state (which is done by rewindTo).
Definition: physical_object.hpp:291
PhysicalObject::isSoccerBall
bool isSoccerBall() const
Returns true if this object is a soccer ball.
Definition: physical_object.cpp:727
PhysicalObject::m_on_item_collision
std::string m_on_item_collision
If non-empty, the name of the scripting function to call when a (flyable) item collides with this obj...
Definition: physical_object.hpp:180
PhysicalObject::updateGraphics
void updateGraphics(float dt)
This updates all only graphical elements.
Definition: physical_object.cpp:606
PhysicalObject::Settings::m_flatten_kart
bool m_flatten_kart
Flatten the kart when this object is touched.
Definition: physical_object.hpp:79
PhysicalObject::Settings::m_linear_damping
float m_linear_damping
Bullet's linear damping factor.
Definition: physical_object.hpp:70
PhysicalObject::m_init_hpr
core::vector3df m_init_hpr
The initial hpr of the object.
Definition: physical_object.hpp:107
PhysicalObject::getBody
btRigidBody * getBody() const
Returns the rigid body of this physical object.
Definition: physical_object.hpp:229
PhysicalObject::init
void init(const Settings &settings)
Additional initialisation after loading of the model is finished.
Definition: physical_object.cpp:234
PhysicalObject::m_reset_height
float m_reset_height
If m_reset_when_too_low this object is set back to its start position if its height is below this val...
Definition: physical_object.hpp:172
TrackObject
This is a base object for any separate object on the track, which might also have a skeletal animatio...
Definition: track_object.hpp:46
PhysicalObject::BodyTypes
BodyTypes
The supported collision shapes.
Definition: physical_object.hpp:43
PhysicalObject::Settings::m_reset_when_too_low
bool m_reset_when_too_low
Reset the object when it falls under the track (useful e.g.
Definition: physical_object.hpp:82
SmoothNetworkBody
Definition: smooth_network_body.hpp:39
PhysicalObject::saveState
virtual BareNetworkString * saveState(std::vector< std::string > *ru)
Provides a copy of the state of the object in one memory buffer.
Definition: physical_object.cpp:815
Vec3
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
PhysicalObject::m_shape
btCollisionShape * m_shape
The bullet collision shape.
Definition: physical_object.hpp:118
PhysicalObject::Settings::m_linear_factor
Vec3 m_linear_factor
Bullet's linear factor.
Definition: physical_object.hpp:66
PhysicalObject::removeBody
void removeBody()
Remove body from dynamic world.
Definition: physical_object.cpp:748
Rewinder
Definition: rewinder.hpp:44
PhysicalObject::getID
std::string getID()
Returns the ID of this physical object.
Definition: physical_object.hpp:224
XMLNode
utility class used to parse XML files
Definition: xml_node.hpp:48
PhysicalObject::Settings::m_on_item_collision
std::string m_on_item_collision
If non-empty, the name of the scripting function to call when a (flyable) item collides with this obj...
Definition: physical_object.hpp:93
PhysicalObject::isFlattenKartObject
bool isFlattenKartObject() const
Returns true if this object should cause a kart that touches it to be flattened.
Definition: physical_object.hpp:269
PhysicalObject::Settings::m_body_type
PhysicalObject::BodyTypes m_body_type
Shape of the object.
Definition: physical_object.hpp:60
PhysicalObject::m_body_type
BodyTypes m_body_type
The shape of this object.
Definition: physical_object.hpp:115
smooth_network_body.hpp
This class help to smooth the graphicial transformation of network controlled object....
PhysicalObject::m_current_transform
btTransform m_current_transform
Save current transform to avoid frequent lookup from world transform.
Definition: physical_object.hpp:143
PhysicalObject::m_graphical_offset
Vec3 m_graphical_offset
The mesh might not have the same center as bullet does.
Definition: physical_object.hpp:148
PhysicalObject::Settings::m_reset_height
float m_reset_height
If the item is below that height, it is reset (when m_reset_when_too_low is true).
Definition: physical_object.hpp:85
PhysicalObject::Settings::m_friction
float m_friction
Friction for this object.
Definition: physical_object.hpp:64
PhysicalObject::Settings::m_angular_factor
Vec3 m_angular_factor
Bullet angular factor.
Definition: physical_object.hpp:68
PhysicalObject::Settings::m_crash_reset
bool m_crash_reset
Trigger a reset in karts touching it?
Definition: physical_object.hpp:75
PhysicalObject::m_init_xyz
core::vector3df m_init_xyz
The initial XYZ position of the object.
Definition: physical_object.hpp:104
PhysicalObject::m_id
std::string m_id
ID of the object.
Definition: physical_object.hpp:121
PhysicalObject::hit
void hit(const Material *m, const Vec3 &normal)
Called when a physical object hits the track.
Definition: physical_object.cpp:774
PhysicalObject::addBody
void addBody()
Add body to dynamic world.
Definition: physical_object.cpp:759
PhysicalObject::update
void update(float dt)
Update, called once per physics time step.
Definition: physical_object.cpp:632
PhysicalObject::Settings::m_radius
float m_radius
Radius of the object, to overwrite the graphical dimension.
Definition: physical_object.hpp:56
PhysicalObject::m_init_scale
core::vector3df m_init_scale
The initial scale of the object.
Definition: physical_object.hpp:110
PhysicalObject::restoreState
virtual void restoreState(BareNetworkString *buffer, int count)
Called when a state needs to be replayed.
Definition: physical_object.cpp:848
PhysicalObject::Settings::m_id
std::string m_id
ID of the object.
Definition: physical_object.hpp:52