i6engine  1.0
PhysicalStateComponent.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 
57 #ifndef __I6ENGINE_API_PHYSICALSTATECOMPONENT_H__
58 #define __I6ENGINE_API_PHYSICALSTATECOMPONENT_H__
59 
62 
64 
65 #include "boost/thread/mutex.hpp"
66 
67 namespace i6e {
68 namespace api {
69 
70  namespace ResponseType {
76  enum ResponseType {
77  NONE = 0, // !< Simple objects being pushable
78  STATIC = 1<<0, // !< Static object (don't move, can't be pushed away, ...)
79  GHOST = 1<<1, // !< Objects that don't interact physically with other objects (it's more like an area)
80  TRIGGER = 1<<2 // !< collision with this object will trigger the internal callbacks (use this if you need shatter info)
81  };
82  }
83 
87  struct CollisionGroup {
91  uint32_t responseType;
92 
96  uint32_t crashType;
97 
101  uint32_t crashMask;
102 
106  CollisionGroup() : responseType(0), crashType(0), crashMask(0) {
107  }
108 
115  CollisionGroup(uint32_t response, uint32_t type, uint32_t mask) : responseType(response), crashType(type), crashMask(mask) {
116  }
117 
122  explicit CollisionGroup(const std::string & str) : responseType(), crashType(), crashMask() {
123  std::stringstream ss(str);
124  ss >> responseType;
125  ss >> crashType;
126  ss >> crashMask;
127  }
128 
130  responseType = cg.responseType;
131  crashType = cg.crashType;
132  crashMask = cg.crashMask;
133  return *this;
134  }
135 
136  void insertInMap(const std::string & entry, attributeMap & params) const {
137  std::stringstream ss;
138  ss << responseType << " " << crashType << " " << crashMask;
139  params.insert(std::make_pair(entry, ss.str()));
140  }
141 
142  template<class Archive>
143  void serialize(Archive & ar, const unsigned int /*version*/) {
144  ar & responseType;
145  ar & crashType;
146  ar & crashMask;
147  }
148  };
149 
156  typedef struct RayTestResult {
157  int64_t objID;
158  int64_t sourceID;
160 
161  RayTestResult() : objID(-1), sourceID(-1), collisionPoint() {}
162  } RayTestResult;
163 
167  enum ShatterInterest : uint16_t {
168  NONE = 0, // !< shouldn't be used with responseType TRIGGER
169  START = 1 << 0, // !< start of a collision
170  END = 1 << 1, // !< end of a collision
171  ALWAYS = 1 << 2 // !< every collision
172  };
173 
191  public:
195  enum class ShapeType : uint16_t {
196  PLANE, // !< an infinite large plane
197  BOX, // !< a cuboid
198  SPHERE, // !< a sphere
199  FILE // !< load from file
200  };
201 
205  enum class RayTestRepetition : uint16_t {
206  STOP = 0, // !< stops raytest, if started with periodic
207  ONCE, // !< sends one ray
208  PERIODIC // !< sends a ray every tick
209  };
210 
214  enum class RayTestNotify : uint16_t {
215  ALWAYS, // !< notifies always
216  CHANGE, // !< notifies only on change whether something was hit or not
217  FOUND, // !< notifies only if raytest was successful
218  NOTFOUND, // !< notifies only if raytest was unsuccessful
219  OBJECTCHANGE // !< notifies only if found object is changed
220  };
221 
225  PhysicalStateComponent(const int64_t id, const attributeMap & params);
226 
231 
236  Vec3 getPosition() const;
237 
247  void setPosition(const Vec3 & position, uint32_t prio);
248 
253  inline Quaternion getRotation() const { return _rotation; }
254 
263  void setRotation(const Quaternion & rotation, uint32_t prio);
264 
268  inline Vec3 getScale() const {
269  return _scale;
270  }
271 
275  void setScale(const Vec3 & scale, uint32_t prio);
276 
280  void setCollisionFlags(const CollisionGroup & col);
281 
285  CollisionGroup getCollisionFlags() const { return _collisionGroup; }
286 
291  void reset();
292 
296  void setCollisionShape(ShapeType st, const attributeMap & params);
297 
301  void applyRotation(const Quaternion & rotation);
302 
307  if (_speedDirty > 0) {
308  return _speedNew;
309  } else {
310  return _linearVelocity;
311  }
312  }
313 
320  void setLinearVelocity(const Vec3 & linVel, uint32_t prio);
321 
326  void applyCentralForce(const Vec3 & cForce, bool forceIsLocalSpace);
327  void applyForce(const Vec3 & force, const Vec3 & offset, bool forceIsLocalSpace);
328 
332  void setShatterInterest(ShatterInterest si);
333 
337  void setGravity(const Vec3 & gravity);
338 
342  attributeMap synchronize() const override;
343 
352  void rayTest(const Vec3 & from, const Vec3 & to, RayTestRepetition rtr, RayTestNotify rtn, const GameMessage::Ptr & msg);
353 
354  std::string getTemplateName() const override {
355  return "PhysicalState";
356  }
357 
361  void addPosition(const Vec3 & pos);
362 
363  std::vector<componentOptions> getComponentOptions() override;
364 
365  private:
369  Vec3 _position;
370  Vec3 _positionNew;
371  uint32_t _posDirty;
372  uint32_t _rotDirty;
373  uint32_t _speedDirty;
374 
378  Quaternion _rotation;
379  Quaternion _rotationNew;
380 
384  Vec3 _scale;
385  Vec3 _scaleNew;
386  uint32_t _scaleDirty;
387 
391  Vec3 _linearVelocity;
392  Vec3 _speedNew;
393 
399  std::vector<std::tuple<Vec3, Vec3, bool>> _forces;
400 
404  Vec3 _gravity;
405 
409  CollisionGroup _collisionGroup;
410 
414  ShapeType _shapeType;
415 
419  attributeMap _shapeParams;
420 
424  // TODO (Michael) maybe move this to Component base class?
425  bool _initialized;
426 
430  ShatterInterest _shatterInterest;
431 
435  mutable boost::mutex _lock;
436 
437  uint32_t _syncPrio;
438 
443  void Init() override;
444 
445  void Finalize() override;
446 
450  void News(const GameMessage::Ptr & msg) override;
451 
455  void Tick() override;
456 
457  virtual std::pair<AddStrategy, int64_t> howToAdd(const ComPtr & comp) const override;
458  };
459 
460 } /* namespace api */
461 } /* namespace i6e */
462 
463 #endif /* __I6ENGINE_API_PHYSICALSTATECOMPONENT_H__ */
464 
CollisionGroup getCollisionFlags() const
returns a reference to the Collision Flags of this GameObject
Class describing a 3d rotation.
Definition: i6eQuaternion.h:58
void serialize(Archive &ar, const unsigned int)
Vec3 getScale() const
returns the current scale value
#define ISIXE_MODULES_API
Quaternion getRotation() const
Returns the rotation of the object.
ShapeType
Different shape types supported by i6engine.
CollisionGroup(uint32_t response, uint32_t type, uint32_t mask)
Constructor. Initializes members with given values.
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
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.
std::string getTemplateName() const override
returns the name this template was registered with
Implements 3-dimensional vectors.
Definition: i6eVector.h:48
Vec3 getLinearVelocity() const
returns linear velocity
A shared pointer counting references and adds objects being not referenced any more to an internal li...
Definition: sharedPtr.h:50
void insertInMap(const std::string &entry, attributeMap &params) const
struct containing information from raytest objID: id of GO hit first by the ray sourceID: id of the s...
struct containing the collision info for an object
struct i6e::api::RayTestResult RayTestResult
struct containing information from raytest objID: id of GO hit first by the ray sourceID: id of the s...
Represents an object's physical state and synchronizes with the Physics subsystem. For creating a PhysicalStateComponent, these keys are possible: Name Required Type Description Public syncPrio no int priority from which upwards updates should be sent through network yes gravity no Vec3 gravity for this object. Replaces default yes pos yes Vec3 position of the SceneNode yes rot yes Quaternion rotation of the SceneNode yes scale yes Vec3 scale of the SceneNode yes collisionGroup yes int int int ResponseType CrashType CrashMask yes shapeType yes int one of the shapeTypes from ShapeType enum yes shatterInterest yes int one of the shatterInterests from ShatterInterest enum yes compound yes bool should this PSC be used as a compound shape together with other ones of this GameObject yes
CollisionGroup & operator=(const CollisionGroup &cg)
CollisionGroup(const std::string &str)
Constructor. Initializes members with given values.
RayTestNotify
Different flags for the kind of notify mechanism of raytest.
uint32_t crashType
type of the object
Component Base Class. All Components must derive from Component.
Definition: Component.h:97
RayTestRepetition
Different flags for kind of raytest repetition.
uint32_t responseType
defines the physical properties
CollisionGroup()
Constructor. Initializes members with 0 (no Collisions)