i6engine  1.0
PhysicsNode.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_MODULES_PHYSICSNODE_H__
26 #define __I6ENGINE_MODULES_PHYSICSNODE_H__
27 
28 #include <vector>
29 
31 
32 #include "LinearMath/btMotionState.h"
33 
34 class btCollisionShape;
35 class btCompoundShape;
36 class btRigidBody;
37 
38 namespace i6e {
39 namespace api {
40  struct CollisionGroup;
41 } /* namespace api */
42 namespace modules {
43 
44  class PhysicsMailbox;
45  class PhysicsManager;
46  class PhysicsVelocityComponent;
47 
51  typedef struct PeriodicRaytest {
56 
58 
59  PeriodicRaytest() : from(), to(), raytestNotify(), message(), lastResult() {
60  }
61 
62  PeriodicRaytest(const Vec3 & f, const Vec3 & t, api::PhysicalStateComponent::RayTestNotify rtn, api::GameMessage::Ptr msg) : from(f), to(t), raytestNotify(rtn), message(msg), lastResult() {
63  }
65 
81  class PhysicsNode : public btMotionState {
82  friend class PhysicsMailbox;
83  friend class PhysicsManager;
85 
86  public:
90  PhysicsNode(const int64_t id, const int64_t compId, const Vec3 & position, const Quaternion & rotation, const Vec3 & scale, const api::CollisionGroup & cg, const api::attributeMap & params, api::ShatterInterest shatterInterest, PhysicsManager * pm);
91 
95  ~PhysicsNode();
96 
100  bool addChild(const int64_t compId, const Vec3 & position, const Quaternion & rotation, const Vec3 & scale, const api::CollisionGroup & cg, const api::PhysicalStateComponent::ShapeType shapeType, const api::attributeMap & shapeParams);
101 
105  void removeChild(int64_t compId);
106 
107  bool empty() const;
108 
113  void reset();
114 
121  void setPosition(const Vec3 & position, int64_t id);
122 
129  void setRotation(const Quaternion & rotation, int64_t id);
130 
136  void setScale(const Vec3 & scale, int64_t id);
137 
141  inline void getWorldTransform(btTransform & worldTrans) const { worldTrans = _transform; }
142 
146  void setWorldTransform(const btTransform & worldTrans);
147 
151  inline btRigidBody * getRigidBody() const { return _rigidBody; }
152 
156  api::RayTestResult HitTest(const Vec3 & from, const Vec3 & to);
157 
161  int64_t getID() const {
162  return _id;
163  }
164 
165  int64_t getCompID() const {
166  return _compId;
167  }
168 
172  uint32_t getCrashType() { return _collisionGroup.crashType; }
173 
177  void setCollisionFlags(const api::CollisionGroup & cg);
178 
182  void setCrashType(uint32_t ct) {
183  _collisionGroup.crashType = ct;
184  }
185 
189  void setCrashMask(uint32_t cm) {
190  _collisionGroup.crashMask = cm;
191  }
192 
196  uint32_t getCrashMask() { return _collisionGroup.crashMask; }
197 
201  Vec3 getPosition() const { return _position; }
202 
206  Quaternion getRotation() const { return _rotation; }
207 
211  Vec3 getScale() const { return _scale; }
212 
216  void applyRotation(const Quaternion & rotation, int64_t id);
217 
221  api::ShatterInterest getShatterInterest() const { return _shatterInterest; }
222 
226  void Tick();
227 
231  void createVelocityComponent(double maxSpeed, double resistanceCoefficient, double windage);
232 
237 
238  private:
242  PhysicsManager * _manager;
243 
247  int64_t _id;
248  int64_t _compId;
249  api::CollisionGroup _collisionGroup;
250 
251  Vec3 _position;
252  Quaternion _rotation;
253  Vec3 _scale;
254 
255  btTransform _transform;
256  btRigidBody * _rigidBody;
257 
258  Vec3 _linearVelocity;
259  Vec3 _centralForce;
260 
264  api::ShatterInterest _shatterInterest;
265 
269  std::vector<PeriodicRaytest *> _rayTests;
270 
275  btCompoundShape * _parentShape;
276 
277  std::map<int64_t, std::pair<int64_t, btCollisionShape *>> _childShapes;
278 
279  PhysicsVelocityComponent * _velocityComponent;
280 
281  uint16_t _tickCount;
282 
283  double _mass;
284 
286 
290  void startSimulatingBody();
291 
295  void stopSimulatingBody();
296 
300  void News(const api::GameMessage::Ptr & msg);
301 
305  void addTicker();
306 
310  void removeTicker();
311 
312  Vec3 getVelocity() const;
313 
314  void applyForce(const Vec3 & force, const Vec3 & offset, bool local);
315 
317  };
318 
319 } /* namespace modules */
320 } /* namespace i6e */
321 
322 #endif /* __I6ENGINE_MODULES_PHYSICSNODE_H__ */
323 
void deleteVelocityComponent()
deletes a VelocityComponent
bool addChild(const int64_t compId, const Vec3 &position, const Quaternion &rotation, const Vec3 &scale, const api::CollisionGroup &cg, const api::PhysicalStateComponent::ShapeType shapeType, const api::attributeMap &shapeParams)
adds a new child shape to this node
Class describing a 3d rotation.
Definition: i6eQuaternion.h:58
struct i6e::modules::PeriodicRaytest PeriodicRaytest
contains informations for periodic raytests
Vec3 getScale() const
returns the scale of the node
Definition: PhysicsNode.h:211
Vec3 getPosition() const
returns the position of the node
Definition: PhysicsNode.h:201
ShapeType
Different shape types supported by i6engine.
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
void Tick()
called during every physics tick
std::map< std::string, std::string > attributeMap
Definition: attributeMap.h:42
uint32_t crashMask
mask of all objects the given object gets collision feedback
ShatterInterest
Different flags for collision interest.
api::ShatterInterest getShatterInterest() const
get the collision interest
Definition: PhysicsNode.h:221
Handles the incoming messages on the physic channel and calls the corresponding methods.
Implements 3-dimensional vectors.
Definition: i6eVector.h:48
contains informations for periodic raytests
Definition: PhysicsNode.h:51
void setCollisionFlags(const api::CollisionGroup &cg)
sets the collision flags of the Node
void getWorldTransform(btTransform &worldTrans) const
Called by Bullet when the object is first scheduled for simulation to retrieve the initial transform...
Definition: PhysicsNode.h:141
uint32_t getCrashMask()
returns the CrashMask of the Node
Definition: PhysicsNode.h:196
void reset()
Clears all forces and sets the velocity to 0, so that e.g. a bike after shattering doesn't still move...
void setCrashMask(uint32_t cm)
sets the crash mask of the Node
Definition: PhysicsNode.h:189
#define ASSERT_THREAD_SAFETY_HEADER
api::RayTestResult HitTest(const Vec3 &from, const Vec3 &to)
Tests a Hit with a ray given by the vector.
void setScale(const Vec3 &scale, int64_t id)
Override from PhysicalStateComponent to update internal transform. Sets new scale by calling Physical...
struct containing information from raytest objID: id of GO hit first by the ray sourceID: id of the s...
void setCrashType(uint32_t ct)
sets the crashtype of the node
Definition: PhysicsNode.h:182
api::RayTestResult lastResult
Definition: PhysicsNode.h:57
struct containing the collision info for an object
api::PhysicalStateComponent::RayTestNotify raytestNotify
Definition: PhysicsNode.h:54
int64_t getID() const
returns the GO id of the Node
Definition: PhysicsNode.h:161
Represents the physical state of an object simulated by the Bullet library. Contains the btTransform ...
Definition: PhysicsNode.h:81
void setRotation(const Quaternion &rotation, int64_t id)
Override from PhysicalStateComponent to update internal transform. Sets new rotation by calling Physi...
void setWorldTransform(const btTransform &worldTrans)
Called by Bullet when the simulated object has moved.
void createVelocityComponent(double maxSpeed, double resistanceCoefficient, double windage)
creates a new VelocityComponent
void applyRotation(const Quaternion &rotation, int64_t id)
applies the new rotation transforming rotation and forces
uint32_t getCrashType()
returns the CrashType of the Node
Definition: PhysicsNode.h:172
Handles calculation of air friction.
PeriodicRaytest(const Vec3 &f, const Vec3 &t, api::PhysicalStateComponent::RayTestNotify rtn, api::GameMessage::Ptr msg)
Definition: PhysicsNode.h:62
btRigidBody * getRigidBody() const
returns the rigidBody of the PhysicsNode
Definition: PhysicsNode.h:151
RayTestNotify
Different flags for the kind of notify mechanism of raytest.
int64_t getCompID() const
Definition: PhysicsNode.h:165
api::GameMessage::Ptr message
Definition: PhysicsNode.h:55
void setPosition(const Vec3 &position, int64_t id)
Override from PhysicalStateComponent to update internal transform. Sets new position by calling Physi...
uint32_t crashType
type of the object
PhysicsNode(const int64_t id, const int64_t compId, const Vec3 &position, const Quaternion &rotation, const Vec3 &scale, const api::CollisionGroup &cg, const api::attributeMap &params, api::ShatterInterest shatterInterest, PhysicsManager *pm)
constructor
Quaternion getRotation() const
returns the rotation of the node
Definition: PhysicsNode.h:206
void removeChild(int64_t compId)
removes a child shape