dl
dl_physics.h
浏览该文件的文档.
1
12#pragma once
13
14#include <vector>
15
16//#include <btBulletDynamicsCommon.h>
17
18#include "dl_type.h"
19#include "math/dl_transform.h"
20#include "system/dl_time.h"
21#include "dl_rigid_body.h"
22
23
24//Extras / BulletMultiThreaded 拥有多线程版本 碰撞调度和 解算器
25
26namespace dl
27{
28//
29//inline btVector3 toBtVector3(const Position3& v)
30//{
31// return btVector3(v[0], v[1], v[2]);
32//}
33
35{
36public:
37 //btRigidBody* _rigidBody;
38 //btMotionState* _motion;//运动状态,可选 动态物体才有
39 size_t _idShape;//形状id
40};
41
43{
44public:
45 //btDefaultCollisionConfiguration* _collConfig;//碰撞配置
46 //btCollisionDispatcher* _collDispatcher;//碰撞调度
47 //btBroadphaseInterface* _broadphase;//broad-phase
48 //btSequentialImpulseConstraintSolver* _solver;//解算器
49 //btDiscreteDynamicsWorld* _world;//世界
50
51 //std::vector<btCollisionShape*> _allShape;//所有形状
52
54 {
55 /*_collConfig = new btDefaultCollisionConfiguration;
56 _collDispatcher = new btCollisionDispatcher(_collConfig);
57 _broadphase = new btDbvtBroadphase;
58 _solver = new btSequentialImpulseConstraintSolver;
59 _world = new btDiscreteDynamicsWorld(_collDispatcher, _broadphase, _solver, _collConfig);
60 _world->setGravity(btVector3(0, 0, -Numbers::GRAVITY));
61
62 btCollisionShape* shape = new btBoxShape(
63 btVector3(1, 1, 1));
64
65 _allShape.push_back(shape);*/
66 }
67
75 RigidBody* CreateRigidBody(size_t shape_id, Transform* trans, Float mass)
76 {
77 //if (shape_id >= _allShape.size())
78 //{
79 // log_err0("shape id越界!");
80 // return nullptr;
81 //}
82 //RigidBody* ret = new RigidBody;
83 //RigidBodyImp* body = ret->GetImp();
85 //body->_idShape = shape_id;
86 //btCollisionShape* shape = _allShape[shape_id];
88 //btTransform bt_trans;
89 //bt_trans.setIdentity();
90 //Position3 pos = trans->GetTranslation();
91 //bt_trans.setOrigin(toBtVector3(pos));
92
93 //body->_motion = new btDefaultMotionState(bt_trans);
95 //btScalar bt_mass{ mass };
96 //btVector3 local_inertia(0, 0, 0);
97 //if (bt_mass)
98 // shape->calculateLocalInertia(bt_mass, local_inertia);
100 //body->_rigidBody = new btRigidBody(bt_mass, body->_motion, shape);
102 //_world->addRigidBody(body->_rigidBody);
103 //return ret;
104 return nullptr;
105 }
106
109 {
110 return CreateRigidBody(0, trans, 1);
111 }
112
114 size_t CreateShapeBox(const Position3& box)
115 {
116 /*size_t id = _allShape.size();
117 btCollisionShape* shape = new btBoxShape(toBtVector3(box));
118 _allShape.push_back(shape);
119 return id;*/
120 return 0;
121 }
122
123 void Update()
124 {
125 //_world->stepSimulation(g_time->GetDelta());
126
127 //更新位置
128 /*for (int j = _world->getNumCollisionObjects() - 1; j >= 0; j--)
129 {
130 btCollisionObject* obj = _world->getCollisionObjectArray()[j];
131 btRigidBody* body = btRigidBody::upcast(obj);
132
133 btTransform trans;
134 if (body && body->getMotionState())
135 {
136 body->getMotionState()->getWorldTransform(trans);
137 }
138 else
139 {
140 trans = obj->getWorldTransform();
141 }
142 trans.getOpenGLMatrix()
143 printf("world pos object %d = %f,%f,%f\n", j, float(trans.getOrigin().getX()), float(trans.getOrigin().getY()), float(trans.getOrigin().getZ()));
144 }*/
145 }
146
148 {
149 /*btTransform bt_trans;
150 btRigidBody* bt_body = body->_rigidBody;
151
152 if (bt_body->getMotionState())
153 {
154 bt_body->getMotionState()->getWorldTransform(bt_trans);
155 }
156 else
157 {
158 bt_trans = bt_body->getWorldTransform();
159 }
160 const btVector3& pos = bt_trans.getOrigin();
161 trans->SetTranslation({pos.getX(), pos.getY(), pos.getZ()});*/
162 }
163
165 {
167 //for (int i = _world->getNumCollisionObjects() - 1; i >= 0; --i)
168 //{
169 // btCollisionObject* obj = _world->getCollisionObjectArray()[i];
170 // btRigidBody* body = btRigidBody::upcast(obj);
171 // if (body && body->getMotionState())
172 // {
173 // delete body->getMotionState();
174 // }
175 // _world->removeCollisionObject(obj);
176 // delete obj;
177 //}
178
179 //for (auto& iter : _allShape)
180 //{
181 // delete iter;
182 //}
183
184 //delete _world;
185 //delete _solver;
186 //delete _broadphase;
187 //delete _collDispatcher;
188 //delete _collConfig;
189 }
190};
191
192extern Physics* g_physics;
193
194}
size_t CreateShapeBox(const Position3 &box)
创建形状 box
RigidBody * CreateRigidBody(size_t shape_id, Transform *trans, Float mass)
创建刚体
void UpdateRigidBody(RigidBodyImp *body, Transform *trans)
RigidBody * CreateRigidBodyDefault(Transform *trans)
创建默认刚体
时间相关
通用类型
std::array< Float, 3 > Position3
float Float
Physics * g_physics