tags:

views:

342

answers:

0

I am working on a hockey game and I am implementing Single Player mode. I am trying to move the "computer's" paddle in offense mode (move towards the ball). I am using CoCos2d and Box2d. I am moving the paddle using MouseJoints. The problem is the Paddle doesnt move at all!

tick is called in init method

[self schedule:@selector(tick:)];

  • (void)tick:(ccTime) dt { _world->Step(dt, 10, 10);

    CCSprite *computer_paddle; CCSprite *ball; b2Body *computer_paddle_b2Body; float32 direction; b2Vec2 velocity;

    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) { CCSprite *sprite = (CCSprite *)b->GetUserData();

    if (sprite.tag == 1) { //ball ball = sprite; static int maxSpeed = 10; velocity = b->GetLinearVelocity(); float32 speed = velocity.Length(); direction = velocity.y;

    if (speed > maxSpeed) {
    b->SetLinearDamping(0.5); } else if (speed < maxSpeed) { b->SetLinearDamping(0.0); }

    }

    if (sprite.tag == 3){ // paddle computer_paddle = sprite; computer_paddle_b2Body = b;

    }

    // update sprite position sprite.position = ccp(b->GetPosition().x * PTM_RATIO,b->GetPosition().y * PTM_RATIO); sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());

    }

    }

    // update the computer paddle in single player moving it towards the ball using MouseJoint

    //move towards the ball b2Vec2 b2Position = b2Vec2(ball.position.x/PTM_RATIO,ball.position.y/PTM_RATIO);

    b2MouseJointDef md1;

md1.bodyA = _groundBody;

md1.bodyB = computer_paddle_b2Body;

md1.target = b2Position;

md1.collideConnected = true;

md1.maxForce = 9999.0 * computer_paddle_b2Body->GetMass();

_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md1);

computer_paddle_b2Body->SetAwake(true);