我已经成功地将Farseer for XNA集成到MonoGame中,几乎没有遇到任何麻烦。堆叠,复杂的动力学等似乎都很好,但我绝对不能让球在地板上反弹。 当球接触地面时,它只会停在那里,只有在被另一个球推动时才会移开。
我的代码(在F#中,但它应该足够清楚;< - 表示分配,其他一切与C#相同)用于创建球,地面是:
member physics.CreateBall(x:float32<m>,y:float32<m>,r:float32<m>,m:float32<kg>,dynamic:bool) =
let myBody = Factories.BodyFactory.CreateBody(physics.Physics, Microsoft.Xna.Framework.Vector2(x * 1.0f<1/m> + random_range -30.0f 30.0f, y * 1.0f<1/m>) * PhysicsWorld.Scale)
do myBody.LastFrameUpdate <- Casanova.Core.get_frame_counter()
do myBody.BodyType <- if dynamic then BodyType.Dynamic else BodyType.Static
do myBody.Restitution <- 1.0f
do myBody.Friction <- 0.0f
do myBody.Mass <- m * 1.0f<_>
let circleShape = new CircleShape(r * PhysicsWorld.Scale * 1.0f<1/m>, 1.0f)
let fixture = myBody.CreateFixture(circleShape)
{ Body = myBody; Tick = Rule.Create 0 }
member physics.CreateBox(x:float32<m>,y:float32<m>,w:float32<m>,h:float32<m>,m:float32<kg>,dynamic:bool) =
let myBody = Factories.BodyFactory.CreateBody(physics.Physics, Microsoft.Xna.Framework.Vector2(x * 1.0f<1/m>, y * 1.0f<1/m>) * PhysicsWorld.Scale)
do myBody.LastFrameUpdate <- Casanova.Core.get_frame_counter()
do myBody.BodyType <- if dynamic then BodyType.Dynamic else BodyType.Static
do myBody.Restitution <- 1.0f
do myBody.Friction <- 0.0f
do myBody.Mass <- m * 1.0f<_>
let rectangle = PolygonTools.CreateRectangle(w * PhysicsWorld.Scale * 1.0f<1/m>, h * PhysicsWorld.Scale * 1.0f<1/m>)
let boxShape = PolygonShape(rectangle, 1.0f)
let fixture = myBody.CreateFixture(boxShape)
{ Body = myBody; Tick = Rule.Create 0 }
答案 0 :(得分:1)
最简单的解决方案。上面的代码几乎是正确的,减去了初始化顺序。以下是供将来参考的工作版本:
let myBody = Factories.BodyFactory.CreateBody(physics.Physics, Microsoft.Xna.Framework.Vector2(x * 1.0f<1/m>, y * 1.0f<1/m>) * PhysicsWorld.Scale)
let rectangle = PolygonTools.CreateRectangle(w * PhysicsWorld.Scale * 1.0f<1/m>, h * PhysicsWorld.Scale * 1.0f<1/m>)
let boxShape = PolygonShape(rectangle, 1.0f)
let fixture = myBody.CreateFixture(boxShape)
do myBody.LastFrameUpdate <- Casanova.Core.get_frame_counter()
do myBody.BodyType <- if dynamic then BodyType.Dynamic else BodyType.Static
do myBody.Restitution <- restitution
do myBody.Friction <- friction
do myBody.Mass <- m * 1.0f<_>