读书人

cocos2d学习札记(八)物理引擎box2d之

发布时间: 2012-08-27 21:21:56 作者: rapoo

cocos2d学习笔记(八)物理引擎box2d之二

今天我们来看看如何通过拖拽移动一个物体


拖移物体需要用到box2d中的b2MouseJoint

首先我们在touchbegan方法中为我们点击到的物体创建b2MouseJoint对象,那么问题来了,我们如何获取点击到的物体呢?

box2d为我们提供了相关方法,即AABB(axis-aligned bounding box ),原理为:首先,我们点击位置的四边加上了1-point的边,这样我们点击的位置变成了一个小方块;之后,以某个形状的物体做个长方形,看看我们点击的位置是否在这个长方形里面。如图:

cocos2d学习札记(八)物理引擎box2d之二

如果在,那么调用callback对象,这个callback对象是我们自己写的,可以通过TestPoint看看点击的位置是否的确在AABB找出的物体上,如果在,那么我们就获取到了点击的物体。上图可以发现其实我们并没有点到那个黑色的物体上,所以TestPoint会返回false。如果同时点到了多个物体,只返回第一个被发现的。


之后是touch began代码,我们可以看到b2MouseJoint如何被创建的:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    for( UITouch *touch in touches ) {        CGPoint location = [touch locationInView:touch.view];        location = [[CCDirector sharedDirector] convertToGL:location];        location = [self convertToNodeSpace:location];        b2Vec2 b2Location = b2Vec2(location.x / PTM_RATIO, location.y / PTM_RATIO);                if (mouseJoint) {            mouseJoint->SetTarget(b2Location);        }    }}- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{//Add a new body/atlas sprite at the touched locationfor( UITouch *touch in touches ) {CGPoint location = [touch locationInView: [touch view]];location = [[CCDirector sharedDirector] convertToGL: location];//[self addNewSpriteWithCoords: location];        if (mouseJoint) {            world->DestroyJoint(mouseJoint);            mouseJoint = NULL;        }}}



读书人网 >操作系统

热点推荐