Setting up space right.

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
bobbin
Posts: 4
Joined: Tue Mar 24, 2009 11:01 am

Setting up space right.

Post by bobbin »

Hi all im a new user to bepu and im trying to just get a simple box to fall due to gravity to test it out, i am using a very basic,
almost default scene which i have added a physics space too and added a box entity to the space. i am then drawing my model (Which is
rendered using my own quick model class) at the centre of mass of the entity in the physics space hoping that this would update each
cycle and the ox would sloly fall due to gravity. the entity itself is not moving at all and i am lost as to why.

this is my initialization of the physics system, like i said i am just using demo code:
and in my update function i am just calling space.Update(gameTime)

Code: Select all

protected override void Initialize()
        {
            gameCamera = new Camera();
            ground = new GameObject();
            displayCube = new GameObject();

            //physics
            space = new Space(new PersistentUniformGrid(10));
            space.simulationSettings.gravity = new Vector3(0, -9.81f, 0f); //If left unset, the default value is (0,0,0).

            space.simulationSettings.timeScale = 1f; //If left unset, the default value is 1.
            space.simulationSettings.iterations = 15; //If left unset, the default value is 15.
            //Fiddling with margins and position correction is one way to tune the engine for the specific game/simulation.
            space.simulationSettings.defaultMargin = .04f;//Defaults to .04f
            space.simulationSettings.defaultAllowedPenetration = .025f;//Defaults to .025f
            space.simulationSettings.useSplitImpulsePositionCorrection = false; //Defaults to false.

            toAddBox = new Box(new Vector3(0, 10, -5), 5.0f, 5.0f, 5.0f);
            //toAddBox.isPhysicallySimulated = true;
                        
            space.add(toAddBox);
            space.entities[0].isAlwaysActive = true;
            space.entities[0].applyLinearImpulse(new Vector3(0,0.05f,0));


            base.Initialize();
        }

Currently this displays my box model in the initial position but it stays there, when debugging it seems that the position of the entity does not move either.
i was under the impression the entity would be affected by gravity and just start falling, am i wrong? have i set something up wrong?

Thanks for any answers,

Bobbin
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Setting up space right.

Post by Norbo »

The box you defined is kinematic and has 'infinite' mass. If you want to make the object dynamic, either specify a mass in another parameter to the constructor of the box (recommended) or use entity.makePhysical(mass).

And just to clarify, you can remove those property assignments for the simulation settings if you want since they are all given valid default values. Stripping down the code yields:

Code: Select all

            space = new Space();
            space.simulationSettings.gravity = new Vector3(0, -9.81f, 0f);

            toAddBox = new Box(new Vector3(0, 10, -5), 5.0f, 5.0f, 5.0f, 10);
                        
            space.add(toAddBox);
bobbin
Posts: 4
Joined: Tue Mar 24, 2009 11:01 am

Re: Setting up space right.

Post by bobbin »

Thanks Norbo for the quick reply :)

I didn't even think of mass, i didn't see any reference to it in the first demo scene which is what i was basing it on. I see my falling box now! I set a box with no mass for the imovable ground plane, is this the best type to use?

I kept the default values in my initialization so i knew what the default values were and i could play around with them when i got a scene working :)

Anyways now my task is to link this to my model/object class so each entity can draw itself. Does anyone have any tips to doing this, i'm having trouble designing the classes so they use the physics engine.

My thought was to make my model class inherit from the bepu 'Entity' and then would i simply be able to add my model class to the physics 'space'?
space.add(myModel);
and then create a physicManager perhaps that handles adding entities to the space, and updating the physics system. My only concern with this is that unless i render all my models straight from the physics manager i would need another list of them as well and this seems like a waste of memory? maybe im wrong, i havent been programming for a long time hehe

thanks guys

Bobbin
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Setting up space right.

Post by Norbo »

Boxes do tend to be the best planes, though right now there may be robustness issues with extremely large boxes against other types of geometry (like box-cylinder). Box-box and box-sphere will still look fine with a huge box plane since they use a more stable special case. 100x100 should be fine in just about every case though.

