Cocos2d と Box2d で弾むボールをテストするサンプル プロジェクトを作成しました。
問題は、ボールが落ちないことで、その理由がわかりません。重力が設定され、質量は問題ありませんが、ボールは各ステップで同じ位置にとどまります。
class BouncingBallLayer : CCLayer
{
int m_screen_width;
int m_screen_height;
CCSprite m_ball;
b2Body m_body;
b2World m_world;
public BouncingBallLayer()
{
AccelerometerEnabled = false;
var screen_size = CCDirector.SharedDirector.WinSize;
m_screen_width = (int) screen_size.Width;
m_screen_height = (int) screen_size.Height;
m_ball = new CCSprite("assets/soccer-ball.png");
m_ball.Position = new CCPoint(m_screen_width / 2, m_screen_height / 2);
AddChild(m_ball);
b2Vec2 gravity = new b2Vec2(0.0f, -8.0f);
m_world = new b2World(gravity);
m_world.AllowSleep = false;
b2BodyDef ground_body_def = new b2BodyDef();
ground_body_def.position.Set(0, 0);
b2Body ground_body = m_world.CreateBody(ground_body_def);
b2EdgeShape ground_edge = new b2EdgeShape();
b2FixtureDef box_shape_def = new b2FixtureDef();
box_shape_def.shape = ground_edge;
ground_edge.Set(new b2Vec2(0, 0), new b2Vec2(m_screen_width / Constants.PTM_RATIO, 0));
ground_body.CreateFixture(box_shape_def);
ground_edge.Set(new b2Vec2(0, 0), new b2Vec2(0, m_screen_height / Constants.PTM_RATIO));
ground_body.CreateFixture(box_shape_def);
ground_edge.Set(new b2Vec2(0, m_screen_height / Constants.PTM_RATIO), new b2Vec2(m_screen_width / Constants.PTM_RATIO, m_screen_height / Constants.PTM_RATIO));
ground_body.CreateFixture(box_shape_def);
ground_edge.Set(new b2Vec2(m_screen_width / Constants.PTM_RATIO, m_screen_height / Constants.PTM_RATIO), new b2Vec2(m_screen_width / Constants.PTM_RATIO, 0));
ground_body.CreateFixture(box_shape_def);
b2BodyDef ball_body_def = new b2BodyDef();
ball_body_def.type = b2BodyType.b2_dynamicBody;
ball_body_def.position.Set(m_screen_width / 2 / Constants.PTM_RATIO, m_screen_height / 2 / Constants.PTM_RATIO);
ball_body_def.userData = m_ball;
m_body = m_world.CreateBody(ball_body_def);
b2CircleShape circle = new b2CircleShape();
circle.Radius = 26.0f / Constants.PTM_RATIO;
b2FixtureDef ball_shape_def = new b2FixtureDef();
ball_shape_def.shape = circle;
ball_shape_def.density = 1.0f;
ball_shape_def.friction = 0.2f;
ball_shape_def.restitution = 0.8f;
m_body.CreateFixture(ball_shape_def);
this.Schedule(Tick);
}
void Tick(float dt)
{
int velocityIterations = 8;
int positionIterations = 1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
m_world.Step(dt, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (var b = m_world.BodyList; b != null; b = b.Next)
{
if (b.UserData != null)
{
//Synchronize the AtlasSprites position and rotation with the corresponding body
var myActor = ((CCNode)b.UserData);
myActor.PositionX = b.Position.x * Constants.PTM_RATIO;
myActor.PositionY = b.Position.y * Constants.PTM_RATIO;
myActor.Rotation = -1 * CCMacros.CCRadiansToDegrees(b.Angle);
}
}
}
public static CCScene Scene
{
get
{
// 'scene' is an autorelease object.
var scene = new CCScene();
// 'layer' is an autorelease object.
var layer = new BouncingBallLayer();
// add layer as a child to scene
scene.AddChild(layer);
// return the scene
return scene;
}
}
}
ちなみに、デバッグ中に次の例外が発生します。
'Bounce.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\Bounce.exe',シンボルが読み込まれました。
'Bounce.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\MonoGame.Framework.dll '
'Bounce.vshost.exe' (マネージド (v4.0.30319)): Loaded 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\cocos2d-xna.dll '
'Bounce.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\OpenTK.dll'
タイプ 'System.DllNotFoundException' の初回例外が OpenTK.dll で発生しました
タイプ 'System.TypeInitializationException' の初回例外が OpenTK.dll で発生しました
タイプ 'System.DllNotFoundException' の初回例外が OpenTK.dll で発生しました
MonoGame.Framework.dll で「System.DllNotFoundException」タイプの初回例外が発生しました
'Bounce.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\box2d.dll'
タイプ 'System.IO.FileNotFoundException' の初回例外が mscorlib.dll で発生しました
MonoGame.Framework.dll で、タイプ 'Microsoft.Xna.Framework.Content.ContentLoadException' の初回例外が発生しました
これらのDLLはロードされていますが。
Ok!ついにすべての Cocos2d-xna のデバッグ バージョンをビルドしました! そして、ワールドが初期化されると、m_flags = b2WorldFlags.e_clearForces. これにより、すべての動きがリセットされます。これを解決する方法を見つける必要があります。