physics object creation class help.

Topics: User Forum
May 27, 2009 at 11:38 PM

I created a class that can easily generate a physics object. here it is:

 

 class PhysicsObject
    {
       
        public Vector2 Origin = new Vector2();
        public Texture2D texture;
        public float rotation = new float();
        Geom ObjectGeom;
        Body ObjectBody = new Body();
        public Vector2 position = new Vector2();
        public PhysicsSimulator physicsSim;
        public float radius;
        public float mass;
        Vertices verts = new Vertices();
        public bool Static;

        public PhysicsObject(Texture2D loadedTexture)
        {
            position = new Vector2();
            ObjectBody.Position = position ;
            physicsSim = new PhysicsSimulator(new Vector2(0, 300));
            texture = loadedTexture;
            Origin = new Vector2(texture.Width, texture.Height);
            ObjectBody.Rotation = rotation;
            ObjectBody.IsStatic = Static;
        }

        public void CreateCircle()
        {
            ObjectBody = BodyFactory.Instance.CreateCircleBody(physicsSim, radius, mass);
        }
        public void CreateBox()
        {
            ObjectBody = BodyFactory.Instance.CreateRectangleBody(physicsSim, texture.Width, texture.Height, mass);

        }
        public void CreatePoly()
        {
            uint[] data = new uint[texture.Width * texture.Height];
            texture.GetData(data);
           
            verts = Vertices.CreatePolygon(data, texture.Width, texture.Height);
            Origin = verts.GetCentroid();
            ObjectBody = BodyFactory.Instance.CreatePolygonBody(physicsSim, verts, mass);
            ObjectGeom = GeomFactory.Instance.CreatePolygonGeom(physicsSim, ObjectBody, verts, 0);
            ObjectGeom.FrictionCoefficient = 0.0f;


        }
       
       
    }

 

i call the physicsSim to update in Game1.cs but it doesn't work, the texture draws in the correct position but it does not react at all, it just sits there, it will not collide with anything either.

Coordinator
May 27, 2009 at 11:44 PM

Collision should only work if you use CreatePoly(). Both the circle and box does not create a geometry.

Also, you should only have one instance of PhysicsSimulator. If I were you, I would create a PhysicsSimulator instance inside my Game class (Game.cs) and the PhysicsObject would contain a Body, Geom and Texture2D object.

You can also include the DrawingSystem folder and the PhysicsSimulatorView.cs file from the samples into your project. Then you create a PhysicsSimulatorView object and you should be able to see the internals of the physics engine (debug view). Just like in the samples when you press F1.