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

Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D,第1张

概述由于刚开始学的时候不知道Bullet的单位1.0代表多大,所以制作出的模型的大小也无法判断。 不用担心,Bullet提供了一个类btIDebugDraw,这个泪已经实现了很多绘制功能,我们要做的就是实现几个虚函数。 我们继承btIDebugDraw,实现虚函数 class PhysicsDraw3D : public btIDebugDraw{public: void drawLine(co

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

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

我们继承btIDeBUGDraw,实现虚函数

class PhysicsDraw3D : public btIDeBUGDraw{public:	voID drawline(const btVector3& from,const btVector3& to,const btVector3& color);	voID drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color);	voID reportErrorWarning(const char* warningString);	voID draw3dText(const btVector3& location,const char* textString);	voID setDeBUGMode(int deBUGMode);	int getDeBUGMode() const;// ...................private:	int _deBUGDrawMode;// ...................};

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

drawContactPoint,draw3dText什么也不做。

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

实现绘制模式

voID PhysicsDraw3D::setDeBUGMode(int deBUGMode){	_deBUGDrawMode = deBUGMode;}int PhysicsDraw3D::getDeBUGMode() const{	return _deBUGDrawMode;}

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

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

DrawPrimitives绘制line的方法

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


voID drawline(const Vec2& origin,const Vec2& destination){	lazy_init();				Vec2 vertices[2] = {		Vec2(origin.x,origin.y),Vec2(destination.x,destination.y)	};	_color.r = color.x();	_color.g = color.y();	_color.b = color.z();	s_shader->use();	s_shader->setUniformsForBuiltins();	s_shader->setUniformlocationWith4fv(s_colorLocation,(GLfloat*) &s_color.r,1);	GL::enabLevertexAttribs( GL::VERTEX_ATTRIB_FLAG_position );#ifdef EMSCRIPTEN	setGLBufferData(vertices,16);	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_position,2,GL_float,GL_FALSE,0);#else	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_position,vertices);#endif // EMSCRIPTEN	glDrawArrays(GL_lines,2);	CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);}

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

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.对这个划线方法修改以后

voID PhysicsDraw3D::drawline(const btVector3& from,const btVector3& color){	Vec3 vertices[2] = {		Vec3(from.x(),from.y(),from.z()),Vec3(to.x(),to.y(),to.z())	};	_shader->use();	_shader->setUniformsForBuiltins();	_shader->setUniformlocationWith4fv(_colorLocation,(GLfloat*) &_color.r,1);	glEnable(GL_DEPTH_TEST);	GL::enabLevertexAttribs( GL::VERTEX_ATTRIB_FLAG_position );	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_position,3,vertices);	glDrawArrays(GL_lines,2);gldisable(GL_DEPTH_TEST);	CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);}

再来看看lazy_init

   //        // position and 1 color passed as a uniform (to simulate glcolor4ub )        //        s_shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_name_position_U_color);        s_shader->retain();                s_colorLocation = s_shader->getUniformlocation("u_color");        CHECK_GL_ERROR_DEBUG();        s_pointSizeLocation = s_shader->getUniformlocation("u_pointSize");        CHECK_GL_ERROR_DEBUG();        s_initialized = true;

其实就是获取shader

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

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

那么添加到PhysicsDraw3D,就如下

@H_301_158@bool PhysicsDraw3D::initDraw(){ _shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_name_position_U_color); if (_shader == nullptr) { return false; } _shader->retain(); _colorLocation = _shader->getUniformlocation("u_color"); CHECK_GL_ERROR_DEBUG(); _pointSizeLocation = _shader->getUniformlocation("u_pointSize"); CHECK_GL_ERROR_DEBUG(); _deBUGDrawMode = btIDeBUGDraw::DBG_MAX_DEBUG_DRAW_MODE; // 绘制全部调试信息 return true;}

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

PhysicsDraw3D* PhysicsDraw3D::create(){	auto draw = new PhysicsDraw3D;	if (draw && draw->initDraw())	{		return draw;	}	return nullptr;}

销毁方法

voID PhysicsDraw3D::destroy(){	CC_SAFE_RELEASE_NulL(_shader);	delete this;}

将PhysicsDraw3D整合到PhysicsWorld3D

为PhysicsWorld3D添加私有变量

PhysicsDraw3D* _deBUGDraw;

并在initWorld最后添加

_deBUGDraw = PhysicsDraw3D::create();_world->setDeBUGDrawer(_deBUGDraw);

在destroy前添加

_deBUGDraw->destroy();_deBUGDraw = nullptr;

完整代码见文章最后

测试这个PhysicsDraw3D

在HelloWorld重载

voID HelloWorld::draw(Renderer *renderer,const Mat4 &transform,uint32_t flags){	_world->deBUGDraw();	Layer::draw(renderer,transform,flags);}

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

后续

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

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

_Box->applyCentralimpulse(btVector3(0,-10));

增加touchListener

bool HelloWorld::initListener(){	_touchListener = EventListenertouchOneByOne::create();	if (_touchListener == nullptr)	{		return false;	}	_touchListener->ontouchBegan = CC_CALLBACK_2(HelloWorld::ontouchBegan,this);	_touchListener->ontouchmoved = CC_CALLBACK_2(HelloWorld::ontouchmoved,this);	_touchListener->ontouchended = CC_CALLBACK_2(HelloWorld::ontouchended,this);	Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(_touchListener,this);	return true;}

触摸时提供冲量

bool HelloWorld::ontouchBegan(touch *touch,Event *unused_event){	_Box->setActivationState(ACTIVE_TAG);	_Box->applyCentralimpulse(btVector3(0,-10));	return true;}

Box跑远了



完整源码

总结

以上是内存溢出为你收集整理的Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D全部内容,希望文章能够帮你解决Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1029816.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存