Collision No Working, Farseer Noob

Topics: User Forum
Apr 1, 2010 at 1:24 PM

 

When Ever i test all that happens is cube 1 fall right and dose not collide with cube2, can some one please help.
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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;

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

        PhysicsSimulator physicsSimulatior;

        Cube cube1;
        Cube cube2;

        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;
        }
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);


            physicsSimulatior = new PhysicsSimulator(new Vector2(0, 5));
            

            cube1 = new Cube(Content.Load<Texture2D>("box"), 50, 50, 1, physicsSimulatior);
            cube2 = new Cube(Content.Load<Texture2D>("box"), 800, 100, 1, physicsSimulatior);

            cube2.body.Position = new Vector2(0, 590);
            cube1.body.Position = new Vector2(400, 300);
            cube2.body.IsStatic = true;
            

            // TODO: use this.Content to load your game content here
        }
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            Console.Clear();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            cube1.Update(physicsSimulatior, gameTime);
            cube2.Update(physicsSimulatior, gameTime);

            physicsSimulatior.Update(gameTime.ElapsedGameTime.Milliseconds * .001f); //convert from ms to seconds
            
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            
            cube1.Draw(spriteBatch);
            cube2.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

 

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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;

namespace FarseerTest
{
    class Cube
    {
        public Body body;
        public Geom geom;
        public Texture2D art;
        public float sizeX;
        public float sizeY;

        public Cube(Texture2D Art, float SizeX, float SizeY, float mass, PhysicsSimulator physicsSimulator)
        {
            art = Art;
            sizeX = SizeX;
            sizeY = SizeY;

            body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, sizeX, sizeY, mass);
            geom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, body, sizeX, sizeY);
            geom.CollidesWith = CollisionCategory.All;
            geom.CollisionCategories = CollisionCategory.Cat1;
            geom.CollisionGroup = 100;

            physicsSimulator.Add(body);
            
        }
        public void Update(PhysicsSimulator physicsSimulator, GameTime gameTime)
        {

            physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle decRec = new Rectangle((int)body.Position.X, (int)body.Position.Y, (int)sizeX, (int)sizeY);

            spriteBatch.Draw(art, decRec, Color.White);
        }
    }
}

 

 

 

Apr 1, 2010 at 4:25 PM

Hi Hellmonkeyman,

I think you need to add the Geometry to the physics simulator, like you have with the body.

 

Under where you put

physicsSimulator.Add(body);

try adding 

physicsSimulator.Add(geom);

 

I hope that's what's going wrong.  Let us know how you get on.

 

Alan

Apr 1, 2010 at 5:52 PM

@hellomonkeyman

Thank you so much for posting your code! It truly makes it easier to tell you what's going on.

There are several things I have to comment on your code.

 

1. You only really need to call physicsSimulator.Update(...) once. You're calling physicsSimulator.update(...)

in each "Cube." I would recommend cutting that out.

 

2. You're creating a new rectangle per cube per frame in the Draw method. It might be better for you to 

just include that "decRec" as a member variable, and set decRec.X,Y where you are creating a new one each

Draw call.

 

3. For your simple test, you should cut out all that CollisionGroup/Categories/CollidesWith stuff, it's not

doing anything right now as far as I can see right here.

 

4. When you put your instance of physicsSimulator in the Body/GeomFactory, then it's automatically doing

physicsSimulator.Add for the instantiated body and geom. Your explicit physicsSimulator.Add call is confusing

it most likely, so you should cut that line out.

 

Tell us what happens!

 

Apr 1, 2010 at 6:57 PM
Edited Apr 2, 2010 at 1:07 AM

 

yea it was working but now, nothing. Here it all of the new code, warning there is a lot of it:

 

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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;

