SuperTuxKart
Loading...
Searching...
No Matches
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
31class Material;
32class TrackObject;
33class XMLNode;
34
38class PhysicalObject : public Rewinder,
40{
41public:
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
49 {
50 public:
52 std::string m_id;
54 float m_mass;
56 float m_radius;
58 float m_height;
73
94 private:
95 void init();
96 public:
97 Settings(BodyTypes type, float radius, float mass);
98 Settings(const XMLNode &xml_node);
99 }; // Settings
100
101private:
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
144
149
156 float m_radius;
157
160
163
164 bool m_flatten_kart;
165
169
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);
199public:
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
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
Definition: material.hpp:48
Definition: physical_object.hpp:49
Vec3 m_linear_factor
Bullet's linear factor.
Definition: physical_object.hpp:66
float m_linear_damping
Bullet's linear damping factor.
Definition: physical_object.hpp:70
float m_friction
Friction for this object.
Definition: physical_object.hpp:64
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
std::string m_id
ID of the object.
Definition: physical_object.hpp:52
bool m_crash_reset
Trigger a reset in karts touching it?
Definition: physical_object.hpp:75
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
bool m_reset_when_too_low
Reset the object when it falls under the track (useful e.g.
Definition: physical_object.hpp:82
float m_height
Height of an object, to overwrite the graphical dimension.
Definition: physical_object.hpp:58
float m_radius
Radius of the object, to overwrite the graphical dimension.
Definition: physical_object.hpp:56
Vec3 m_angular_factor
Bullet angular factor.
Definition: physical_object.hpp:68
PhysicalObject::BodyTypes m_body_type
Shape of the object.
Definition: physical_object.hpp:60
float m_angular_damping
Bullet's angular damping factor.
Definition: physical_object.hpp:72
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
void init()
Initialises a Settings object.
Definition: physical_object.cpp:113
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
float m_restitution
Restitution of the physical object.
Definition: physical_object.hpp:62
bool m_knock_kart
Knock the kart around.
Definition: physical_object.hpp:77
Definition: physical_object.hpp:40
btDefaultMotionState * m_motion_state
Bullet's motion state for this object.
Definition: physical_object.hpp:127
UserPointer m_user_pointer
The pointer that is stored in the bullet rigid body back to this object.
Definition: physical_object.hpp:136
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
virtual void rewindToEvent(BareNetworkString *buffer)
Called when an event needs to be replayed.
Definition: physical_object.hpp:289
btRigidBody * m_body
The corresponding bullet rigid body.
Definition: physical_object.hpp:124
Vec3 m_graphical_offset
The mesh might not have the same center as bullet does.
Definition: physical_object.hpp:148
void removeBody()
Remove body from dynamic world.
Definition: physical_object.cpp:774
float m_mass
The mass of this object.
Definition: physical_object.hpp:130
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:841
virtual void reset()
Nothing to do here.
Definition: physical_object.cpp:708
bool isSoccerBall() const
Returns true if this object is a soccer ball.
Definition: physical_object.cpp:753
std::string m_id
ID of the object.
Definition: physical_object.hpp:121
btCollisionShape * m_shape
The bullet collision shape.
Definition: physical_object.hpp:118
void init(const Settings &settings)
Additional initialisation after loading of the model is finished.
Definition: physical_object.cpp:238
void updateGraphics(float dt)
This updates all only graphical elements.
Definition: physical_object.cpp:630
bool m_explode_kart
True if kart should "explode" when touching this.
Definition: physical_object.hpp:162
btTransform m_init_pos
This is the initial position of the object for the physics.
Definition: physical_object.hpp:139
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
core::vector3df m_init_xyz
The initial XYZ position of the object.
Definition: physical_object.hpp:104
btRigidBody * getBody() const
Returns the rigid body of this physical object.
Definition: physical_object.hpp:229
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
TriangleMesh * m_triangle_mesh
Non-null only if the shape is exact.
Definition: physical_object.hpp:187
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
virtual void undoEvent(BareNetworkString *buffer)
Called when an event needs to be undone.
Definition: physical_object.hpp:288
core::vector3df m_init_scale
The initial scale of the object.
Definition: physical_object.hpp:110
void setInteraction(std::string interaction)
Sets the interaction type.
Definition: physical_object.cpp:760
btTransform m_current_transform
Save current transform to avoid frequent lookup from world transform.
Definition: physical_object.hpp:143
void addBody()
Add body to dynamic world.
Definition: physical_object.cpp:785
std::string getID()
Returns the ID of this physical object.
Definition: physical_object.hpp:224
void update(float dt)
Update, called once per physics time step.
Definition: physical_object.cpp:658
float m_radius
Radius of the object - this obviously depends on the actual shape.
Definition: physical_object.hpp:156
bool m_crash_reset
True if a kart colliding with this object should be rescued.
Definition: physical_object.hpp:159
core::vector3df m_init_hpr
The initial hpr of the object.
Definition: physical_object.hpp:107
void hit(const Material *m, const Vec3 &normal)
Called when a physical object hits the track.
Definition: physical_object.cpp:800
BodyTypes
The supported collision shapes.
Definition: physical_object.hpp:43
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
bool m_is_dynamic
If this body is a bullet dynamic body, i.e.
Definition: physical_object.hpp:184
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
bool isExplodeKartObject() const
Returns true if this object should cause an explosion if a kart hits it.
Definition: physical_object.hpp:237
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:692
virtual void saveTransform()
Called before a rewind.
Definition: physical_object.cpp:826
virtual void restoreState(BareNetworkString *buffer, int count)
Called when a state needs to be replayed.
Definition: physical_object.cpp:874
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:834
Definition: rewinder.hpp:44
Definition: smooth_network_body.hpp:39
This is a base object for any separate object on the track, which might also have a skeletal animatio...
Definition: track_object.hpp:56
A special class to store a triangle mesh with a separate material per triangle.
Definition: triangle_mesh.hpp:35
A UserPointer is stored as a user pointer in all bullet bodies.
Definition: user_pointer.hpp:36
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
bool isFlattenKartObject() const
Returns true if this object should cause a kart that touches it to be flattened.
Definition: physical_object.hpp:269
This class help to smooth the graphicial transformation of network controlled object....