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
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.
22 #include <string>
24 #include "btBulletDynamicsCommon.h"
26 #include "network/rewinder.hpp"
28 #include "physics/user_pointer.hpp"
29 #include "utils/vec3.hpp"
31 class Material;
32 class TrackObject;
33 class XMLNode;
38 class PhysicalObject : public Rewinder,
39  public SmoothNetworkBody
40 {
41 public:
43  enum BodyTypes {MP_NONE,
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;
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
101 private:
104  core::vector3df m_init_xyz;
107  core::vector3df m_init_hpr;
110  core::vector3df m_init_scale;
112  TrackObject *m_object;
118  btCollisionShape *m_shape;
121  std::string m_id;
124  btRigidBody *m_body;
127  btDefaultMotionState *m_motion_state;
130  float m_mass;
132  bool m_body_added;
139  btTransform m_init_pos;
143  btTransform m_current_transform;
156  float m_radius;
164  bool m_flatten_kart;
176  std::string m_on_kart_collision;
180  std::string m_on_item_collision;
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;
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;
198  void copyFromMainProcess(TrackObject* track_obj);
199 public:
200  PhysicalObject(bool is_dynamic, const Settings& settings,
201  TrackObject* object);
203  static std::shared_ptr<PhysicalObject> fromXML
204  (bool is_dynamic, const XMLNode &node, TrackObject* object);
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;
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; }
256  // Methods usable by scripts
269  bool isFlattenKartObject() const { return m_flatten_kart; }
270  void disable(void *memory)
271  {
272  ((PhysicalObject*)(memory))->removeBody();
273  }
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
303 #endif
304 /* EOF */
btCollisionShape * m_shape
The bullet collision shape.
Definition: physical_object.hpp:118
std::string m_id
ID of the object.
Definition: physical_object.hpp:121
btTransform m_init_pos
This is the initial position of the object for the physics.
Definition: physical_object.hpp:139
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
bool m_is_dynamic
If this body is a bullet dynamic body, i.e.
Definition: physical_object.hpp:184
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
A wrapper around bullets btVector3 to include conventient conversion functions (e.g.
Definition: vec3.hpp:34
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
bool m_reset_when_too_low
Reset the object when it falls under the track (useful e.g.
Definition: physical_object.hpp:82
Definition: smooth_network_body.hpp:38
void removeBody()
Remove body from dynamic world.
Definition: physical_object.cpp:748
Definition: rewinder.hpp:43
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
Vec3 m_linear_factor
Bullet&#39;s linear factor.
Definition: physical_object.hpp:66
A special class to store a triangle mesh with a separate material per triangle.
Definition: triangle_mesh.hpp:34
btRigidBody * getBody() const
Returns the rigid body of this physical object.
Definition: physical_object.hpp:229
bool m_crash_reset
True if a kart colliding with this object should be rescued.
Definition: physical_object.hpp:159
Definition: script_track.cpp:250
bool isSoccerBall() const
Returns true if this object is a soccer ball.
Definition: physical_object.cpp:727
BodyTypes m_body_type
The shape of this object.
Definition: physical_object.hpp:115
bool isCrashReset() const
Returns true if this object should trigger a rescue in a kart that hits it.
Definition: physical_object.hpp:233
void hit(const Material *m, const Vec3 &normal)
Called when a physical object hits the track.
Definition: physical_object.cpp:774
bool m_crash_reset
Trigger a reset in karts touching it?
Definition: physical_object.hpp:75
bool m_reset_when_too_low
True if object should be reset to its initial position if it&#39;s too low (see m_reset_height).
Definition: physical_object.hpp:168
virtual void undoEvent(BareNetworkString *buffer)
Called when an event needs to be undone.
Definition: physical_object.hpp:288
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::BodyTypes m_body_type
Shape of the object.
Definition: physical_object.hpp:60
void update(float dt)
Update, called once per physics time step.
Definition: physical_object.cpp:632
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
btRigidBody * m_body
The corresponding bullet rigid body.
Definition: physical_object.hpp:124
core::vector3df m_init_scale
The initial scale of the object.
Definition: physical_object.hpp:110
Definition: physical_object.hpp:38
Vec3 m_angular_factor
Bullet angular factor.
Definition: physical_object.hpp:68
void init()
Initialises a Settings object.
Definition: physical_object.cpp:109
bool m_explode_kart
True if kart should "explode" when touching this.
Definition: physical_object.hpp:162
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
float m_friction
Friction for this object.
Definition: physical_object.hpp:64
float m_angular_damping
Bullet&#39;s angular damping factor.
Definition: physical_object.hpp:72
bool isExplodeKartObject() const
Returns true if this object should cause an explosion if a kart hits it.
Definition: physical_object.hpp:237
virtual void rewindToEvent(BareNetworkString *buffer)
Called when an event needs to be replayed.
Definition: physical_object.hpp:289
Definition: physical_object.hpp:48
float m_restitution
Restitution of the physical object.
Definition: physical_object.hpp:62
bool isFlattenKartObject() const
Returns true if this object should cause a kart that touches it to be flattened.
Definition: physical_object.hpp:269
Vec3 m_graphical_offset
The mesh might not have the same center as bullet does.
Definition: physical_object.hpp:148
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
TriangleMesh * m_triangle_mesh
Non-null only if the shape is exact.
Definition: physical_object.hpp:187
btDefaultMotionState * m_motion_state
Bullet&#39;s motion state for this object.
Definition: physical_object.hpp:127
float m_mass
The mass of this object.
Definition: physical_object.hpp:130
bool m_knock_kart
Knock the kart around.
Definition: physical_object.hpp:77
btTransform m_current_transform
Save current transform to avoid frequent lookup from world transform.
Definition: physical_object.hpp:143
Definition: material.hpp:47
This class help to smooth the graphicial transformation of network controlled object. In case there is any difference between server and client predicted values, instead of showing the server one immediately, it will interpolate between them with an extrapolated value from the old predicted values stored in m_adjust_control_point estimated by current speed of object.
core::vector3df m_init_xyz
The initial XYZ position of the object.
Definition: physical_object.hpp:104
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:52
virtual void saveTransform()
Called before a rewind.
Definition: physical_object.cpp:800
float m_radius
Radius of the object - this obviously depends on the actual shape.
Definition: physical_object.hpp:156
float m_radius
Radius of the object, to overwrite the graphical dimension.
Definition: physical_object.hpp:56
Settings(BodyTypes type, float radius, float mass)
Creates a physical Settings object with the given type, radius and mass.
Definition: physical_object.cpp:53
A UserPointer is stored as a user pointer in all bullet bodies.
Definition: user_pointer.hpp:35
std::string m_id
ID of the object.
Definition: physical_object.hpp:52
utility class used to parse XML files
Definition: xml_node.hpp:47
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
void addBody()
Add body to dynamic world.
Definition: physical_object.cpp:759
float m_linear_damping
Bullet&#39;s linear damping factor.
Definition: physical_object.hpp:70
The supported collision shapes.
Definition: physical_object.hpp:43
core::vector3df m_init_hpr
The initial hpr of the object.
Definition: physical_object.hpp:107
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
UserPointer m_user_pointer
The pointer that is stored in the bullet rigid body back to this object.
Definition: physical_object.hpp:136
float m_height
Height of an object, to overwrite the graphical dimension.
Definition: physical_object.hpp:58
float m_mass
Mass of the object.
Definition: physical_object.hpp:54
bool m_flatten_kart
Flatten the kart when this object is touched.
Definition: physical_object.hpp:79
void setInteraction(std::string interaction)
Sets the interaction type.
Definition: physical_object.cpp:734
virtual void restoreState(BareNetworkString *buffer, int count)
Called when a state needs to be replayed.
Definition: physical_object.cpp:848
virtual void reset()
Nothing to do here.
Definition: physical_object.cpp:682
std::string getID()
Returns the ID of this physical object.
Definition: physical_object.hpp:224
void updateGraphics(float dt)
This updates all only graphical elements.
Definition: physical_object.cpp:606