Tramway SDK
motionstates.h
Go to the documentation of this file.
1// TRAMWAY DRIFT AND DUNGEON EXPLORATION SIMULATOR 2022
2// All rights reserved.
3
4#ifndef PHYSICS_BULLET_MOTIONSTATES_H
5#define PHYSICS_BULLET_MOTIONSTATES_H
6
7#include <btBulletDynamicsCommon.h>
8
9#include <framework/core.h>
10#include <framework/entity.h>
11
12#include <components/physics.h>
14
15namespace tram::Physics {
16
17
18class EntMotionState : public btMotionState {
19public:
20 EntMotionState (Entity* ent, vec3& offset) {
21 entity = ent;
22 troffse = offset;
23 }
24
25 virtual ~EntMotionState() {
26 }
27
28 void SetEntity(Entity* ent) {
29 entity = ent;
30 }
31
32 virtual void getWorldTransform (btTransform &worldTrans) const {
33 vec3 loc;
34 quat rot;
35
36 if (entity == nullptr){
37 loc = vec3(0.0f, 0.0f, 0.0f);
38 rot = quat(vec3(0.0f, 0.0f, 0.0f));
39 } else {
40 loc = entity->GetLocation();
41 rot = entity->GetRotation();
42 }
43
44 loc += troffse;
45
46 btVector3 translation;
47 translation.setX(loc.x);
48 translation.setY(loc.y);
49 translation.setZ(loc.z);
50
51 btQuaternion rotation;
52 rotation.setX(rot.x);
53 rotation.setY(rot.y);
54 rotation.setZ(rot.z);
55 rotation.setW(rot.w);
56
57 btTransform transf;
58 transf.setRotation(rotation);
59 transf.setOrigin(translation);
60
61 worldTrans = transf;
62 }
63
64 virtual void setWorldTransform (const btTransform &worldTrans) {
65 if (entity == nullptr) return;
66
67 vec3 location;
68 quat rotation;
69
70 btQuaternion rot = worldTrans.getRotation();
71 btVector3 loc = worldTrans.getOrigin();
72
73 location.x = loc.getX();
74 location.y = loc.getY();
75 location.z = loc.getZ();
76
77 location -= troffse;
78
79 rotation.x = rot.getX();
80 rotation.y = rot.getY();
81 rotation.z = rot.getZ();
82 rotation.w = rot.getW();
83
84 entity->UpdateTransform(location, rotation);
85 }
86
87protected:
88 Entity* entity = nullptr;
90};
91
92class ArmMotionState : public btMotionState {
93public:
94 ArmMotionState (name_t boneName, AnimationComponent* armature, vec3 bindPos, Entity* entity, PhysicsComponent* physicsComp) {
95 ent = entity;
96 arm = armature;
97 offset = bindPos;
98 bone = boneName;
99 physcomp = physicsComp;
100 }
101
102 virtual ~ArmMotionState() {}
103
104 virtual void getWorldTransform (btTransform &worldTrans) const {
105 vec3 loc = ent->GetLocation();
106 quat rot = ent->GetRotation();
107
108 loc += rot * offset;
109
110 btVector3 translation;
111 translation.setX(loc.x);
112 translation.setY(loc.y);
113 translation.setZ(loc.z);
114
115 btQuaternion rotation;
116 rotation.setX(rot.x);
117 rotation.setY(rot.y);
118 rotation.setZ(rot.z);
119 rotation.setW(rot.w);
120
121 btTransform transf;
122 transf.setRotation(rotation);
123 transf.setOrigin(translation);
124
125 worldTrans = transf;
126 }
127
128 virtual void setWorldTransform (const btTransform &worldTrans) {
129 vec3 location;
130 quat rotation;
131
132 btQuaternion rot = worldTrans.getRotation();
133 btVector3 loc = worldTrans.getOrigin();
134
135 location.x = loc.getX();
136 location.y = loc.getY();
137 location.z = loc.getZ();
138
139 rotation.x = rot.getX();
140 rotation.y = rot.getY();
141 rotation.z = rot.getZ();
142 rotation.w = rot.getW();
143
144 vec3 ent_loc = ent->GetLocation();
145 quat ent_rot = ent->GetRotation();
146
147
148 location = location - ent_loc;
149 location = glm::inverse(ent_rot) * location;
150 location = location - offset;
151
152 rotation = rotation * glm::inverse(ent_rot);
153
154 Render::Keyframe kframe;
155 kframe.rotation = rotation;
156 kframe.location = location;
157
158 arm->SetBoneKeyframe(bone, kframe);
159 }
160
161protected:
167};
168
169}
170
171#endif // PHYSICS_BULLET_MOTIONSTATES_H
Definition: animation.h:13
Definition: entity.h:20
void UpdateTransform(const vec3 &loc, const quat &rot)
Definition: entity.h:54
const vec3 & GetLocation()
Definition: entity.h:61
const quat & GetRotation()
Definition: entity.h:62
Definition: motionstates.h:92
virtual ~ArmMotionState()
Definition: motionstates.h:102
ArmMotionState(name_t boneName, AnimationComponent *armature, vec3 bindPos, Entity *entity, PhysicsComponent *physicsComp)
Definition: motionstates.h:94
PhysicsComponent * physcomp
Definition: motionstates.h:162
vec3 offset
Definition: motionstates.h:165
name_t bone
Definition: motionstates.h:166
AnimationComponent * arm
Definition: motionstates.h:163
Entity * ent
Definition: motionstates.h:164
virtual void getWorldTransform(btTransform &worldTrans) const
Definition: motionstates.h:104
virtual void setWorldTransform(const btTransform &worldTrans)
Definition: motionstates.h:128
Definition: motionstates.h:18
virtual void getWorldTransform(btTransform &worldTrans) const
Definition: motionstates.h:32
virtual void setWorldTransform(const btTransform &worldTrans)
Definition: motionstates.h:64
vec3 troffse
Definition: motionstates.h:89
Entity * entity
Definition: motionstates.h:88
virtual ~EntMotionState()
Definition: motionstates.h:25
EntMotionState(Entity *ent, vec3 &offset)
Definition: motionstates.h:20
void SetEntity(Entity *ent)
Definition: motionstates.h:28
Definition: physics.h:13
Definition: api.h:10
glm::vec3 vec3
Definition: math.h:12
glm::quat quat
Definition: math.h:13
Definition: animation.h:28
vec3 location
Definition: animation.h:30
quat rotation
Definition: animation.h:31
Definition: uid.h:11