Cocos2d学习笔记一(box2d设置b2MouseJoint鼠标节点)

By | 2014/04/13

1.在.h文件中添加私有变量  b2MouseJoint *_mouseJoint;//用来拖拽手臂
2.在.m文件的init文件添加可以触摸 self.touchEnabled = YES;
3.实现touch-delegate 的下列方法

[code lang=”objc”]

#pragma mark – 触摸方法

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

if (_mouseJoint!=NULL) {

return;

}

for (UITouch *touch in touches) {

CGPoint location=[touch locationInView:[touch view]];

location=[[CCDirector sharedDirector] convertToGL:location];

b2Vec2 worldLocation=[Helper metersFromPoint:location];

//得到手臂的夹具

b2Fixture *armFixture=_armBody->GetFixtureList();

if (armFixture->TestPoint(worldLocation)) {   //判断touch坐标点是否选中了手臂刚体

//定义鼠标关节

b2MouseJointDef mouseDef;

mouseDef.bodyA=_groundBody;

mouseDef.bodyB=_armBody;

mouseDef.target=worldLocation;     //用来指明移动的目标

mouseDef.maxForce=_armBody->GetMass()*1000.f;//maxForce越大移动越简单

_mouseJoint=(b2MouseJoint *)world->CreateJoint(&mouseDef);

}

}

}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

for (UITouch *touch in touches) {

CGPoint location=[touch locationInView:[touch view]];

location=[[CCDirector sharedDirector] convertToGL:location];

b2Vec2 worldLocation=[Helper metersFromPoint:location];

if (_mouseJoint) {

_mouseJoint->SetTarget(worldLocation);

}

}

 

}

-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

 

if (_mouseJoint) {

world->DestroyJoint(_mouseJoint);

_mouseJoint=NULL;

}

 

}

– (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

//Add a new body/atlas sprite at the touched location

for( UITouch *touch in touches ) {

CGPoint location = [touch locationInView: [touch view]];

 

location = [[CCDirector sharedDirector] convertToGL: location];

if (_mouseJoint) {

world->DestroyJoint(_mouseJoint);

_mouseJoint=NULL;

//计算射击的方向和用力的大小

CGFloat shootAngle=_armBody->GetAngle();

int power=20;

float x1=cos(shootAngle);

float y1=sin(shootAngle);

//2.根据计算出来的角度计算发射的力,因为力是矢量所以有大小和方向

b2Vec2 force=b2Vec2(x1*power, y1*power);

//3.创建子弹刚体

b2BodyDef bulletDef;

bulletDef.position=_armBody->GetWorldCenter()+(4.0/PTM_RATIO)*b2Vec2(x1, y1);

bulletDef.bullet=true;

bulletDef.type=b2_dynamicBody;

bulletDef.fixedRotation=true;

bulletDef.linearDamping=0.5f;

b2Body *bulletBody=world->CreateBody(&bulletDef);

//将sprite关联到body的userData上 ,然后就可以通过userData的到精灵

int *bulletTag=new int(kTagBulletBody);  //将刚体与精灵关联

bulletBody->SetUserData(bulletTag);

b2CircleShape bulletShap;

bulletShap.m_radius=5.0/PTM_RATIO;

b2FixtureDef bulletFixture;

bulletFixture.shape=&bulletShap;

bulletFixture.friction=0.4f;   //摩擦力

bulletFixture.restitution=0.2f;  //恢复力

bulletFixture.density=10.0f;    //密度

bulletFixture.filter.categoryBits=0x0001; //默认状态下category也是1

bulletFixture.filter.maskBits=0xffff;     //设置与所有的刚体发生碰撞

bulletFixture.filter.groupIndex=4;   //大于0总是会参与碰撞

bulletBody->CreateFixture(&bulletFixture);

//4.给子弹施加计算的力

bulletBody->ApplyLinearImpulse(force, bulletBody->GetLocalCenter());

}

 

}

}

 

[/code]

One thought on “Cocos2d学习笔记一(box2d设置b2MouseJoint鼠标节点)

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据