The parent.addchild behaviour is ok.
The child setparent may has bug.
Here is a small example: Conveyor is a component. My original code has bounding box and other check. I have removed them from this example.
[code]
SharedPtr conveyor = CreateConveyor(“Conveyor1”, Vector3(0,0,0));
//Add 100 Jacks nodes onto Conveyor1
for (int i = 0; i < 100; ++i)
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
SharedPtr<Node> jackNode(scene_->CreateChild("Jack"));
jackNode->SetPosition(pos + Vector3(0,0, -i*4.0f));
AnimatedModel* modelObject = jackNode->CreateComponent<AnimatedModel>();
modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl"));
Material *mat = cache->GetResource<Material>("Materials/Jack.xml");
modelObject->SetMaterial(mat);
modelObject->SetCastShadows(true);
jackNode->CreateComponent<AnimationController>();
conveyor->AddNode(jackNode);
}
SharedPtr conveyor2 = CreateConveyor(“Conveyor2”, Vector3(20, 0, 20));
conveyor->ConnectTo(conveyor2);
SharedPtr NVuDuMain::CreateConveyor(String name, Vector3 pos)
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
SharedPtr<Node> node(scene_->CreateChild("Jack"));
SharedPtr<AnimatedModel> modelObject (node->CreateComponent<AnimatedModel>());
SharedPtr conveyor( node->CreateComponent());
node->SetPosition(pos);
node->SetName(name);
modelObject->SetModel(cache->GetResource(“Models/Jack.mdl”)); //Conveyor is a jack :).
// modelObject->SetMaterial(cache->GetResource(“Materials/Jack.xml”));
modelObject->SetCastShadows(true);
VariantVector lstInputs;
VariantVector lstOutputs;
Conveyor* mainComponent;
node->SetVar(VAR_INPUTS, lstInputs);
node->SetVar(VAR_INPUTS, lstOutputs);
return conveyor;
}
void Conveyor::Update(float timeStep)
{
float length = 10.0f;
Vector3 mPos = node_->GetPosition();
Vector3 delta = (Vector3::FORWARD*timeStep * speed_);
//node_->SetRotation(node_->GetRotation()*Quaternion(0, timeStep * 10, timeStep * 10));
Node* previousNode;
BoundingBox preBound;
for (int i = 1; i < node_->GetNumChildren(); ++i)
{
Node *n = node_->GetChild(i);
Vector3 childLocalPos = n->GetPosition();
previousNode = node_->GetChild(i - 1);
Vector3 previousNodePos = previousNode->GetPosition();
//String modelType = n->GetComponents()[0]->GetTypeName();
if (length_ - childLocalPos.z_ - delta.z_ >= 0)
{
n->Translate(delta);
status_ = Status::Travel;
}
else
{
n->Translate((Vector3::FORWARD*length_ - childLocalPos));
this->MoveToNext(n); //This calls n->SetParent( conveyor2Node)
}
}
}[/code]