加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D

发布时间:2020-12-14 20:09:21 所属栏目:百科 来源:网络整理
导读:Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D 分类: cocos2d-x 物理引擎 2015-01-06 15:50 142人阅读 评论(0) 收藏 举报 cocos2dx 物理引擎 bullet 由于刚开始学的时候不知道 Bullet 的单位 1.0 代表多大,所以制作出的模型的大小也无法判断。 不用担心,

Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D

分类: cocos2d-x 物理引擎 142人阅读 评论(0) 收藏 举报
cocos2dx 物理引擎 bullet

由于刚开始学的时候不知道Bullet的单位1.0代表多大,所以制作出的模型的大小也无法判断。

不用担心,Bullet提供了一个类btIDebugDraw,这个泪已经实现了很多绘制功能,我们要做的就是实现几个虚函数。

我们继承btIDebugDraw,实现虚函数

[cpp] view plain copy
  1. classPhysicsDraw3D:publicbtIDebugDraw
  2. {
  3. public:
  4. voiddrawLine(constbtVector3&from,constbtVector3&to,constbtVector3&color);
  5. voiddrawContactPoint(constbtVector3&PointOnB,constbtVector3&normalOnB,
  6. btScalardistance,intlifeTime,constbtVector3&color);
  7. voidreportErrorWarning(constchar*warningString);
  8. voiddraw3dText(constbtVector3&location,constchar*textString);
  9. voidsetDebugMode(intdebugMode);
  10. intgetDebugMode()const;
  11. //...................
  12. private:
  13. int_debugDrawMode;
  14. //...................
  15. };

目前只需要drawLine,等以后涉及到其他方面时再来实现其他的绘制功能

drawContactPoint,draw3dText什么也不做。

_debugDrawMode一定要定义,因为btIDebugDraw在绘制时会检查需要绘制什么。

实现绘制模式

[cpp] view plain copy
  1. voidPhysicsDraw3D::setDebugMode(intdebugMode)
  2. {
  3. _debugDrawMode=debugMode;
  4. }
  5. intPhysicsDraw3D::getDebugMode()const
  6. {
  7. return_debugDrawMode;
  8. }

下面重点说一下drawLine,我们知道cocos2dx的DrawNode提供的都是绘制2d图元的功能,

而且修改起来也比较复杂,本着一切从简,主要是为了学习Bullet,所以我们就去copy一下

DrawPrimitives绘制Line的方法

同样DrawPrimitives::drawLine也是一个绘制2dLine的方法,那么就应该把它修改成可以绘制3DLine的方法,先观察这个drawLine的实现


[cpp] view plain copy
  1. voiddrawLine(constVec2&origin,constVec2&destination)
  2. {
  3. lazy_init();
  4. Vec2vertices[2]={
  5. Vec2(origin.x,origin.y),
  6. Vec2(destination.x,destination.y)
  7. };
  8. _color.r=color.x();
  9. _color.g=color.y();
  10. _color.b=color.z();
  11. s_shader->use();
  12. s_shader->setUniformsForBuiltins();
  13. s_shader->setUniformLocationWith4fv(s_colorLocation,(GLfloat*)&s_color.r,1);
  14. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
  15. #ifdefEMSCRIPTEN
  16. setGLBufferData(vertices,16);
  17. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,2,GL_FLOAT,GL_FALSE,0);
  18. #else
  19. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,vertices);
  20. #endif//EMSCRIPTEN
  21. glDrawArrays(GL_LINES,2);
  22. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);
  23. }

首先初始化一些数据(待会看),

1.vertices就是存放的2dline的起点终点,如果绘制3dline自然要改成Vec3

s_shader使用shader这个shader是cocos2dx启动时预载入的

2.EMSCRIPTEN看这http://www.cocos2d-x.org/wiki/Emscripten_usage

Cocos2D-X’sEmscriptensupportallowsgameswritteninC*+tobecompiledtoJavaScriptanddeployeddirectlytotheweb......

既然是js要用的暂且忽略

3.重点的重点来了http://www.baike.com/wiki/glVertexAttribPointer

glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,

GL_FALSE,vertices);

其中的2就是便是一个顶点的坐标数量,2d自然是2,3d的就是3了

注意:在glVertexAttribPointer之前一定要开启深度测试

glEnable(GL_DEPTH_TEST);绘制完成后要关闭深度检测glDisable(GL_DEPTH_TEST);

如果不开启深度测试看看这个效果你就明白了


可以看到阿狸的手被遮挡了,因为不开启深度测试gl就不会根据深度来显示,而是后绘制的会覆盖先绘制的

开启后


