Advertisement
Guest User

MotionState

a guest
May 19th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "btBulletDynamicsCommon.h"
  4. #include "entityx/entityx.h"
  5. #include "components/TransformComponent.hpp"
  6. #include "components/GraphNodeComponent.hpp"
  7. #include <glm/gtc/type_ptr.hpp>
  8. #include "glm/gtx/string_cast.hpp"
  9. #include "glm/gtc/matrix_inverse.hpp"
  10.  
  11. namespace ex = entityx;
  12.  
  13. namespace sw {
  14.  
  15.  
  16.     class MyMotionState : public btMotionState {
  17.  
  18.     public:
  19.  
  20.         MyMotionState(ex::Entity entity) : entity_(entity), transformComponent_(entity.component<TransformComponent>()),
  21.                                            node_(entity.component<GraphNodeComponent>()) {
  22.  
  23.  
  24.             transform_.setFromOpenGLMatrix(glm::value_ptr(transformComponent_->cached_world_));
  25.  
  26.             glm::mat4 test;
  27.  
  28.             transform_.getOpenGLMatrix(glm::value_ptr(test));
  29.         }
  30.  
  31.         virtual ~MyMotionState() {
  32.         }
  33.  
  34.  
  35.         virtual void getWorldTransform(btTransform &worldTrans) const {
  36.             worldTrans = transform_;
  37.         }
  38.  
  39.         virtual void setWorldTransform(const btTransform &worldTrans) {
  40.  
  41.             if (!transformComponent_) { return; }
  42.  
  43.             transform_ = worldTrans;
  44.  
  45.             glm::mat4 temp;
  46.  
  47.             worldTrans.getOpenGLMatrix(glm::value_ptr(temp));
  48.  
  49.             btQuaternion rot = worldTrans.getRotation();
  50.             btVector3 pos = worldTrans.getOrigin();
  51.  
  52.             recalculate_world_transform(entity_, rot, pos);
  53.  
  54.  
  55.  
  56.         }
  57.  
  58.     protected:
  59.  
  60.         btTransform transform_;
  61.         ex::Entity entity_;
  62.         ex::ComponentHandle<TransformComponent> transformComponent_;
  63.         ex::ComponentHandle<GraphNodeComponent> node_;
  64.  
  65.  
  66.     private:
  67.  
  68.         void recalculate_local_transform(ex::ComponentHandle<GraphNodeComponent> parent,
  69.                                          glm::mat4 current_transform) {
  70.  
  71.             glm::mat4 change = current_transform *
  72.                                glm::affineInverse(node_->parent_.component<TransformComponent>()->cached_world_);
  73.  
  74.         }
  75.  
  76.         void recalculate_world_transform(ex::Entity entity, btQuaternion rot, btVector3 pos) {
  77.  
  78.             auto transform = entity.component<TransformComponent>();
  79.             auto node = entity.component<GraphNodeComponent>();
  80.  
  81.             transform->world_rotation_ = glm::quat(rot.w(), rot.x(), rot.y(), rot.z());
  82.             transform->world_position_ = glm::vec3(pos.x(), pos.y(), pos.z());
  83.             transform->update_world_transform();
  84.  
  85.             if (!node_->children_.empty()) {
  86.                 for (ex::Entity e : node->children_) {
  87.                     recalculate_world_transform(e, rot, pos);
  88.                 }
  89.             }
  90.  
  91.  
  92.         }
  93.  
  94.  
  95.     };
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement