2016-04-16 2 views
0

私は物理学をゲームで実装する方法を学びたいので、私はFarseer Physicsを選択しました。私は障害物(私の場合は地面)まで落下するオブジェクトを実装しようとしています。ここに私のコードは次のとおりです。Farseer物理エンジンを搭載したMonoGame。最下部に位置しない

using FarseerPhysics; 
using FarseerPhysics.Collision.Shapes; 
using FarseerPhysics.Dynamics; 
using FarseerPhysics.Factories; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using System; 

namespace TestFarseer 
{ 
    /// <summary> 
    /// This is the main type for your game. 
    /// </summary> 
    public class Game1 : Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     World world; 
     Texture2D buttonTexture, groundTexture; 
     Body buttonBody, groundBody; 
     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 

     /// <summary> 
     /// Allows the game to perform any initialization it needs to before starting to run. 
     /// This is where it can query for any required services and load any non-graphic 
     /// related content. Calling base.Initialize will enumerate through any components 
     /// and initialize them as well. 
     /// </summary> 
     protected override void Initialize() 
     { 
      // TODO: Add your initialization logic here 
      world = new World(new Vector2(0, 1f)); 
      ConvertUnits.SetDisplayUnitToSimUnitRatio(102f); 
      base.Initialize(); 
     } 

     /// <summary> 
     /// LoadContent will be called once per game and is the place to load 
     /// all of your content. 
     /// </summary> 
     protected override void LoadContent() 
     { 
      // Create a new SpriteBatch, which can be used to draw textures. 
      spriteBatch = new SpriteBatch(GraphicsDevice); 
      buttonTexture = Content.Load<Texture2D>("pause"); 
      groundTexture = Content.Load<Texture2D>("Panel"); 

      groundBody = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(groundTexture.Width), ConvertUnits.ToSimUnits(groundTexture.Height), 1f); 
      groundBody.BodyType = BodyType.Static; 
      groundBody.Position = new Vector2(ConvertUnits.ToSimUnits(groundTexture.Width/2f), ConvertUnits.ToSimUnits(GraphicsDevice.Viewport.Height-groundTexture.Height)); 
      CircleShape buttonShape = new CircleShape(0.5f, 0.8f); 

      buttonBody = new Body(world); 
      buttonBody.Mass = 10f; 
      buttonBody.CreateFixture(buttonShape); 
      buttonBody.BodyType = BodyType.Dynamic; 
      buttonBody.Position = new Vector2(ConvertUnits.ToSimUnits(51f), ConvertUnits.ToSimUnits(51f)); 
      // TODO: use this.Content to load your game content here 
     } 

     /// <summary> 
     /// UnloadContent will be called once per game and is the place to unload 
     /// game-specific content. 
     /// </summary> 
     protected override void UnloadContent() 
     { 
      // TODO: Unload any non ContentManager content here 
     } 

     /// <summary> 
     /// Allows the game to run logic such as updating the world, 
     /// checking for collisions, gathering input, and playing audio. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Update(GameTime gameTime) 
     { 
      // TODO: Add your update logic here 
      world.Step((float)gameTime.ElapsedGameTime.TotalSeconds); 
      base.Update(gameTime); 
     } 

     /// <summary> 
     /// This is called when the game should draw itself. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 
      spriteBatch.Begin(); 
      spriteBatch.Draw(buttonTexture, ConvertUnits.ToDisplayUnits(buttonBody.Position), null, Color.White, buttonBody.Rotation, new Vector2(buttonTexture.Width/2, buttonTexture.Height/2), 1f, SpriteEffects.None, 0); 
      spriteBatch.Draw(groundTexture, ConvertUnits.ToDisplayUnits(groundBody.Position), null, Color.White, groundBody.Rotation, new Vector2(groundTexture.Width/2, groundTexture.Height/2), 1f, SpriteEffects.None, 0); 
      spriteBatch.End(); 
      base.Draw(gameTime); 
     } 
    } 
} 

、衝突が発生しますが、私は一番下になるように地面を設定することはできません。ここではスクリーンショットです:FarseerでBottom problem

答えて

0

体の起源はそう、中心にある:

groundBody.Position = new Vector2(ConvertUnits.ToSimUnits(groundTexture.Width/2f), ConvertUnits.ToSimUnits(GraphicsDevice.Viewport.Height-(groundTexture.Height/2)));` 

GROOVER、課題の一つであった

+0

うん、。しかし、また、PreferredBackBufferHeight/Widthのバグがありました。 Farseerはこの値が必要でした。この変更後、問題は解決されました。しかし、チップのおかげで、私は決着としてマークします。 –

関連する問題