关节的使用 mouseJoint和PrismaticJoint

原文地址(http://blog.youkuaiyun.com/cq361106306/article/details/7950000)

mouseJoint:

作用是实现鼠标拽的效果。

使用方法如下

void HelloWorld::ccTouchesBegan(CCSet *touches, CCEvent *pEvent)  
{  
    //CCLOG("touchBegan");  
    if(mouseJoint!=NULL)  
        return;  
    CCLOG("touchBegan");  
    CCSetIterator it;  
    CCTouch* touch;  
  
    for( it = touches->begin(); it != touches->end(); it++)   
    {  
        touch = (CCTouch*)(*it);  
  
        if(!touch)  
            break;  
  
        CCPoint location = touch->locationInView();  
  
        location = CCDirector::sharedDirector()->convertToGL(location);  
  
        b2Vec2 locationWorld=b2Vec2(location.x/PTM_RATIO,location.y/PTM_RATIO);  
        //这里取得触摸的坐标  
        if(paddleFixeture->TestPoint(locationWorld))//判断是否在触摸范围  
        {  
            //if touched  
            b2MouseJointDef md;  
            md.bodyA=groundBody;//一般为世界边界  
            md.bodyB=paddleBody;//需要拖动的物体  
            md.target=locationWorld;//指定拖动的坐标  
            md.collideConnected=true; //  
            md.maxForce=1000.0f*paddleBody->GetMass(); //给一个拖动的力  
            mouseJoint=(b2MouseJoint*)world->CreateJoint(&md);//创建  
            paddleBody->SetAwake(true);//  
            CCLOG("touchBegan");  
  
        }  
    }  
  
}  

 

上面就定义了mouseJoint的参数

然后再在touchedMoved里面时时改变坐标就OK了

 void HelloWorld::ccTouchesMoved(CCSet *touches, CCEvent *pEvent)  
{  
    if(mouseJoint==NULL)  
        return;  
    CCSetIterator it;  
    CCTouch* touch;  
  
    for( it = touches->begin(); it != touches->end(); it++)   
    {  
        touch = (CCTouch*)(*it);  
  
        if(!touch)  
            break;  
  
        CCPoint location = touch->locationInView();  
  
        location = CCDirector::sharedDirector()->convertToGL(location);  
        b2Vec2 locationWorld=b2Vec2(location.x/PTM_RATIO,location.y/PTM_RATIO);  
        mouseJoint->SetTarget(locationWorld);  
    }  

最后记得在touchedEnded里面加上这句释放

if(mouseJoint!=NULL)
 {
  world->DestroyJoint(mouseJoint);
  mouseJoint=NULL;
 }

 

PrismaticJoint

这个就更简单 了

作用是限定一个物理的运动范围

//restrict paddle  
        b2PrismaticJointDef jointDef;  
        jointDef.collideConnected=true;  
        b2Vec2 worldAxis(1.0f,0.0f);//这里限定在水平X轴的方向  
        jointDef.Initialize(paddleBody,groundBody,paddleBody->GetWorldCenter(),worldAxis);//各个参数的意思是,被限定物体,限定的区域(一般是世界),限定点,方向  
        world->CreateJoint(&jointDef);  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值