Scale & Translation work fine, rotation of Entities.. not
Posted: Fri Nov 29, 2013 8:04 am
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:


Code:
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?
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:


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