NEWBIE TO BEPU

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
lomiwong
Posts: 2
Joined: Wed Aug 25, 2010 8:57 am

NEWBIE TO BEPU

Post by lomiwong »

hi ive downloaded bepuphysics and is trying to use it in some game i am trying to make.
i am having a hard time figuring out how to use this in the game.

i have added the space already in the game
but how do i add collision,physics to my FBX files?

any information is very much important.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: NEWBIE TO BEPU

Post by Norbo »

I'd recommend the getting started documentation. This will give a quick overview of the basics, and touch on a few fancier features that may come in handy.

You can find it on the documentation page: http://www.bepu-games.com/BEPUphysics/documentation.htm

For a static mesh, you can collect the vertices and indices from a loaded Model using the StaticTriangleGroup.GetVerticesAndIndicesFromModel, or pull them out in a content processor. A StaticTriangleGroup can be created from that data. StaticTriangleGroups are bunches of unmoving triangles with acceleration structures built in to make it run quick.

For an object that moves according to physics, you could technically create a CompoundBody out of a bunch of Triangle entities, but that isn't recommended. The more entities you have, the slower things are going to be. A better approach would be to create a small set of ConvexHulls representing the model and put them in a CompoundBody. That way, the shape has volume rather than being just a shell.

The simulation most likely doesn't require that level of fidelity. Approximate things whenever possible. For example, you could use normal primitives like Boxes to simulate some meshes. For more complicated (concave) types, you could look into using multiple primitives in a CompoundBody.
lomiwong
Posts: 2
Joined: Wed Aug 25, 2010 8:57 am

Re: NEWBIE TO BEPU

Post by lomiwong »

Thank you. One last question can you please show use oF bepu with just one model. Simplicity first. In the sampledemo in the website it already uses lots of techniques though i was able to use it. but starting from scratch is hard using your demo project. Please show me simple use only 1 fbx 1 entity movement. Thank you for the quick reply. Helps but still figuring out the means to your Engine. Kudos on the engine though its very good.
Mercurial
Posts: 8
Joined: Wed Aug 25, 2010 9:37 am

Re: NEWBIE TO BEPU

Post by Mercurial »

Heres a really simple one...

Code: Select all


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 Innobento.Engine.Classes;
using BEPUphysics.Entities;
using BEPUphysics;

namespace sample
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        InnoCamera camera; // my custom camera object
        Space space;
        Entity entity;
        Entity sphere;


        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
            camera = new InnoCamera(this,new Vector3(0,0,20),Vector3.Zero,Vector3.Up);
//Initialize the Space Object for the game
            space = new Space();
// Set Gravity to -9.8f physics law?
            space.SimulationSettings.MotionUpdate.Gravity = new Vector3(0, -9.8f, 0);

//Create Box Entity
            entity = new Box(BEPUphysics.DataStructures.MotionState.DefaultState,3,3,3);
//Set Box properties, here we make the box static. I dunno if theres a better way of doing this though. default entity location will be Vector3.Zero
            entity.IsAffectedByGravity = false;
            entity.IsActive = false;
//Create Sphere Entity and set its properties
            sphere = new Sphere(BEPUphysics.DataStructures.MotionState.DefaultState,1,1);
            sphere.Mass = 5;
            sphere.Bounciness = 0.5f;
            sphere.Density = 1.0f;
//Set the Initial Position of the Sphere
            sphere.WorldTransform = Matrix.CreateTranslation(new Vector3(0, 10, 0));

// Add the entities to the physics system
            space.Add(sphere);
            space.Add(entity);


            Components.Add(camera);
        
            

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


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

            // TODO: Add your update logic here
          
//Update the Space Object - Physics System?
            space.Update(gameTime);
            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);
//Basically a class that renders bounding boxes. we get the boxes from the entities so we can view it visually.
            InnobentoHelper.BoundingBoxRenderer.Render(entity.BoundingBox,GraphicsDevice , camera.view, camera.projection, Color.White);
            InnobentoHelper.BoundingBoxRenderer.Render(sphere.BoundingBox, GraphicsDevice, camera.view, camera.projection, Color.Red);


      

            
            // TODO: Add your drawing code here
            base.Draw(gameTime);
        }
    }
}
study the code it will help I guess XD
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: NEWBIE TO BEPU

