我正在尝试使用鼠标关节将球(动态体)移动到目标位置。
我有一个拍摄按钮,当我点击它时,我正在调用这个方法来创建我的球。
-(void) addNewSpriteAtPosition:(CGPoint)p withAngle:(CGFloat)angle
{
CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y);
angle = 270-angle;
sprite1 = [PhysicsSprite spriteWithTexture:spriteTexture_ rect:CGRectMake(0 ,0 ,22,22)];
[self addChild:sprite1 z:0 tag:1111];
[spritesToRemoveArray addObject:sprite1];
sprite1.rotation = angle;
sprite1.position = ccp( p.x, p.y);
// Define the dynamic body.
//Set up a 1m squared box in the physics world
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO-1.0);
bodyDef.userData = sprite1;
_footballBody = world->CreateBody(&bodyDef);
_footballBody->SetUserData(sprite1);
// Define another box shape for our dynamic body.
b2CircleShape dynamicBox;
dynamicBox.m_radius = 0.35f;
//dynamicBox.(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 20.0f;
fixtureDef.friction = 0.9f;
fixtureDef.restitution = 0.6f;
fixtureDef.userData = sprite1;
fixtureDef.filter.groupIndex = -1;
_footballFixture = _footballBody->CreateFixture(&fixtureDef);
[sprite1 setPhysicsBody:_footballBody];
float angle1 = CC_DEGREES_TO_RADIANS(angle);
b2Vec2 vect = b2Vec2(cos(angle1)*120, sin(angle1)*120);
b2Vec2 PointVector = _footballBody->GetPosition();
_footballBody->ApplyLinearImpulse(vect,PointVector);
b2Vec2 targetPos = b2Vec2(body1->GetPosition());
//if (_footballFixture->TestPoint(_footballBody->GetPosition())) {
NSLog(@"\n Target postion = <%f,%f>",targetPos.x,targetPos.y);
NSLog(@"\n football position = <%f,%f>",_footballBody->GetPosition().x/32.0,_footballBody->GetPosition().y/32.0);
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _footballBody;
md.target = targetPos;
md.collideConnected = true;
md.maxForce = 1000.0f * _footballBody->GetMass();
// md.dampingRatio = 0;
// md.frequencyHz = 100;
_mouseJoint = (b2MouseJoint *) world->CreateJoint(&md);
_footballBody->SetAwake(true);
//}
}
实际上当我这样做时,球应该移动到目标点,但是在这里使用上面的方法,球在它创建的点浮动并且慢慢地移动到左边缘的右边缘。
我需要您身边的建议我该如何解决这个问题。
非常感谢任何人的帮助。
全心全意,