Question about setting entity.Parent
Hi,
Firstly im rather new at msrs, so this could be an obvious mistake. I am writing a custom entity, and in its initialize function it creates two SingleShapeEntities. I am trying to set one of these entities parent to the other one, so that position coordinates are relative to the other objects position.
Code Snippet
Vector3
pos =newVector3(0, 0, 0);BoxShapeProperties shape =null;shape =
newBoxShapeProperties(0f,newPose(),newVector3(0.3f, 0.05f, 0.05f));shape.Material =
newMaterialProperties("domino", 0.2f, 0.5f, 0.5f);SingleShapeEntity entity =newSingleShapeEntity(newBoxShape(shape), pos);entity.State.Name =
"Climber";entity.State.Assets.Mesh =
"climber_shorter.obj";entity.State.MassDensity.Mass = 0;
entity.State.Pose.Position =
this.State.Pose.Position;BoxShapeProperties shape2 =null;shape2 =
newBoxShapeProperties(0f,newPose(),newVector3(0.05f, 0.05f, 0.05f));shape2.Material =
newMaterialProperties("domino", 0.2f, 0.5f, 0.5f);Vector3 pos2 =newVector3(0, 0.1f, 0);SingleShapeEntity entity2 =newSingleShapeEntity(newBoxShape(shape2), pos2);entity2.State.Name =
"ClimberTruck";entity2.State.Assets.Mesh =
"trucker.obj";entity2.Parent = entity;
SimulationEngine.GlobalInstancePort.Insert(entity);SimulationEngine.GlobalInstancePort.Insert(entity2);When using the code above the objects appear to be visually correct, but in physics view, the collision object for shape2 is not in the same place as the mesh is being drawn. If i remove the entity2.parent = entity line and set a world coordinate location for entity2, it works fine. Any ideas what im doing wrong?
I have also tried entity.Children.Add(entity2) and i get the same behaviour, the physics objects end up at 0,0,0 as an absolute coordinate, rather then a coordinate relative to their parent.
[4728 byte] By [
Daedalus] at [2008-2-20]
Well setting the parent alone is not enough, you will have to use entity.Children.Add(entity2) in most cases.
What position is this.State.Pose.Position at when you are doing this?
Also, funny things happen when you set a mass of 0 to these objects. Make sure it's at least 1 when you're testing things.
This seems to be an interesting problem. I can see nothing immediately wrong with your code, but I'll give it some thought.
Have done some more tests now, and it still makes little sense.
From what ive done it seems that doing this.Children.Add(entity) inserts entity into the scene, so the call to simulation.GlobalInstancePort.Insert(entity) is redundant in the code above, as it will be inserted when the parent is inserted. Doing this twice results in one copy of the objects visually, but twice as many physics objects being inserted into the scene.
With setting mass=1 the physics objects behave in exactly the same manner, being totally disconnected from the graphical representation. I have now repeated my experiments without meshes, and cleaned up the code a bit, this snippet is exactly what i am using.
Code Snippet
public
override void Initialize(Microsoft.Xna.Framework.Graphics.GraphicsDevice device, PhysicsEngine physicsEngine) {
CreateShapes();
base.Initialize(device, physicsEngine); }
private void CreateShapes() {
Vector3 pos = new Vector3(0, 0, 0); BoxShapeProperties shape = null; shape =
new BoxShapeProperties(1f, new Pose(), new Vector3(0.3f, 0.05f, 0.05f)); shape.Material =
new MaterialProperties("domino", 0.2f, 0.5f, 0.5f); SingleShapeEntity entity = new SingleShapeEntity(new BoxShape(shape), pos); entity.State.Name =
"Climber"; BoxShapeProperties shape2 = null; shape2 =
new BoxShapeProperties(1f, new Pose(), new Vector3(0.05f, 0.05f, 0.05f)); shape2.Material =
new MaterialProperties("domino", 0.2f, 0.5f, 0.5f); Vector3 pos2 = new Vector3(0, -0.2f, 0); SingleShapeEntity entity2 = new SingleShapeEntity(new BoxShape(shape2), pos2); entity2.State.Name =
"ClimberTruck"; entity.Children.Add(entity2);
this.Children.Add(entity); }
The this entity which creates these shapes is created at position {0,2,0}
so from my understanding entity should be rendered at {0,2,0}
and entity2 should be rendered at {0,1.8,0}
what is actually happening is the graphics for entity is in the correct place, the graphics for entity2 is at {0, -0.2,0}
and the physics objects for entity and entity2 are lying on the ground plane, completely disconnected from the graphics (moving them with the physics camera has no bearing on the visual location).
If i could figure out how to post pics on this forum i would, but it doesnt seem to be possible.
Screenshots of the problem are available at:
http://www.personal.rdg.ac.uk/~sip04tas/msrs_notes.html
I might have found one issue, i hadnt set a physics object for the base entity, assuming that this wasnt a requirement. Have done so now, and two out of 3 objects have physics and graphics in allignment.
The entities position coordinates are still not relative to each other though.
Your right about creating shapes with a mass of 0, it kicks up errors in the console view, just didnt spot them before.
From looking in the debugger, if you do:
Climber.Children.Add(ClimberTruck);
this.Children.Add(Climber); base.Initialize(device, physicsEngine); The base.initalize() function causes the physics objects of the child entities to be created, but they are not connected to the entities (different locations).
If instead you do:
Climber.Children.Add(ClimberTruck);
this.Children.Add(Climber); ClimberTruck.Initialize(device, physicsEngine);
Climber.Initialize(device, physicsEngine);
base.Initialize(device, physicsEngine);
you get much the same response, except now you have twice the objects
the closest i have gotten is:
base
.Initialize(device, physicsEngine); this.Children.Add(Climber); Climber.Initialize(device, physicsEngine);
Climber.Children.Add(ClimberTruck);
ClimberTruck.Initialize(device, physicsEngine);
where this entity and climber entity have working physics, and climber truck is disconnected.
On every single attempt the coordinates are not relative to the parent entity.
Alan Oursland's posts about his pendulum entity seem to indicate that there are problems with joints and child entities, so i think i am going to follow his lead, and go the kuga route with regard to child entities.
It's kind of tricky playing around with the entities, joints, and parent-child relationships. I have no intimate knowledge of the inner workings of the physics engine or the graphics engine, and so I cannot tell you what exactly I did to solve the problems you're having. I, too, have had occasions where the visual and physics representations where completely disjoint from each other. Usually this means that there is some error in creating the entity, but I never could figure out what. I just tried a bunch of different things until I got the order/numbers/functions down.
If I figure anything out, I'll let you know. In the mean time, good luck and keep us updated =).
You have found an inconsistency in the way children entities work in the simulator. (That is a nice way of saying "bug"). We are going to have a nice solution for this in our 2.0 release but for now, here is a description of the problem and a work-around you can use.
Child entities work differently than you might expect because the position and orientation of every entity that has a physics shape is controlled by the physics engine. The phyics engine has no notion of entity hierarchy. It calculates the position of each entity only according to the forces that are acting on it. This means that if you have two cubes with non-zero mass and you set one as the child of the other with a particular position, the child cube will initially start out in that position relative to its parent but gravity will affect the child cube and it will fall to the ground. Then, if you move the parent entity, the child entity will not follow because there is no force acting on the child entity from the perspective of the physics engine.
The way to solve this problem is to specify a relationship between the parent and child entities that the physics engine can understand. Going back to the cube example, if there was a way to form a bond between the two cubes, the physics engine would operate on them as if they were a single object. This bond is a joint.
In the 2.0 release (which will be the next release from the MSRS team), child entities will be "glued" to their parent entities with a joint that has no axes of freedom. You will be able to specify the relative position of the child to the parent and the physics engine will treat both entities as a single object.
You can accomplish much the same thing with MSRS today by forgetting all about child entities for now and simply creating separate entities and then joining them together with a joint. Simulation Tutorial 4 shows how to create separate entities and then join them together using joints.
-Kyle
Hi,
Thanks for the clarification on this. I think there are more problems involved then simply missing joints. I would have understood if the child entities all fell to the floor, and their mesh / geometry was drawn on the floor, but for some reason the physical entities seem to be detached from the graphical representation of the models when creating children. Also i haven't tested this very thoroughly, but i would have expected should the objects start in the correct location, for me to initially see the physics entities fall / bounce as gravity is applied (my objects start at y=2), rather then being stationary at the y=0 plane.
I have ignored child entities in my project, and have a working system by using the Kuga arm approach to child entities, so im not stuck. Hopefully when 2.0 comes out i can refactor to using children.
One thing that would be useful when this functionality comes in would be an example showing inserting a child entity and what you are required to do with regard to calling initialise/update on child entities. Earlier in this thread i posted a few variations on this, and getting the wrong order can cause problems.
Tom