目前,我有一个有球的程序,来回蹦蹦跳跳。为了找出它弹跳的速度,我做了这个:
b2Vec2 velocity = blueCircleBody->GetLinearVelocity();
float constantSpeed = velocity.Length();
NSLog(@"%.f", constantSpeed);
这是在tick:函数内。每一秒,我想看看球的移动速度有多快。正如我在“输出”窗口中注意到的那样,几分钟后球似乎会减速。
2012-12-16 11:29:10.458 Bubble Fill V1[31623:c07] 30
2012-12-16 11:29:11.474 Bubble Fill V1[31623:c07] 30
2012-12-16 11:29:12.475 Bubble Fill V1[31623:c07] 30
2012-12-16 11:29:13.492 Bubble Fill V1[31623:c07] 24
2012-12-16 11:29:14.525 Bubble Fill V1[31623:c07] 24
是否有理由将球随机从30变为24?
此外,我还有另一个问题。如果我重新启动程序,使用所有相同的数据,球会以不同的速度运行,原因不明。这意味着我使用的冲动/力量并没有决定它的速度有多快。
以下是关于球的更多信息:
blueCircle = [CCSprite spriteWithFile:@"blueCircle.png"];
blueCircle.position = ccp(winSize.width/4, winSize.height/4);
blueCircle.tag = 2;
[self addChild:blueCircle];
blueCircleBodyDef.type = b2_dynamicBody;
//Find a way to make the BoundingBox/GroundBox to not be affected by the Force of the Walls
blueCircleBodyDef.position.Set(winSize.width/4/PTM_RATIO, winSize.height/4/PTM_RATIO);
//blueCircleBodyDef.position.Set(blueCircle.position.x/PTM_RATIO, blueCircle.position.y/PTM_RATIO);
blueCircleBodyDef.userData = blueCircle;
blueCircleBody = world->CreateBody(&blueCircleBodyDef);
float blueCircleRadius = blueCircle.contentSize.width/2 * blueCircle.scale;
blueCircleShape.m_radius = blueCircleRadius/PTM_RATIO;
blueCircleFixtureDef.shape = &blueCircleShape;
blueCircleFixtureDef.density = 1.0f;
blueCircleFixtureDef.friction = 0.0f;
blueCircleFixtureDef.restitution = 1.0f;
blueCircleBody->CreateFixture(&blueCircleFixtureDef);
force.Set(0, 140);
//blueCircleBody ->ApplyLinearImpulse(force, blueCircleBodyDef.position);
//blueCircleBody->SetLinearVelocity(force);
blueCircleBody->SetGravityScale(0);
blueCircleBody->SetLinearVelocity(force);