Apply Torque problem

Topics: Developer Forum, User Forum
Mar 2, 2011 at 6:49 PM
Edited Mar 2, 2011 at 8:55 PM

 

Later Edit:PROBLEM SOLVED!

Hello! I am new in using Farseer and I encoutered a problem while trying to make my own code. The problem is that my circle body won't accept any torque at all and also won't accept angular impulse. Please help me with this problem. Here is the code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using FarseerPhysics.Factories;


namespace test2_farseer
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        

        static World world = new World(new Vector2(0,0));
 
        Texture2D txrCircle;
        Vector2 v2Origin;
        Fixture fxtCircle;
        FarseerPhysics.Collision.Shapes.CircleShape shpCircle;
        Body bdyCircle;
        
        //private DebugViewXNA debugView;

        const float ctForceAmount = 10;
        Vector2 force = new Vector2(0, 0);
        const float ctTorqueAmount = 10000;
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            //debugView = new DebugViewXNA(world);
        }

        /// <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

            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()

        {
            //debugView.LoadContent(graphics.GraphicsDevice, Content);
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            txrCircle = Content.Load<Texture2D>("Circle");
            v2Origin = new Vector2(txrCircle.Width / 2, txrCircle.Height / 2);
            shpCircle = new FarseerPhysics.Collision.Shapes.CircleShape(txrCircle.Width / 2, 5);
            bdyCircle=new Body(world);
            bdyCircle.Position=new Vector2(300,300);
            fxtCircle = new Fixture(bdyCircle, shpCircle);
        
            //bdyCircle.IsStatic = true;
            fxtCircle.Body.BodyType = BodyType.Dynamic;
            fxtCircle.Body.Mass = 10;
            fxtCircle.Restitution = 0.3f;
            fxtCircle.Friction = 0.5f;
            fxtCircle.Body.AngularDamping = 0.1f;
            fxtCircle.Body.LocalCenter = new Vector2(txrCircle.Width / 2, txrCircle.Height / 2);
            fxtCircle.Body.SleepingAllowed = false;
            //fxtCircle.Body.AngularVelocity = 0.5f;
            
            
            

            // TODO: use this.Content to load your game content here
        }


        
            

            

        
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all 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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            
            
            
            
            KeyboardState key = Keyboard.GetState();

            if (key.IsKeyDown(Keys.A)) force+= new Vector2(-ctForceAmount, 0);
            if (key.IsKeyDown(Keys.D)) force+= new Vector2(ctForceAmount, 0);
            if (key.IsKeyDown(Keys.W)) force+= new Vector2(0, -ctForceAmount);
            if (key.IsKeyDown(Keys.S)) force+= new Vector2(0, ctForceAmount);
            if (key.IsKeyDown(Keys.Q)) fxtCircle.Body.ApplyTorque(-ctTorqueAmount);
            if (key.IsKeyDown(Keys.E)) fxtCircle.Body.ApplyTorque(ctTorqueAmount);

            fxtCircle.Body.ApplyTorque(ctTorqueAmount);



            fxtCircle.Body.ApplyForce(force);
            
            

            
            base.Update(gameTime);

            world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
            
        }

        /// <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(txrCircle, fxtCircle.Body.Position, null, Color.White, fxtCircle.Body.Rotation, v2Origin, 1, SpriteEffects.None, 1);
            spriteBatch.End();
            // TODO: Add your drawing code here

            /*Matrix proj = Matrix.CreateOrthographic(50 * graphics.GraphicsDevice.Viewport.AspectRatio, 50, 0, 1);
            Matrix view = Matrix.Identity;

            debugView.RenderDebugData(ref proj, ref view);*/

            base.Draw(gameTime);
        }
    }
}