namespace LastWar
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        PhysicsSimulator physicsSimulatior;

        Player player1;
        Map map;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            graphics.PreferredBackBufferHeight = 800;
            graphics.PreferredBackBufferWidth = 600;
            this.IsMouseVisible = true;
        }
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            physicsSimulatior = new PhysicsSimulator(new Vector2(0, 5));

            player1 = new Player(50, 23, 28);
            player1.GenCube(new Vector2(20, 20),23,28, 1, physicsSimulatior, Content.Load<Texture2D>("box"));
           // player1.cube.body.IgnoreGravity = true;

             int[] TEMP_Animations = new int[3];
            TEMP_Animations[0] = 6;
            TEMP_Animations[1] = 6;
            TEMP_Animations[2] = 6;

           player1.sprite = new AnimationSprite(Content.Load<Texture2D>
               ("Art\\Units\\marine.58x34"), TEMP_Animations, 10,
                58, 34, 1);
            #region MAPDATA
           int[,] MAP_LAYER_TEMP //It is there, but there is alot of it.
            #endregion

            map = new Map(Content.Load<Texture2D>
               ("Art\\TileSet\\TileSet"),MAP_LAYER_TEMP, 16, 16, 50, 30, 20, 1);
            map.MapLoad(physicsSimulatior, Content.Load<Texture2D>("box"));

            // TODO: use this.Content to load your game content here
        }
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            Console.Clear();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            player1.Update(Keyboard.GetState());

            physicsSimulatior.Update(gameTime.ElapsedGameTime.Milliseconds * .001f); //convert from ms to seconds

            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            map.Draw(spriteBatch);

            player1.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
Class Cube:
namespace LastWar
{
    class Cube
    {
        public Body body;
        public Geom geom;
        public Texture2D art;
        public float sizeX;
        public float sizeY;

        public Cube(Texture2D Art, float SizeX, float SizeY, float mass, PhysicsSimulator physicsSimulator)
        {
            art = Art;
            sizeX = SizeX;
            sizeY = SizeY;

            body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, sizeX, sizeY, mass);
            geom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, body, sizeX, sizeY);

            physicsSimulator.Add(body);
            physicsSimulator.Add(geom);

        }
        public void Update(PhysicsSimulator physicsSimulator, GameTime gameTime)
        {
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle decRec = new Rectangle((int)body.Position.X, (int)body.Position.Y, (int)sizeX, (int)sizeY);

            spriteBatch.Draw(art, decRec, Color.White);
        }
    }
}
Class Player:
namespace LastWar
{
    class Player
    {
        public AnimationSprite sprite;
        public Cube cube;
        public string curWeapon, animation;
        public Rectangle pos;
        public float speed, sizeWidth, sizeHight;
        public bool Alive;
        public SpriteEffects spriteEffect;

        public Player(int Speed, float SizeW, float SizeH)
        {
            speed = Speed;
            sizeHight = SizeH;
            sizeWidth = SizeW;
            curWeapon = "NoWeap";
        }
        public void GenCube(Vector2 Pos, float SizeW, float SizeH, float Mass, PhysicsSimulator physicsSimulator, Texture2D DeBugArt)
        {
            Console.WriteLine("IN GENCUBE");
            cube = new Cube(DeBugArt ,SizeW, SizeH, Mass, physicsSimulator);

            cube.body.Position = Pos;
            pos = new Rectangle((int)cube.body.Position.X, (int)cube.body.Position.X, 
                (int)sizeWidth, (int)sizeHight);

        }

        public void Update(KeyboardState keyboardState)
        {

            if (keyboardState.IsKeyDown(Keys.D) && (cube.body.LinearVelocity.Y == 0))
            {
                cube.body.LinearVelocity.X = speed;
                if (curWeapon == "NoWeap")
                {
                    sprite.curAnimation = 0;
                }
                else if (curWeapon == "AssultRifle")
                {
                    sprite.curAnimation = 1;
                }
                sprite.Update();
                spriteEffect = SpriteEffects.FlipHorizontally;
            }
            else if (keyboardState.IsKeyDown(Keys.A) && (cube.body.LinearVelocity.Y == 0))
            {
                cube.body.LinearVelocity.X = speed * -1;
                if (curWeapon == "NoWeap")
                {
                    sprite.curAnimation = 0;
                }
                else if (curWeapon == "AssultRifle")
                {
                    sprite.curAnimation = 1;
                }
                sprite.Update();
                spriteEffect = SpriteEffects.None;
            }
            else if (keyboardState.IsKeyUp(Keys.D) && keyboardState.IsKeyUp(Keys.A))
            {
                cube.body.LinearVelocity.X = 0;
                if (curWeapon == "NoWeap")
                {
                    sprite.curAnimation = 0;
                    sprite.curFrame = 0;
                }
                else if (curWeapon == "AssultRifle")
                {
                    sprite.curAnimation = 1;
                    sprite.curFrame = 0;
                }
            }
            
            Console.WriteLine("Player.Vos: " + cube.body.LinearVelocity.X);

            
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            sprite.Draw(spriteBatch, new Vector2(cube.body.Position.X -18, cube.body.Position.Y -8), spriteEffect); 
        }
    }
}
Class Map:
namespace LastWar
{
    class Map
    {
        public int[,] layer1;
        public Texture2D tileSheet;
        public int tileSizeWidth, tileSizeHight,
            mapXSize, mapYSize, mapOffsetX, mapOffsetY;
        public int mapX = 0, mapY = 0, setXCount, setYCount;