Post by Norbo »

As an addendum to the above sample, here's a couple of things:

Code: Select all

            //Create Box Entity
            entity = new Box(BEPUphysics.DataStructures.MotionState.DefaultState,3,3,3);
            //Set Box properties, here we make the box static. I dunno if theres a better way of doing this though. default entity location will be Vector3.Zero
            entity.IsAffectedByGravity = false;
            entity.IsActive = false;
If you do not pass a mass into the entity constructor, it is kinematic to begin with. You don't need to worry about setting IsAffectedByGravity or IsActive. IsAffectedByGravity only does something on entities with less than infinite mass (so not kinematic entities). Since the kinematic entity won't move unless specifically told to do so, you also don't n eed to set it to inactive (it will go inactive immediately if appropriate). Setting a dynamic entity to inactive would also just be temporary- once something new collided with it, it would wake up.

Code: Select all

//Create Sphere Entity and set its properties
            sphere = new Sphere(BEPUphysics.DataStructures.MotionState.DefaultState,1,1);
            sphere.Mass = 5;
            sphere.Bounciness = 0.5f;
            sphere.Density = 1.0f;
//Set the Initial Position of the Sphere
            sphere.WorldTransform = Matrix.CreateTranslation(new Vector3(0, 10, 0));
Rather than setting the world transform/mass outside of the constructor, you can do that in the constructor. Right now, it sets the sphere's mass to 1, and then immediately afterward sets it to 5. Note that there is a overload of the constructor that takes a position instead of a motionstate as well.

Density is an auxiliary field used to tune things like buoyancy, and setting it will not directly change the mass or volume of an entity. It also has a default value computed by the initial mass and volume of the entity, so you don't have to set it if you don't want to.

If you want another super-duper bare minimum example, this creates a dynamic box above a flat kinematic box and adds them to the space:
Space.Add(new Box(new Vector3(0, 5, 0), 1, 1, 1, 1));
Space.Add(new Box(new Vector3(0, 0, 0), 10, 1, 10));
Please show me simple use only 1 fbx 1 entity movement.
If by fbx you mean having a loaded fbx graphic follow the entity, then you can use the entity's WorldTransform. Whatever way you choose to draw it (e.g. BasicEffect) should have some method to set the world transform of the graphic, which can be the entity.WorldTransform. This is basically a rendering issue; you can check the BasicSetupDemo for a simple approach, but there's a huge number of ways to handle it.

If you mean loading in a static collision mesh that other stuff collides with, then you should use a StaticTriangleGroup. Here's the loading process copied from the PlaygroundDemo in the BEPUphysicsDemos:

Code: Select all

            //Load in mesh data and create the group.
            StaticTriangleGroup.StaticTriangleGroupVertex[] staticTriangleVertices;
            int[] staticTriangleIndices;

            var playgroundModel = game.Content.Load<Model>("playground");
            //This load method wraps the TriangleMesh.getVerticesAndIndicesFromModel method 
            //to output vertices of type StaticTriangleGroupVertex instead of TriangleMeshVertex or simply Vector3.
            StaticTriangleGroup.GetVerticesAndIndicesFromModel(playgroundModel, out staticTriangleVertices, out staticTriangleIndices);
            var mesh = new TriangleMesh(staticTriangleVertices, staticTriangleIndices, 0);
            var group = new StaticTriangleGroup(mesh);

            Space.Add(group);
Basically, the StaticTriangleGroup is created with a TriangleMesh object. TriangleMesh needs a vertex list and index list- it doesn't care where they come from. In the above, they are created by using the GetVerticesAndIndicesFromModel method. Another option would be setting the vertex and index list up in a content processor.
Mercurial
Posts: 8
Joined: Wed Aug 25, 2010 9:37 am

Re: NEWBIE TO BEPU

Post by Mercurial »

thanks for the explanation I understand it more now.
Post Reply