Example Using InsertEntity
I want to have two entities inserted into the world where one entity is a child of the other and therefore gets jointed to the parent. I have been able to do this at runtime using the method described in Simulation Tutorial 6, however I can not get it to work programatically. Here is the code I am using:
Code Snippet
Vector3 dimensions =newVector3(1, 1, 1);// meters
SingleShapeEntity parentBox =newSingleShapeEntity(newBoxShape(
newBoxShapeProperties(100,
// mass in kilograms.newPose(),// relative posedimensions)),
// dimensionsnewVector3(0, 1, 0));
parentBox.State.Name ="ParentBox";
SingleShapeEntity childBox =newSingleShapeEntity(newBoxShape(
newBoxShapeProperties(
100,// mass in kilograms.
newPose(newVector3(0, 1, 0)),// relative posedimensions)),
// dimensionsnewVector3(0, 2, 0));
childBox.State.Name ="ChildBox";
parentBox.InsertEntity(childBox);
SimulationEngine.GlobalInstancePort.Insert(parentBox);
When I run it, I get this initialization error for the childBox:
System.ApplicationException: Physics engine failed to create joint
at Microsoft.Robotics.Simulation.Engine.VisualEntity.CreateAndInsertPhysicsEntity(PhysicsEngine physicsEngine)
at Microsoft.Robotics.Simulation.Engine.SingleShapeEntity.Initialize(GraphicsDevice device, PhysicsEngine physicsEngine)
Does anyone have an example of how to correctly set up the parent/child relationship between these two boxes? Thanks!
It seems that you are using MSRS 1.5, in MSRS 1.5 you'll have to create a joint between the parentbox and the childbox. But if you use MSRS 1.5 (CTP May 2007) then it won't give you any error. Hope this helps, let me know if you need more help.
Courageous
I am using the MSRS 1.5. I was previously attaching pieces together manually with a joint in 1.0, but I was under the impression that in 1.5 that joint was now automatically created for you as in Simulation Tutorial 6. If you could show me an example of a scenario that works correctly, that would be great. Thanks.
Thank you for posting an example, however it looks like you are manually inserting a joint to connect two unrelated boxes together. I want to use the joint that is automatically created when you add one entity as a child of another. The joint that connects them would be the childBox.ParentJoint in my example.
I have figured out the problem. I am not sure if this is working as intended, but in order to insert an entity as a child of another entity, you must assign it an initial orientation.
Code Snippet
Vector3 dimensions = new Vector3(1, 1, 1); // meters
SingleShapeEntity parentBox = new SingleShapeEntity(new BoxShape( new BoxShapeProperties( 100,
// mass in kilograms. new Pose(), // relative pose dimensions)),
// dimensions new Vector3(0, 1, 0)); parentBox.State.Name =
"box";
SingleShapeEntity childBox = new SingleShapeEntity(new BoxShape(
new BoxShapeProperties(
100, // mass in kilograms.
new Pose(), // relative pose dimensions)),
// dimensions new Vector3(0, 2, 0)); childBox.State.Pose.Orientation =
new Quaternion(0, 0, 0, 1); childBox.State.Name =
"box[1]";
parentBox.InsertEntity(childBox);
SimulationEngine.GlobalInstancePort.Insert(parentBox);
Thank you for finding this bug for us. The problem is that a Quaternion is a struct and so it is initialized, by default, to all 0's. But (0,0,0,0) is an invalid quaternion. In the VisualEntity baseclass Initialize method, we check to see if the Pose Quaternion is invalid and set it to (0,0,0,1) if necessary. If you set a position or a non-default orientation for the child entity, the quaternion in the pose will be initialized correctly, or if you call Initialize before you call CreateAndInsertPhysicsEntity, the pose orientation is initialized correctly. Only in the case where no Pose is specified and CreateAndInsertPhysicsEntity is called before Initialize do you hit this problem.
We'll make sure that it is fixed in the next release. Sorry for the time it took you to track this one down!
-Kyle
I got to work that the joint is created automatically between the two boxes. But how can I adress to the created joint? I want to change the jointProperties. I tried to use the ParentJoint operation, but I got an error:
"System.NullReferenceExeption: Object reference not set to an instance of an object".
the error appears in this line:
childBox.ParentJoint.State.Linear.ZMotionMode = JointDOFMode.Free;
what is wrong? how can I manage this problem?
thanks for your help