        public Cube[,] Tiles;

        public Map(Texture2D TileSheet, int[,] Layer1, int TileSizeWidth, int TileSizeHight,
            int MapXSize, int MapYSize, int SetXCount, int SetYCount)
        {
            tileSheet = TileSheet;
            layer1 = Layer1;
            tileSizeHight = TileSizeHight;
            tileSizeWidth = TileSizeWidth;
            mapXSize = MapXSize;
            mapYSize = MapYSize;
            setXCount = SetXCount;
            setYCount = SetYCount;
            
        }
        public void MapLoad(PhysicsSimulator physicsSimulator, Texture2D DeBugArt  )
        {
            Tiles = new Cube[mapYSize, mapXSize];
            for (int y = 0; y < mapYSize; y++)
            {
                for (int x = 0; x < mapXSize; x++)
                {
                    if((layer1[y,x] != 0 )||( layer1[y,x] != 1) )
                    {
                        
                        Tiles[y, x] = new Cube( DeBugArt,tileSizeWidth, tileSizeWidth, 1, physicsSimulator);
                        Tiles[y, x].body.IsStatic = true;
                       
                        
                    }
                    Console.WriteLine("X: " + x);
                }
                Console.WriteLine("Y: " + y);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            int TileToDraw;
            Rectangle socRec;
            Rectangle decRec;
            for (int y = 0; y < mapYSize; y++)
            {
                for (int x = 0; x < mapXSize; x++)
                {
                    TileToDraw = layer1[y + mapY, x + mapX];
                    if(TileToDraw > 1)
                    {
                        socRec = new Rectangle((TileToDraw % setXCount) * tileSizeWidth,
                            (TileToDraw / setXCount) * tileSizeHight, tileSizeWidth, tileSizeHight);
                        decRec = new Rectangle((x * tileSizeWidth) - mapOffsetX, (y * tileSizeHight) - mapOffsetY, tileSizeWidth, tileSizeHight);

                        spriteBatch.Draw(tileSheet, decRec, socRec, Color.White);
                    }
                }
            }
        }
    }
}
Did not show Animation because has nothing to do with Farseer. So yea it was working, but then it just stopped, any ideas would be a great help.
            spriteBatch.Draw(art, decRec, Col
Apr 27, 2010 at 4:06 AM

I'm having a very similar problem.  I have a very simple scene, one static circle and two dynamic objects (circle and rectangle) above it.  I use the BodyFactory and GeomFactory to create the objects, passing the physics simulator to the factory methods, and let them loose.  The two dynamic objects fall straight through the static object.

It should be as simple as creating the physics simulator, creating the bodies and geoms, and letting them go, right?

Apr 27, 2010 at 4:13 AM

@slygamer: Make sure you are passing your PhysicsSimulator when you create the Geometry and Body for the Static Object.

Apr 27, 2010 at 11:47 AM

Thanks for the reply, kmullins.  I've tried both that and the overloads that don't take a PhysicsSimulator and manually adding the body and geom to the physics simulator.

I removed my fiddling with collision categories, and I have it colliding with something now.  Not what I expected, but it's progress.

Apr 27, 2010 at 7:30 PM

@slygamer: Ahh okay. Yeah I haven't messed with collision categories yet. Good luck!