4.对这个划线方法修改以后

[cpp] view plain copy
  1. voidPhysicsDraw3D::drawLine(constbtVector3&from,constbtVector3&color)
  2. {
  3. Vec3vertices[2]={
  4. Vec3(from.x(),from.y(),from.z()),
  5. Vec3(to.x(),to.y(),to.z())
  6. };
  7. _shader->use();
  8. _shader->setUniformsForBuiltins();
  9. _shader->setUniformLocationWith4fv(_colorLocation,(GLfloat*)&_color.r,1);
  10. glEnable(GL_DEPTH_TEST);
  11. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
  12. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,3,vertices);
  13. glDrawArrays(GL_LINES,2);
  14. glDisable(GL_DEPTH_TEST);
  15. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);
  16. }

再来看看lazy_init

[cpp] view plain copy
  1. //
  2. //Positionand1colorpassedasauniform(tosimulateglColor4ub)
  3. //
  4. s_shader=GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
  5. s_shader->retain();
  6. s_colorLocation=s_shader->getUniformLocation("u_color");
  7. CHECK_GL_ERROR_DEBUG();
  8. s_pointSizeLocation=s_shader->getUniformLocation("u_pointSize");
  9. CHECK_GL_ERROR_DEBUG();
  10. s_initialized=true;

其实就是获取shader

s_colorLocationcolor属性,可以对制定绘制时的颜色

s_pointSizeLocationpointsize属性可以制定绘制点的大小

那么添加到PhysicsDraw3D,就如下

[cpp] view plain copy
  1. boolPhysicsDraw3D::initDraw()
  2. {
  3. _shader=GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
  4. if(_shader==nullptr)
  5. {
  6. returnfalse;
  7. }
  8. _shader->retain();
  9. _colorLocation=_shader->getUniformLocation("u_color");
  10. CHECK_GL_ERROR_DEBUG();
  11. _pointSizeLocation=_shader->getUniformLocation("u_pointSize");
  12. CHECK_GL_ERROR_DEBUG();
  13. _debugDrawMode=btIDebugDraw::DBG_MAX_DEBUG_DRAW_MODE;//绘制全部调试信息
  14. returntrue;
  15. }

方便起见,为PhysicsDraw3D添加构建方法

[cpp] view plain copy
  1. PhysicsDraw3D*PhysicsDraw3D::create()
  2. {
  3. autodraw=newPhysicsDraw3D;
  4. if(draw&&draw->initDraw())
  5. {
  6. returndraw;
  7. }
  8. returnnullptr;
  9. }

销毁方法

[cpp] view plain copy
  1. voidPhysicsDraw3D::destroy()
  2. {
  3. CC_SAFE_RELEASE_NULL(_shader);
  4. deletethis;
  5. }

将PhysicsDraw3D整合到PhysicsWorld3D

为PhysicsWorld3D添加私有变量

[cpp] view plain copy
  1. PhysicsDraw3D*_debugDraw;

并在initWorld最后添加

[cpp] view plain copy
  1. _debugDraw=PhysicsDraw3D::create();
  2. _world->setDebugDrawer(_debugDraw);

在destroy前添加

[cpp] view plain copy
  1. _debugDraw->destroy();
  2. _debugDraw=nullptr;

完整代码见文章最后

测试这个PhysicsDraw3D

在HelloWorld重载

[cpp] view plain copy
  1. voidHelloWorld::draw(Renderer*renderer,constMat4&transform,uint32_tflags)
  2. {
  3. _world->debugDraw();
  4. Layer::draw(renderer,transform,flags);
  5. }

按F5编译通过后可以看到Plane为一个坐标,box有包围盒和法向量

后续

box提供一个冲量box会在原来的基础上增加一定的速度,

这个速度就是impulse/mass,看一下applyCentralImpulse的实现就明白了

_box->applyCentralImpulse(btVector3(0,-10));

增加touchlistener

[cpp] view plain copy
  1. boolHelloWorld::initListener()
  2. {
  3. _touchListener=EventListenerTouchOneByOne::create();
  4. if(_touchListener==nullptr)
  5. {
  6. returnfalse;
  7. }
  8. _touchListener->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
  9. _touchListener->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
  10. _touchListener->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
  11. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_touchListener,this);
  12. returntrue;
  13. }

触摸时提供冲量

[cpp] view plain copy
  1. boolHelloWorld::onTouchBegan(Touch*touch,Event*unused_event)
  2. {
  3. _box->setActivationState(ACTIVE_TAG);
  4. _box->applyCentralImpulse(btVector3(0,-10));
  5. returntrue;
  6. }

Box跑远了



完整源码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读