To make a body moving in box2d , we can give mouseJoint to the body .
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
b2MouseJoint *_mouseJoint; Also body must be dynamic. -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 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); for (b2Body* b = world->GetBodyList(); b; b = b->GetNext() ){ CCSprite *myActor = (CCSprite*)b->GetUserData(); if (myActor != NULL) { b2Fixture *f = b->GetFixtureList(); if (f->TestPoint(locationWorld)) { b2MouseJointDef md; md.bodyA = groundBody;//ground Body md.bodyB = b; md.target = locationWorld; md.collideConnected = true; md.maxForce = 1000.0f * b->GetMass(); _mouseJoint = (b2MouseJoint *)world->CreateJoint(&md); b->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); } -(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { if (_mouseJoint) { world->DestroyJoint(_mouseJoint); _mouseJoint = NULL; } } - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (_mouseJoint == NULL) return; world->DestroyJoint(_mouseJoint); _mouseJoint = NULL; } |
Incoming search terms:
- setawake box2d titanium
- b2body move static
- b2body update position
- box2d body move android
- box2d iphine b2MouseJointDef
- box2s mousejoint body offset
- how to transform a b2body in touches in box2d ios
- ios - update b2body position when ccsprite moves?
- move b2body in the touch direction



