使用 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)): 加载'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\Bounce.exe',已加载符号。
'Bounce.vshost.exe'(托管 (v4.0.30319)):加载 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\MonoGame.Framework.dll '
'Bounce.vshost.exe' (Managed (v4.0.30319)): 加载'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\cocos2d-xna.dll '
'Bounce.vshost.exe'(托管 (v4.0.30319)):加载 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\OpenTK.dll'
OpenTK.dll 中出现了“System.DllNotFoundException”类型的第一次机会异常
OpenTK.dll 中出现了“System.TypeInitializationException”类型的第一次机会异常
OpenTK.dll 中出现了“System.DllNotFoundException”类型的第一次机会异常
MonoGame.Framework.dll 中出现了“System.DllNotFoundException”类型的第一次机会异常
'Bounce.vshost.exe'(托管 (v4.0.30319)):加载 'C:\Users\Максим\Projects\games\kicknrun\branches\cocos2d\KickNRun\Bounce\bin\WindowsGL\Debug\box2d.dll'
mscorlib.dll 中出现了“System.IO.FileNotFoundException”类型的第一次机会异常
MonoGame.Framework.dll 中出现了“Microsoft.Xna.Framework.Content.ContentLoadException”类型的第一次机会异常
尽管这些 DLL 已加载。
好的!我终于构建了所有 Cocos2d-xna 东西的调试版本!似乎当世界初始化时,m_flags = b2WorldFlags.e_clearForces。这导致所有运动被重置!还得想办法解决这个问题!