Overte C++ Documentation
MyCharacterController.h
1 //
2 // MyCharacterController.h
3 // interface/src/avatar
4 //
5 // Created by AndrewMeadows 2015.10.21
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 
13 #ifndef hifi_MyCharacterController_h
14 #define hifi_MyCharacterController_h
15 
16 #include <CharacterController.h>
17 //#include <SharedUtil.h>
18 #include <PhysicsEngine.h>
19 
20 class btCollisionShape;
21 class MyAvatar;
22 class DetailedMotionState;
23 
24 class MyCharacterController : public CharacterController {
25 public:
26  explicit MyCharacterController(std::shared_ptr<MyAvatar> avatar, const FollowTimePerType& followTimeRemainingPerType);
27  ~MyCharacterController ();
28 
29  void addToWorld() override;
30  void updateShapeIfNecessary() override;
31 
32  // Sweeping a convex shape through the physics simulation can be expensive when the obstacles are too
33  // complex (e.g. small 20k triangle static mesh) so instead we cast several rays forward and if they
34  // don't hit anything we consider it a clean sweep. Hence this "Shotgun" code.
35  class RayShotgunResult {
36  public:
37  void reset();
38  float hitFraction { 1.0f };
39  bool walkable { true };
40  };
41 
43  bool testRayShotgun(const glm::vec3& position, const glm::vec3& step, RayShotgunResult& result);
44 
45  void setDensity(btScalar density) { _density = density; }
46 
47  const btCollisionShape* createDetailedCollisionShapeForJoint(int32_t jointIndex);
48  DetailedMotionState* createDetailedMotionStateForJoint(int32_t jointIndex);
49  std::vector<DetailedMotionState*>& getDetailedMotionStates() { return _detailedMotionStates; }
50  void clearDetailedMotionStates();
51 
52  void buildPhysicsTransaction(PhysicsEngine::Transaction& transaction);
53 
54  struct RayAvatarResult {
55  bool _intersect { false };
56  bool _isBound { false };
57  QUuid _intersectWithAvatar;
58  int32_t _intersectWithJoint { -1 };
59  float _distance { 0.0f };
60  float _maxDistance { 0.0f };
61  QVariantMap _extraInfo;
62  glm::vec3 _intersectionPoint;
63  glm::vec3 _intersectionNormal;
64  std::vector<int32_t> _boundJoints;
65  };
66  std::vector<RayAvatarResult> rayTest(const btVector3& origin, const btVector3& direction, const btScalar& length,
67  const QVector<uint>& jointsToExclude) const;
68 
69  int32_t computeCollisionMask() const override;
70  void handleChangedCollisionMask() override;
71 
72  void setCollideWithOtherAvatars(bool collideWithOtherAvatars) { _collideWithOtherAvatars = collideWithOtherAvatars; }
73 
74  bool needsSafeLandingSupport() const;
75 
76 protected:
77  void initRayShotgun(const btCollisionWorld* world);
78  void updateMassProperties() override;
79 
80 private:
81  btConvexHullShape* computeShape() const;
82 
83 protected:
84  std::shared_ptr<MyAvatar> _avatar { nullptr };
85 
86  // shotgun scan data
87  btAlignedObjectArray<btVector3> _topPoints;
88  btAlignedObjectArray<btVector3> _bottomPoints;
89  btScalar _density { 1.0f };
90 
91  std::vector<DetailedMotionState*> _detailedMotionStates;
92  bool _collideWithOtherAvatars { true };
93 };
94 
95 #endif // hifi_MyCharacterController_h