For the class design, I personally like thinking of the physics Entity as a part of a game engine's Entity (whatever you call it). So instead of creating a subclass of BEPUphysics.Entity, you have an object that contains a BEPUphysics.Entity. This just makes a bit more sense to me as the physics is a part of the description of the object rather than the gameplay definition itself.

In the stripped down project in this post http://www.bepu-games.com/forums/viewto ... aded#p4261 (primarily designed to show offloading the engine onto its own thread), you can see how I set up a basic renderer/game object (EntityModel) that transforms a model to the position of the object's contained entity.
bobbin
Posts: 4
Joined: Tue Mar 24, 2009 11:01 am

Re: Setting up space right.

Post by bobbin »

thanks again for the advice, im off to band practice now but i'm leaving the page open for when i get back so i can have a read and get right back into it :)
bobbin
Posts: 4
Joined: Tue Mar 24, 2009 11:01 am

Re: Setting up space right.

Post by bobbin »

Your example was very helpful in understanding better how to associate a model with a physics entity. I have made my own class and i add with space.Add(PhysicsObject.physicsEntity) and it seems to work well, i have a simple demo setup similar to your one that just dumps random placed boxes everywhere.

Below is a shot of my demo, i have it drawing the Bounding boxes of the models so i can see whats happening.
Image

At the moment the class only attaches Box entities to the model, because my game mainly will be using bricks and stones. But i will have to change that at some point.

Code: Select all

public class PhysicsObject  
    {

        public Vector3 Position;
        public Entity physicsEntity;
        private Matrix transform;
        private Matrix[] transforms;
        private BoundingBox boundingBox;
        private Game thisGame;
        public bool drawBoundingBox { get; set; }

        public PhysicsObject(Vector3 pos, Model model, Game game)
        {
            drawBoundingBox = false;
            this.thisGame = game;
            this.Position = pos;
            this.Model = model;
            this.boundingBox = CreateBoundingBox();
            
            Box tempBox = new Box(pos, this.boundingBox.Max.X - this.boundingBox.Min.X, this.boundingBox.Max.Y - this.boundingBox.Min.Y, this.boundingBox.Max.Z - this.boundingBox.Min.Z, 10f);

            //create bounding box from model and use it for our physics entity
            this.physicsEntity = tempBox;
            this.transform = Matrix.CreateScale(tempBox.width,tempBox.height,tempBox.length);

            transforms = new Matrix[model.Bones.Count];
        }


        public void Draw(Matrix view, Matrix projection)
        {
            Matrix worldMatrix = transform * physicsEntity.orientationMatrix * Matrix.CreateTranslation(physicsEntity.centerPosition);

            Model.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in Model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
                    effect.View = view;
                    effect.Projection = projection;

                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                }
                mesh.Draw();
            }

            if (drawBoundingBox)
            {
                BoundingBox temp = new BoundingBox(physicsEntity.boundingBox.Min, physicsEntity.boundingBox.Max);

                BoundingBoxRenderer.Render(temp, thisGame.GraphicsDevice, view, projection, Color.Red);
            }
        }

        //creates a bounding box for the loaded model
        private BoundingBox CreateBoundingBox()
        {
            BoundingBox tempBox = new BoundingBox();

            foreach (ModelMesh mesh in this.Model.Meshes)
            {
                VertexPositionNormalTexture[] vertices =
                  new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes];

                mesh.VertexBuffer.GetData<VertexPositionNormalTexture>(vertices);

                Vector3[] vertexs = new Vector3[vertices.Length];

                for (int index = 0; index < vertexs.Length; index++)
                {
                    vertexs[index] = vertices[index].Position;
                }

                tempBox = BoundingBox.CreateMerged(tempBox,
                  BoundingBox.CreateFromPoints(vertexs));
            }

            return tempBox;
        }  
    }
How do u go about loading a custom collision mesh at the same time as your custom model? is ti a case of having 2 seperate files, one for collision and one for display? I mean instead of creating a Box entity that surrounds the extremities of a model that i load in.

Any critiques of my implementation are welcome :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Setting up space right.

Post by Norbo »

Using two models is a common method. The ConvexHull entity type can be used to give a tighter fit to a model's geometry, though using a lower resolution mesh to create the ConvexHull will speed things up a bit. Another method is defining a set of simple shapes and giving them to a CompoundBody. This would let you create concave objects.
Post Reply