如何在iphone中的box2d中将一个对象移动到另一个对象而不会反弹?

时间:2013-06-27 10:56:28

标签: ios cocos2d-iphone cocos2d-x box2d-iphone

我想在碰撞时将一个物体移动到另一个物体上而不会反弹。

在简单中我有一个球(身体),我有一个矩形线图像(正文),图像斜率为45度。

当球在坡度图像(静止体)上触摸时球从最高速度下降为20,它将始终反弹并继续运行。

当球越过线条图像时,它不会反弹并根据碰撞和球停在最后减速。当球与某物碰撞时,在现实世界中发生的情况相同,球速将减慢并且根据碰撞而转动,然后由于碰撞速度的降低而停止。

我这样做,但无法达到我想要的结果。

 -(id) init
{
if( (self=[super init])) {

    CGSize winSize = [CCDirector sharedDirector].winSize;
    // Create a world
    b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
    _world = new b2World(gravity);


    /////////////////////////   Ball ///////////////////////////////

    // Create sprite and add it to the layer
    CCSprite *ball = [CCSprite spriteWithFile:@"ball.png"];
    ball.position = ccp(100, 200);
    ball.tag = 1;
    [self addChild:ball];

    // Create ball body
    b2BodyDef ballBodyDef;
    ballBodyDef.type = b2_dynamicBody;
    ballBodyDef.position.Set(127/PTM_RATIO, 210/PTM_RATIO);
    ballBodyDef.userData = ball;
    ballBody = _world->CreateBody(&ballBodyDef);       // b2Body * ballBody

    // Create circle shape
    b2CircleShape circle;
    circle.m_radius = 26.0/PTM_RATIO;

    // Create shape definition and add to body
    b2FixtureDef ballShapeDef;
    ballShapeDef.shape = &circle;
    ballShapeDef.density = 15.0f;
    ballShapeDef.friction = 2.f;
    ballShapeDef.restitution = 0.0f;
    _ballFixture = ballBody->CreateFixture(&ballShapeDef);




    b2Vec2 force = b2Vec2(73, -52);
    ballBody->ApplyLinearImpulse(force, ballBodyDef.position);

    //ballBody->SetLinearVelocity(b2Vec2(10,0));   // try
   // ballBody->SetAngularVelocity(0);            //  try


    /////////////////////////   Ball ///////////////////////////////







    // Create paddle and add it to the layer
    CCSprite *paddle = [CCSprite spriteWithFile:@"paddle.png"];
    paddle.position = ccp(winSize.width/2, 50);
    [self addChild:paddle];

    // Create paddle body
    b2BodyDef paddleBodyDef;
    paddleBodyDef.type = b2_staticBody;         //b2_staticBody, b2_dynamicBody
    paddleBodyDef.position.Set(winSize.width/2/PTM_RATIO, 50/PTM_RATIO);
    paddleBodyDef.userData = paddle;
    paddleBodyDef.angle = 75;
    _paddleBody = _world->CreateBody(&paddleBodyDef);

    // Create paddle shape
    b2PolygonShape paddleShape;
    paddleShape.SetAsBox(paddle.contentSize.width/PTM_RATIO/2, paddle.contentSize.height/PTM_RATIO/2);

    // Create shape definition and add to body
    b2FixtureDef paddleShapeDef;
    paddleShapeDef.shape = &paddleShape;
    paddleShapeDef.density = 25.0f;
    paddleShapeDef.friction = 1.1f;
    paddleShapeDef.restitution = 0.1f;
    _paddleFixture = _paddleBody->CreateFixture(&paddleShapeDef);






    // Restrict paddle along the x axis
    b2PrismaticJointDef jointDef;
    b2Vec2 worldAxis(0.0f, 0.0f);
    jointDef.collideConnected = true;
    jointDef.Initialize(_paddleBody, _groundBody, _paddleBody->GetWorldCenter(), worldAxis);
    _world->CreateJoint(&jointDef);


    [self schedule:@selector(tick:)];
    self.touchEnabled = YES;
}



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

if (_mouseJoint != NULL) return;

UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);

if (_paddleFixture->TestPoint(locationWorld)) {

    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);
}

// [自我踢];

}

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

if (_mouseJoint == NULL) return;

UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);

_mouseJoint->SetTarget(locationWorld);





}

1 个答案:

答案 0 :(得分:0)

好的,你疯了很小的错误,问题在于引力。

将其更改为

b2Vec2 gravity = b2Vec2(0.0f, -10.0f);