Advertisement

Bulletsharp forces not applied

Started by June 19, 2015 08:59 AM
5 comments, last by Stefan Fischlschweiger 9 years, 6 months ago

I'm using Bulletsharp with SharpDX. I managed to move an object using RigidBody.Translate()

But I can't get for the heck of it get any forces applied.

Here I build the body:


public static RigidBody BuildBody(_3Df model, float mass)
        {
            var vertices = new List<Vector3>();
            var indices = new List<int>();

            foreach (var mesh in model.Meshes)
            {
                vertices.AddRange(mesh.Vertices.Select(vertex => new Vector3(vertex.Pos[0], vertex.Pos[1], vertex.Pos[2])));

                var meshIndices = mesh.GetIndices();
                indices.AddRange(meshIndices.Select(index => (int) index));
            }

            CollisionShape shape = new BvhTriangleMeshShape(new TriangleIndexVertexArray(indices.ToArray(), vertices.ToArray()), true);

            float mas;
            Vector3 vec;

            shape.GetBoundingSphere(out vec, out mas);

            var body = new RigidBody(
                new RigidBodyConstructionInfo(mass, new DefaultMotionState(), shape, shape.CalculateLocalInertia(mass)));

            return body;
        }

(Note the seemingly useless GetBoundingSphere call. Without that I get a System.AccessViolationException on the next line)

No matter which ApplyForce/Torque/Impulse function I call on the RigidBody it doesn't do anything

Not sure what is going on here as I've never used Bullet#.

I have used some Bullet some time ago however so.

  1. You must find why not calling GetBoundingSphere causes your program to bang;
  2. check mass value. If memory serves Bullet will consider static all objects with mass being 0...
  3. ... unless they're marked kinematic, and you might consider disabling sleeping as well.

If memory serves, the motion state will allow you to get signaled when the API updates transform of a dynamic object but it does not say anything about the object being kinematic.

If you use force/torque/impulse then you probably want the object to be dynamic. Make sure mass is != 0 and a meaningful value.

Previously "Krohm"

Advertisement


No matter which ApplyForce/Torque/Impulse function I call on the RigidBody it doesn't do anything

Where do you add the body to the world ?

@Krohm: The mass is 100.


No matter which ApplyForce/Torque/Impulse function I call on the RigidBody it doesn't do anything

Where do you add the body to the world ?

In case of the player ship, it happens within this code:


case "player":
                {
                    var entity = GameServices.GetService<EntityManager>().Create<EntityPlayerShip>();

                    entity.Position.Position = ship.Position;
                    entity.Rotation.Rotation = ship.Rotation;
                    entity.Archtype.Archtype = Nickname;
                    entity.Offset.Offset = CameraOffset;
                    entity.Render.Model = loader.LoadModel(Model, GameServices.GetService<RendererD3D11>().Shaders["defaultShader"].Signature, Mass);
                    entity.Velocity.TargetVelocity = Vector3.Zero;
                    entity.Tag = "player";
                    entity.Groups = EntityGroups.Collidable;
                    entity.Physics.SteeringTorque = SteeringTorque;

                    EquipmentLoader.LoadEquip(entity.Render.Model, ship.Position, ship.Rotation, ship.Loadout, entity.Id);

                        entity.Render.Model.Body.WorldTransform = Matrix.Translation(ship.Position) *
                                                                  Matrix.RotationQuaternion(ship.Rotation);
                        GameServices.GetService<PhysicsSystem>().World.AddRigidBody(entity.Render.Model.Body);
                    }
                    break;

So, I managed to make BulletSharp do something, however, while I can RigidBody.ApplyForce or RigidBody.ApplyImpulse to translate my ship I still can't for the heck of it make it rotate through RigidBody.ApplyTorque.

No matter what value I give to the ApplyTorque function it refuses to do any rotation

BvhTriangleMeshShape is for fixed/non-moving objects. See the docs here:
http://bulletphysics.org/Bullet/BulletFull/classbtBvhTriangleMeshShape.html#details

You can either use a GImpactMeshShape, which works fine for most cases:
https://github.com/AndresTraks/BulletSharp/blob/master/demos/Generic/GImpactTestDemo/GImpactTestDemo.cs

Or if you want slightly better performance, do convex decomposition (HACD), where you split a concave shape into convex shapes that are easier to compute and then combine them back into a CompoundShape:
https://github.com/AndresTraks/BulletSharp/blob/master/demos/Generic/ConvexDecompositionDemo

Or if your shape is already convex, you can use a ConvexHullShape directly.

Advertisement

BvhTriangleMeshShape is for fixed/non-moving objects. See the docs here:
http://bulletphysics.org/Bullet/BulletFull/classbtBvhTriangleMeshShape.html#details

You can either use a GImpactMeshShape, which works fine for most cases:
https://github.com/AndresTraks/BulletSharp/blob/master/demos/Generic/GImpactTestDemo/GImpactTestDemo.cs

Or if you want slightly better performance, do convex decomposition (HACD), where you split a concave shape into convex shapes that are easier to compute and then combine them back into a CompoundShape:
https://github.com/AndresTraks/BulletSharp/blob/master/demos/Generic/ConvexDecompositionDemo

Or if your shape is already convex, you can use a ConvexHullShape directly.

Yea I found that. For now I solved this by giving the rigidbody building code a "dynamic" value on which it decides to either use BvhTriangleMeshShape or ConverHullShape

This topic is closed to new replies.

Advertisement