我使用Nick Vellios' tutorial來創建具有Box2D對象的徑向引力。我知道這裏的Make a Vortex,但我無法弄清楚如何在我的項目中實現它。在iPhone2的Box2d/Cocos2d中應用旋渦/漩渦效果
我製作了一個渦旋物體,它是一個以一致的角速度旋轉的Box2D circleShape傳感器。當其他Box2D物體接觸到這個渦旋物體時,我希望它們以與渦旋相同的角速度旋轉,逐漸靠近渦旋的中心。此時物體被渦旋中心吸引,但它將直接前往渦流中心,而不是像我想要的那樣緩慢旋轉。它也會沿着與旋渦相反的方向以及旋渦的旋轉方向行進。
鑑於漩渦和box2D的身體,我怎樣才能設置box2d的身體隨渦流旋轉,因爲它被'吸入'。
我設置了旋渦的旋轉,當我創建這樣的:
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.angle = 2.0f;
bodyDef.angularVelocity = 2.0f;
這裏是我如何應用徑向重力,按Nick Vellios' sample code。
-(void)applyVortexForcesOnSprite:(CCSpriteSubclass*)sprite spriteBody:(b2Body*)spriteBody withVortex:(Vortex*)vortex VortexBody:(b2Body*)vortexBody vortexCircleShape:(b2CircleShape*)vortexCircleShape{
//From RadialGravity.xcodeproj
b2Body* ground = vortexBody;
b2CircleShape* circle = vortexCircleShape;
// Get position of our "Planet" - Nick
b2Vec2 center = ground->GetWorldPoint(circle->m_p);
// Get position of our current body in the iteration - Nick
b2Vec2 position = spriteBody->GetPosition();
// Get the distance between the two objects. - Nick
b2Vec2 d = center - position;
// The further away the objects are, the weaker the gravitational force is - Nick
float force = 1/d.LengthSquared(); // 150 can be changed to adjust the amount of force - Nick
d.Normalize();
b2Vec2 F = force * d;
// Finally apply a force on the body in the direction of the "Planet" - Nick
spriteBody->ApplyForce(F, position);
//end radialGravity.xcodeproj
}
更新我覺得iForce2d給了我足夠的信息讓我的路,現在它只是調整。除了上面的代碼之外,這是我目前正在做的事情。正在發生的事情是身體獲得足夠的速度以便很好地離開漩渦的重力 - 在某處我需要檢查速度是否低於這個數字。我有點擔心,我目前沒有考慮到物體的質量。
b2Vec2 vortexVelocity = vortexBody->GetLinearVelocityFromWorldPoint(spriteBody->GetPosition());
b2Vec2 vortexVelNormal = vortexVelocity;
vortexVelNormal.Normalize();
b2Vec2 bodyVelocity = b2Dot(vortexVelNormal, spriteBody->GetLinearVelocity()) * vortexVelNormal;
//Using a force
b2Vec2 vel = bodyVelocity;
float forceCircleX = .6 * bodyVelocity.x;
float forceCircleY = .6 * bodyVelocity.y;
spriteBody->ApplyForce(b2Vec2(forceCircleX,forceCircleY), spriteBody->GetWorldCenter());
這看起來像一個很好的答案,今晚我會告訴你一個好消息,讓你知道我如何繼續。謝謝! – glenstorey
只是爲了澄清 - 我應該這樣做,而不是我已經在做什麼,而不是除了它之外? – glenstorey
另外。你有什麼好看的。 – iforce2d