Page 1 of 1

Scale & Translation work fine, rotation of Entities.. not

Posted: Fri Nov 29, 2013 8:04 am
by Insanto
Hey,
let me start by saying how much i like bepu and it's ease of integration (once one figures out the center of mass thing for entities ;)

But now I've run into a rather odd problem.
Scenario:
- Physics is set up but not calling the update
- StaticMeshes are created and added
- Entities are created and added
- Entities are transformed (Translation and rotation)
- Update is called

Once the update is called the rotation is reset as can be seen on the image below:
Image
Image

Code:

Code: Select all

  public class PhysicsRigidBodyComponent : BaseComponent
  {

    private Entity physMesh;
    public Entity PhysMesh
    {
      get { return physMesh; }
      set { physMesh = value; }
    }

    [NonSerialized]
    private Matrix localOffsetTransform;

    private float mass = 10f;
    public float Mass
    {
      get { return mass; }
      set { mass = value; }
    }



    public override void PostInitComponent()
    {
      base.PostInitComponent();

      //set up the physmesh from the renderComponent's model
      if (GameObject != null)
      {
        var renderC = GameObject.GetComponent<RenderComponent>();

        if (renderC != null && renderC.Model != null)
        {
          physMesh = CreatePhysicsMesh(renderC.Model);
          physMesh.PositionUpdated += physMesh_PositionUpdated;

          GameObject.Level.PhysicalWorld.Add(physMesh);
        }
        //else
        //{
        //  throw new Exception("Rendercomponent not found or it has no model");
        //}
      }
    }

    private bool isPhysMeshTMUpdate = false;

    void physMesh_PositionUpdated(Entity obj)
    {
      isPhysMeshTMUpdate = true;
      GameObject.GlobalTransform = localOffsetTransform *  physMesh.WorldTransform;
    }

    public override void OnTransformChanged()
    {
      base.OnTransformChanged();

      //if the physMesh didn't change the transform something else did.. so have the physMesh follow the object
      if (!isPhysMeshTMUpdate && physMesh != null)
        physMesh.WorldTransform = GameObject.GlobalTransform;

      isPhysMeshTMUpdate = false;
    }

    private Entity CreatePhysicsMesh(Model model)
    {
      Vector3[] vertices;
      int[] indices;

      TriangleMesh.GetVerticesAndIndicesFromModel(model, out vertices, out indices);

      var tm = new AffineTransform(
          new Vector3(1 * 0.01f),
          Quaternion.Identity,
          Vector3.Zero);

      var convexHullShape = new MobileMeshShape(vertices, indices, tm, MobileMeshSolidity.Counterclockwise);

      localOffsetTransform = convexHullShape.Transform.Matrix; //centerOfMass offset

      Entity mesh;
      if (mass != 0)
        mesh = new Entity(convexHullShape, mass);
      else
        mesh = new Entity(convexHullShape);

      mesh.WorldTransform = GameObject.GlobalTransform;

      return mesh;
    }

    protected override void DestroyComponent()
    {
      if (physMesh != null)
        GameObject.Level.PhysicalWorld.Remove(physMesh);

      base.DestroyComponent();
    }

  }
I've got no idea why this is happening, maybe someOne already ran across something like this and can point me in the right direction?

Re: Scale & Translation work fine, rotation of Entities.. no

Posted: Fri Nov 29, 2013 6:47 pm
by Norbo
Nothing jumps out at me as a cause of orientation reset. Updating the Space will never reset orientations by itself. Checking the physical entities' orientations after they appear to reset might help. If the entities don't actually have a reset orientation, it implies a graphical issue. If they do have a reset orientation, then you can search for all references to the entity.Orientation/OrientationMatrix/WorldTransform and see if anything is setting it incorrectly.

By the way, MobileMeshes are generally a lot slower than simpler approximations. A BoxShape will be much faster than 12 triangles set up to look like a box. In terms of performance vs shape fidelity, the ranking goes roughly like this:
Max performance
-BoxShapes, SphereShapes, and other simple implicit primitives
-ConvexHullShapes based on the model (preferably a simplified version)
-CompoundShapes of simple primitives
-CompoundShapes of ConvexHullShapes
-MobileMeshShapes
Max fidelity