#include "CTKFixe.h"
#include "CMountain.h"
//#include "CTronconRM.h"

CTKFixe::CTKFixe(scene::ISceneManager *smg,video::IVideoDriver* dr)
{

    smgr=smg;
    driver=dr;
    meshDepart = smgr->getMesh("media/departTKFixe.obj");
    materialDepart.setTexture(0,driver->getTexture("media/texture-pylone.png"));

    meshPoulieDepart = smgr->getMesh("media/poulieTK.obj");
    materialPoulieDepart.setTexture(0,driver->getTexture("media/texture-pylone.png"));

    meshPoulieArrivee = smgr->getMesh("media/poulieTK.obj");
    materialPoulieArrivee.setTexture(0,driver->getTexture("media/texture-pylone.png"));
    posMonteePoulieDepart=0;
    posDescentePoulieDepart=0;

    meshArrivee= smgr->getMesh("media/baseArriveeTK.obj");
    materialArrivee.setTexture(0,driver->getTexture("media/texture-pylone.png"));

    nodePoulieDepart=0;
    nodePoulieArrivee=0;

    posMonteePoulieArrivee=0;
    posDescentePoulieArrivee=0;

    typeRM=RM_TKF;
    constructionFinie=false;
    proprietesOuvert=false;
    vitesse=2.0f;
    vitesseMax=2.0f;
    espacementVehicules=20.0f;
}

void CTKFixe::SetPositionDepart(core::vector3df p)
{
	positionDepart=p;
}


void CTKFixe::SetPositionArrivee(core::vector3df p)
{
	positionArrivee=p;
}

core::vector3df CTKFixe::GetPositionDepart() {
    return positionDepart;
}

core::vector3df CTKFixe::GetPositionArrivee() {
    return positionArrivee;
}

//Methode a surcharger pour chaque RM
core::stringc CTKFixe::GetID() {
    return nodeDepart->getName();
}

//Methode a surcharger pour chaque RM
void CTKFixe::SetID(core::stringc name) {
    nodeDepart->setName(name);
}

void CTKFixe::DrawRemonteeDepart() {

    nodeDepart=smgr->addMeshSceneNode( meshDepart );
    nodeDepart->setID(CMountain::TKF_NODE_ID);
    nodeDepart->setScale(core::vector3df(1.8,1.8,1.8));
    nodeDepart->getMaterial(0) = materialDepart;
    nodeDepart->setMaterialFlag(video::EMF_LIGHTING, false);
    nodeDepart->setPosition(positionDepart);
    nodeDepart->updateAbsolutePosition();

    scene::ITriangleSelector* selector = 0;
    selector =smgr->createTriangleSelector(meshDepart,nodeDepart);
    nodeDepart->setTriangleSelector(selector);
    selector->drop(); // We're done with this selector, so drop it now.

    nodePoulieDepart=smgr->addMeshSceneNode( meshPoulieDepart,nodeDepart );
    nodePoulieDepart->getMaterial(0) = materialPoulieDepart;
    nodePoulieDepart->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieDepart->setScale(core::vector3df(0.8,0.8,0.8));
    nodePoulieDepart->setRotation(core::vector3df(180,0,-10));
    nodePoulieDepart->setPosition(core::vector3df(-1.2,5.2,0));
    nodePoulieDepart->updateAbsolutePosition();

    posMonteePoulieDepart=smgr->addEmptySceneNode(nodeDepart);
    posMonteePoulieDepart->setPosition(core::vector3df(-1.2,5.2,-0.8));
    //posMonteePoulieDepart->setPosition(core::vector3df(0,0,-1));
    posMonteePoulieDepart->updateAbsolutePosition();

    posDescentePoulieDepart=smgr->addEmptySceneNode(nodeDepart);
    //posDescentePoulieDepart->setPosition(core::vector3df(0,0,1));
    posDescentePoulieDepart->setPosition(core::vector3df(-1.2,5.2,0.8));
    posDescentePoulieDepart->updateAbsolutePosition();
}

void CTKFixe::DrawRemonteeArrivee()
{
    nodeArrivee=smgr->addMeshSceneNode(meshArrivee);
    nodeArrivee->setScale(core::vector3df(1.5,1.5,1.5));
    nodeArrivee->getMaterial(0) = materialArrivee;
    nodeArrivee->setMaterialFlag(video::EMF_LIGHTING, false);
    nodeArrivee->setPosition(positionArrivee);
    nodeArrivee->updateAbsolutePosition();

    nodePoulieArrivee=smgr->addMeshSceneNode( meshPoulieArrivee,nodeArrivee );
    nodePoulieArrivee->setParent(nodeArrivee);
    nodePoulieArrivee->getMaterial(0) = materialPoulieArrivee;
    nodePoulieArrivee->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieArrivee->setScale(core::vector3df(1.5,1.5,1.5));
    nodePoulieArrivee->setRotation(core::vector3df(193,0,0));
    nodePoulieArrivee->setPosition(core::vector3df(-14.4,14.5,0));
    nodePoulieArrivee->updateAbsolutePosition();

    posMonteePoulieArrivee=smgr->addEmptySceneNode(nodeArrivee);
    posMonteePoulieArrivee->setPosition(core::vector3df(-14.4,14.2,1.4));
    posMonteePoulieArrivee->updateAbsolutePosition();

    posDescentePoulieArrivee=smgr->addEmptySceneNode(nodeArrivee);
    posDescentePoulieArrivee->setPosition(core::vector3df(-14.4,14.8,-1.4));
    posDescentePoulieArrivee->updateAbsolutePosition();


    //Oriente la rotation du départ et arrivée
    core::vector3df result=positionArrivee-positionDepart;
    result=result.getHorizontalAngle();
    nodeDepart->setRotation(core::vector3df(0,result.Y+90,0));
    nodeArrivee->setRotation(core::vector3df(0,result.Y-90,0));
    //Dessine le cable initial
    //RedrawCable();
}

//Redessine le cable, après chaque modification du TK, en fonction du nombre et des coordonnées des pylones
void CTKFixe::RedrawCable()
{
    core::vector3df deltaPoulieM;
    core::vector3df deltaPoulieD;
    core::vector3df deltaPoulieM2;
    core::vector3df deltaPoulieD2;

    if(nodePoulieDepart && nodePoulieArrivee) {
        driver->setTransform(video::ETS_WORLD,core::IdentityMatrix);
        //driver->draw3DLine(nodePoulieDepart->getAbsolutePosition() , nodePoulieArrivee->getAbsolutePosition(),  video::SColor(255, 0, 0, 0));

        if(arrPylones.size()==0)
        {
            driver->draw3DLine(posMonteePoulieDepart->getAbsolutePosition() , posMonteePoulieArrivee->getAbsolutePosition(),  video::SColor(255, 0, 0, 0));
            driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition() , posDescentePoulieArrivee->getAbsolutePosition(),  video::SColor(255, 0, 0, 0));
        } else {
            //Pour la montée
            for(unsigned int i=0;i<arrPylones.size();i++)
            {

                if(i==0)
                {
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/
                    driver->draw3DLine(posMonteePoulieDepart->getAbsolutePosition() , arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM,  video::SColor(255, 0, 0, 0));
                    //driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition(),arrPylones[i]->posDescentePylone->getAbsolutePosition()+deltaPoulieD,  video::SColor(255, 0, 0, 0));

                } else {
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/

                    if(arrPylones[i-1]->monteeSupportCompression==1)
                    {
                        deltaPoulieM2=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM2=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i-1]->descenteSupportCompression==1)
                    {
                        deltaPoulieD2=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD2=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/

                    driver->draw3DLine(arrPylones[i-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM2, arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM, video::SColor(255, 0, 0, 0));
                   // driver->draw3DLine(arrPylones[i-1]->posDescentePylone->getAbsolutePosition()+deltaPoulieD2, arrPylones[i]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, video::SColor(255, 0, 0, 0));
                }

                if(i==arrPylones.size()-1)
                {
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/

                    driver->draw3DLine(arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM, posMonteePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                   // driver->draw3DLine(arrPylones[i]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                }
            }


            //Pour la descente, c'est particulier, on peut avoir des pylones sans brin descente
            int cptDes=arrPylones.size()-1;

            //cas particulier
            if(arrPylones.size()==0) {
                 driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition(), posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
            }

            while(cptDes>=0) {
                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKF_MONTEE_DESCENTE) {
                    u32 idprevious=findPreviousPyloneWithDescente(cptDes);
                    if(idprevious==-1) {
                        if(arrPylones[cptDes]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        driver->draw3DLine(arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                    } else {
                        if(arrPylones[cptDes]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        if(arrPylones[idprevious]->descenteSupportCompression==1)
                        {
                            deltaPoulieD2=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD2=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        driver->draw3DLine(arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD2, video::SColor(255, 0, 0, 0));
                    }
                    cptDes--;
                    //Juste pour le dernier troncon
                    if(cptDes==-1) {
                         driver->draw3DLine(arrPylones[0]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieDepart->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                    }
                } else {
                    u32 idnext=findNextPyloneWithDescente(cptDes);
                    u32 idprevious=findPreviousPyloneWithDescente(cptDes);

                    if(idprevious==-1) {
                        if(idnext==-1) {
                            driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition(), posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                            cptDes=-1;
                        } else {
                            if(arrPylones[idnext]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            driver->draw3DLine(arrPylones[idnext]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                            cptDes=idnext;
                        }
                    } else {
                        if(idnext==-1) {
                            if(arrPylones[idprevious]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            driver->draw3DLine(arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieDepart->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                            cptDes=-1;
                        } else {
                            if(arrPylones[idprevious]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            if(arrPylones[idnext]->descenteSupportCompression==1)
                            {
                                deltaPoulieD2=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD2=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            driver->draw3DLine(arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, arrPylones[idnext]->posDescentePylone->getAbsolutePosition()+deltaPoulieD2, video::SColor(255, 0, 0, 0));
                            cptDes=idnext;
                        }
                    }
                }
            }

        }

    //printf("RedrawCable entre %f %f %f  et %f %f %f \n",nodePoulieDepart->getAbsolutePosition().X,nodePoulieDepart->getAbsolutePosition().Y,nodePoulieDepart->getAbsolutePosition().Z,nodePoulieArrivee->getAbsolutePosition().X,nodePoulieArrivee->getAbsolutePosition().Y,nodePoulieArrivee->getAbsolutePosition().Z);
    }
}

u32 CTKFixe::findNextPyloneWithDescente(u32 startindex) {
    u32 idnext=startindex-1;
    if(idnext!=-1) {
        for(unsigned int i=idnext;i>=0;i--)
        {
            if(arrPylones[i]->GetType()==CPyloneRM::PYLONE_TKF_MONTEE_DESCENTE) {
                return i;
            }
        }
    }

     return -1;
}

u32 CTKFixe::findPreviousPyloneWithDescente(u32 startindex) {
    u32 idprevious=startindex+1;

    if(idprevious<arrPylones.size()) {
        for(unsigned int i=idprevious;i<arrPylones.size();i++)
        {
            if(arrPylones[i]->GetType()==CPyloneRM::PYLONE_TKF_MONTEE_DESCENTE) {
                return i;
            }
        }
    }

    return -1;
}

core::vector3df CTKFixe::GetRotationRM()
{
    return nodeDepart->getRotation();
}


//Anime le tk
void CTKFixe::AnimateRm(core::vector3df posCamera,f32 deltaTime)
{
    if(nodePoulieDepart==0 || nodePoulieArrivee==0) {
        return;
    }

    if(enMarche) {
        core::vector3df rotation=nodePoulieDepart->getRotation();
        //rotation.X=0;
        rotation.Y+=vitesse*deltaTime*36.0f;
        //rotation.Z=0;
        if(rotation.Y>360.0f) {
            rotation.Y=rotation.Y-360.0f;
        }
        nodePoulieDepart->setRotation(rotation);

        rotation=nodePoulieArrivee->getRotation();
        core::matrix4 m;
        core::matrix4 mp;
        m.setRotationDegrees(rotation);
        mp.setRotationDegrees(core::vector3df(0.0f,-vitesse*deltaTime*36.0f,0.0f));

        m*=mp;
        rotation=m.getRotationDegrees();
        nodePoulieArrivee->setRotation(rotation);

        //Gère le son du moteur selon distance caméra
        core::vector3df diffcamera=posCamera-nodeDepart->getPosition();
        f32 distanceCamera=abs(diffcamera.getLength());

        if(soundMoteur) {
            if(distanceCamera>100.0f)
            {
                if(soundMoteur->isPlaying()) {
                    soundMoteur->stop();
                }
            } else {
                soundMoteur->setVolume(1.0f-(distanceCamera/100.0f)); // 50% volume
                if(!soundMoteur->isPlaying())
                {
                     soundMoteur->play();
                }

            }
        }


        //Anime les pylones
        for(unsigned int i=0;i<arrPylones.size();i++)
        {
            arrPylones[i]->AnimatePylone(vitesse*deltaTime*50.0f);
        }

        //Anime les perches
        for(unsigned int i=0;i<arrVehicules.size();i++)
        {
            //trouve le tronçon de la perche
            CTronconRM* t= GetTronconByNumero(arrVehicules[i]->numeroTroncon);
            if(t)
            {
                //Mouvement rectiligne pour un troncon de ligne normale
                if(t->GetType()==CTronconRM::TRONCON_LIGNE)
                {
                    core::vector3df lastPosition=arrVehicules[i]->getPosition();
                    //printf("lastPosition = %f ; %f ; %f\n",lastPosition.X,lastPosition.Y,lastPosition.Z);
                    //printf("positionFin = %f ; %f ; %f\n",t->positionFin.X,t->positionFin.Y,t->positionFin.Z);
                    core::vector3df difference=t->positionFin-t->positionDebut;
                    f32 longueur=difference.getLength();

                    arrVehicules[i]->pas+=deltaTime*vitesse/longueur;
                    /*if(i==0) {
                        printf("perche %i  troncon=%i pas=%f  sur longueur=%f  deltatime=%f\n",i,t->numero,arrVehicules[i]->pas,longueur,deltaTime);
                    }*/
                    core::vector3df vecPosPerche;

                    vecPosPerche.X=t->positionDebut.X+difference.X*arrVehicules[i]->pas;
                    vecPosPerche.Y=t->positionDebut.Y+difference.Y*arrVehicules[i]->pas;
                    vecPosPerche.Z=t->positionDebut.Z+difference.Z*arrVehicules[i]->pas;

                    arrVehicules[i]->setPosition(vecPosPerche);

                    core::vector3df newPosition=arrVehicules[i]->getPosition();
                    //printf("2-perche %i => %f ; %f ; %f\n",i,newPosition.X,newPosition.Y,newPosition.Z);
                    //Verifie si on a atteint la fin du troncon. On se laisse une marge pour eviter d'avoir un mouvement qui rate la fin
                    if(newPosition.X>=t->positionFin.X-0.1f && newPosition.X<=t->positionFin.X+0.1f && newPosition.Y>=t->positionFin.Y-0.1f && newPosition.Y<=t->positionFin.Y+0.1f && newPosition.Z>=t->positionFin.Z-0.1f && newPosition.Z<=t->positionFin.Z+0.1f)
                    {
                        arrVehicules[i]->setPosition(t->positionFin);
                        arrVehicules[i]->pas=0;
                        //printf("Changement troncon perche %i\n",i);
                        //On passe au troncon suivant, sauf si c est le dernier
                        if(t->numero<arrTroncons.size()-1) {
                            arrVehicules[i]->numeroTroncon++;
                        } else {
                            arrVehicules[i]->numeroTroncon=0;
                        }
                    }
                }

                //Demi cercle autour de la poulie d'arrivée
                if(t->GetType()==CTronconRM::TRONCON_POULIEARRIVEE)
                {
                    core::vector3df lastPosition=arrVehicules[i]->getPosition();
                    core::vector3df lastRotation=arrVehicules[i]->getRotation();

                    //printf("getRotation-perche %i => %f ; %f ; %f\n",i,lastRotation.X,lastRotation.Y,lastRotation.Z);

                    core::vector3df vecteurRayon=posDescentePoulieArrivee->getPosition()-posMonteePoulieArrivee->getPosition();
                    f32 rayon=(vecteurRayon.getLength()/2)+0.5f;
                    //core::vector3df centreCercle=posMonteePoulieArrivee->getPosition()+(posDescentePoulieArrivee->getPosition()-posMonteePoulieArrivee-> getPosition())/2.0f;

                    //arrVehicules[i]->pas+=0.005f;
                    arrVehicules[i]->pas+=(vitesse*deltaTime*5.0f)/(core::PI*rayon);
                    f32 angle=core::PI-arrVehicules[i]->pas;
                    //f32 angle=arrVehicules[i]->pas-core::PI;

                    //Rotation de la perche
                    core::quaternion rot(lastRotation * core::DEGTORAD);
                    core::quaternion toAdd;
                    toAdd.fromAngleAxis((vitesse*deltaTime*5.0f)/(core::PI*rayon), core::vector3df(0.0f,1.0f,0.0f));
                    rot *= toAdd;

                    core::vector3df r;
                    rot.toEuler(r);
                    arrVehicules[i]->setRotation(r * core::RADTODEG);


                    core::vector3df vecPosPyl;
                    f32 diffY=posDescentePoulieArrivee->getAbsolutePosition().Y-posMonteePoulieArrivee->getAbsolutePosition().Y;
                    f32 deltaY=diffY*arrVehicules[i]->pas/core::PI;
                    vecPosPyl.X=t->centreRotation.X+rayon*cos(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)+core::PI/2.0f);
                    vecPosPyl.Y=t->centreRotation.Y+deltaY-(diffY/2.0f);
                    vecPosPyl.Z=t->centreRotation.Z+rayon*sin(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)+core::PI/2.0f);
                    //printf("Rotation-perche %i angle=%f => %f ; %f ; %f\n",i,angle,vecPosPyl.X,vecPosPyl.Y,vecPosPyl.Z);
                    arrVehicules[i]->setPosition(vecPosPyl);



                    //Verifie si on a atteint la fin du troncon. On se laisse une marge pour eviter d'avoir un mouvement qui rate la fin
                    //if(angle>=0)
                    if(angle<=0)
                    {
                        arrVehicules[i]->setPosition(t->positionFin);
                        arrVehicules[i]->pas=0;
                        arrVehicules[i]->setRotation(core::vector3df(0.0f,nodeDepart->getRotation().Y+180.0f,0.0f));
                        //printf("Changement troncon poulie arrivee perche %i\n",i);
                        //On passe au troncon suivant, sauf si c est le dernier
                        if(t->numero<arrTroncons.size()-1) {
                            arrVehicules[i]->numeroTroncon++;
                        } else {
                            arrVehicules[i]->numeroTroncon=0;
                        }
                    }
                }

                //Demi cercle autour de la poulie de départ
                if(t->GetType()==CTronconRM::TRONCON_POULIEDEPART)
                {
                    core::vector3df lastPosition=arrVehicules[i]->getPosition();
                    core::vector3df lastRotation=arrVehicules[i]->getRotation();

                    //printf("getRotation-perche %i => %f ; %f ; %f\n",i,lastRotation.X,lastRotation.Y,lastRotation.Z);

                    core::vector3df vecteurRayon=posDescentePoulieDepart->getPosition()-posMonteePoulieDepart->getPosition();
                    f32 rayon=(vecteurRayon.getLength()/2.0f)+0.3f;

                    //arrVehicules[i]->pas+=0.005f;
                    arrVehicules[i]->pas+=(vitesse*deltaTime*4.0f)/(core::PI*rayon);

                    core::vector3df vecPosPyl;
                    f32 angle=core::PI-arrVehicules[i]->pas;
                    vecPosPyl.X=t->centreRotation.X+rayon*cos(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)-core::PI/2.0f);
                    vecPosPyl.Y=t->centreRotation.Y+(sin (-angle))/10.0f;
                    vecPosPyl.Z=t->centreRotation.Z+rayon*sin(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)-core::PI/2.0f);
                    //printf("Rotation-perche %i angle=%f => %f ; %f ; %f\n",i,angle,vecPosPyl.X,vecPosPyl.Y,vecPosPyl.Z);
                    arrVehicules[i]->setPosition(vecPosPyl);

                    //Rotation de la perche
                    core::quaternion rot(lastRotation * core::DEGTORAD);
                    core::quaternion toAdd;
                    toAdd.fromAngleAxis((vitesse*deltaTime*4.0f)/(core::PI*rayon), core::vector3df(0.0f,1.0f,0.0f));
                    rot *= toAdd;

                    core::vector3df r;
                    rot.toEuler(r);
                    arrVehicules[i]->setRotation(r * core::RADTODEG);

                     //Verifie si on a atteint la fin du troncon. On se laisse une marge pour eviter d'avoir un mouvement qui rate la fin
                    if(angle<=0)
                    {
                        arrVehicules[i]->setPosition(t->positionFin);
                        arrVehicules[i]->pas=0;
                        arrVehicules[i]->setRotation(core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
                        //printf("Changement troncon poulie arrivee perche %i\n",i);
                        //On passe au troncon suivant, sauf si c est le dernier
                        if(t->numero<arrTroncons.size()-1) {
                            arrVehicules[i]->numeroTroncon++;
                        } else {
                            arrVehicules[i]->numeroTroncon=0;
                        }
                    }
                }

            } else {
                printf("ERREUR GetTronconByNumero for CTKFixe t->0\n");
            }
        }
    } else {
        if(constructionFinie && soundMoteur->isPlaying()) {
            soundMoteur->stop();
        }
    }
}

//Fonction qui met à jour chaque troncon du RM, c'est ce qui permet de connaitre
// pour chacun de ces tronons le point de départ et le point d'arrivée
void CTKFixe::UpdateTroncons()
{
    if(!nodePoulieDepart || !nodePoulieArrivee) {
        RedrawCable();
        return;
    }

    core::vector3df deltaPoulieM;
    core::vector3df deltaPoulieD;

    arrTroncons.clear();
    //Cas particulier si j'ai pas de pylone
    if(arrPylones.size()==0)
    {
        printf("------------------------\n");
        CTronconRM* tronconDep=new CTronconRM();
        tronconDep->numero=0;
        tronconDep->SetType(CTronconRM::TRONCON_POULIEDEPART);
        tronconDep->centreRotation=nodePoulieDepart->getAbsolutePosition();
        arrTroncons.push_back(tronconDep);

        CTronconRM* tronconLigneMontee=new CTronconRM();
        tronconLigneMontee->numero=1;
        tronconLigneMontee->SetType(CTronconRM::TRONCON_LIGNE);
        tronconLigneMontee->positionDebut=posMonteePoulieDepart->getAbsolutePosition();
        tronconLigneMontee->positionFin=posMonteePoulieArrivee->getAbsolutePosition();
        tronconLigneMontee->enMontee=true;
        arrTroncons.push_back(tronconLigneMontee);

        CTronconRM* tronconArr=new CTronconRM();
        tronconArr->numero=2;
        tronconArr->SetType(CTronconRM::TRONCON_POULIEARRIVEE);
        tronconArr->centreRotation=nodePoulieArrivee->getAbsolutePosition();
        arrTroncons.push_back(tronconArr);

        CTronconRM *tronconLigneDescente=new CTronconRM();
        tronconLigneDescente->numero=3;
        tronconLigneDescente->SetType(CTronconRM::TRONCON_LIGNE);
        tronconLigneDescente->positionDebut= posDescentePoulieArrivee->getAbsolutePosition();
        tronconLigneDescente->positionFin=posDescentePoulieDepart->getAbsolutePosition();
        tronconLigneDescente->enMontee=false;
        arrTroncons.push_back(tronconLigneDescente);

    } else { //Sinon c'est qu'il y a au moins 1 pylone
        CTronconRM* tronconDep=new CTronconRM();
        tronconDep->numero=0;
        tronconDep->SetType(CTronconRM::TRONCON_POULIEDEPART);
        tronconDep->centreRotation=nodePoulieDepart->getAbsolutePosition();
        arrTroncons.push_back(tronconDep);
        printf("------------------------\n");
        printf("troncon %i POULIE Depart\n",tronconDep->numero);

        //u32 nbTr=4+2*arrPylones.size();
        core::vector3df v2;
        core::vector3df v1;
        core::vector3df v2last;
        core::vector3df v1last;

        //Pour la montée
        for(unsigned int i=0;i<arrPylones.size();i++)
        {

            CTronconRM* tronconPM=new CTronconRM();
            tronconPM->numero=i+1;
            tronconPM->SetType(CTronconRM::TRONCON_LIGNE);

            if(i==0) {
                tronconPM->positionDebut=posMonteePoulieDepart->getAbsolutePosition();
                tronconPM->positionFin=arrPylones[0]->posMonteePylone->getAbsolutePosition();
                v1=nodePoulieDepart->getAbsolutePosition();
                if(arrPylones.size()>1)
                {
                    v2=arrPylones[1]->posMonteePylone->getAbsolutePosition();
                } else {
                    v2=nodePoulieArrivee->getAbsolutePosition();
                }
                arrPylones[0]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[0]->posMonteePylone->getAbsolutePosition(),v2);
                //Ajuste selon compression/support
                if(arrPylones[0]->monteeSupportCompression==1)
                {
                    deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    printf("P0 support\n");
                } else {
                    deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    printf("P0 compression\n");
                }
                tronconPM->positionFin=arrPylones[0]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;

            } else {
                if(i<arrPylones.size()-1) {

                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    if(arrPylones[i-1]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;

                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition();

                    if(i>=arrPylones.size()-1) {
                        //tronconPM->positionFin=posMonteePoulieArrivee->getAbsolutePosition();
                        v2=posMonteePoulieArrivee->getAbsolutePosition();
                    } else {
                        //tronconPM->positionFin=arrPylones[i+1]->posMonteePylone->getAbsolutePosition();
                        v2=arrPylones[i+1]->posMonteePylone->getAbsolutePosition();
                    }

                    v1=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
                    //printf("P%i angle montee compression=%i\n",i,arrPylones[i]->monteeSupportCompression);
                    //Ajuste selon compression/support
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                        printf("Pi support\n");
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                        printf("Pi compression\n");
                    }
                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
                } else {
                    //dernier troncon
                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    if(arrPylones[i-1]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition();
                    v2=nodePoulieArrivee->getAbsolutePosition();
                    if(arrPylones.size()<2)
                    {
                        v1=nodePoulieDepart->getAbsolutePosition();
                    } else {
                        v1=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    }
                    //arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
                    arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
                    //Ajuste le cable aux poulies
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                        printf("Pi-1 support\n");
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                        printf("Pi-1 compression\n");
                    }
                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
                }

            }
            printf("troncon %i LIGNE montee debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconPM->numero,tronconPM->positionDebut.X,tronconPM->positionDebut.Y,tronconPM->positionDebut.Z,tronconPM->positionFin.X,tronconPM->positionFin.Y,tronconPM->positionFin.Z);
            tronconPM->enMontee=true;
            arrTroncons.push_back(tronconPM);

        }

        //dernier troncon
        //if(i==arrPylones.size()-1) {
        CTronconRM* tronconPM2=new CTronconRM();
        //tronconPM2->numero=i+2;
        tronconPM2->numero=arrPylones.size()+1;
        tronconPM2->SetType(CTronconRM::TRONCON_LIGNE);
        //tronconPM2->positionDebut=arrPylones[i]->posMonteePylone->getAbsolutePosition();
        tronconPM2->positionDebut=arrPylones[arrPylones.size()-1]->posMonteePylone->getAbsolutePosition();
        tronconPM2->positionFin=posMonteePoulieArrivee->getAbsolutePosition();
        v2=nodePoulieArrivee->getAbsolutePosition();
        if(arrPylones.size()<2)
        {
            v1=nodePoulieDepart->getAbsolutePosition();
        } else {
            v1=arrPylones[arrPylones.size()-2]->posMonteePylone->getAbsolutePosition();
        }
        //arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
        arrPylones[arrPylones.size()-1]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[arrPylones.size()-1]->posMonteePylone->getAbsolutePosition(),v2);
        //Ajuste selon compression/support
        //if(arrPylones[i]->monteeSupportCompression==1)
        if(arrPylones[arrPylones.size()-1]->monteeSupportCompression==1)
        {
            deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
            printf("P last support\n");
        } else {
            deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
            printf("P last compression\n");
        }
        //tronconPM2->positionDebut=arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
        tronconPM2->positionDebut=arrPylones[arrPylones.size()-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
        tronconPM2->enMontee=true;
        printf("troncon %i LIGNE montee debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconPM2->numero,tronconPM2->positionDebut.X,tronconPM2->positionDebut.Y,tronconPM2->positionDebut.Z,tronconPM2->positionFin.X,tronconPM2->positionFin.Y,tronconPM2->positionFin.Z);
        arrTroncons.push_back(tronconPM2);
        //}

        //Pour la poulie retour
        CTronconRM* tronconArr=new CTronconRM();
        tronconArr->numero=arrPylones.size()+2;
        tronconArr->SetType(CTronconRM::TRONCON_POULIEARRIVEE);
        tronconArr->centreRotation=nodePoulieArrivee->getAbsolutePosition();
        arrTroncons.push_back(tronconArr);

        printf("troncon %i POULIE Arrivee\n",tronconArr->numero);

        //Pour la descente
        int cptDes=arrPylones.size()-1;
        u32 currentNumero=arrPylones.size()+2;

        while(cptDes>=0) {
            u32 idnext=findNextPyloneWithDescente(cptDes);
            u32 idprevious=findPreviousPyloneWithDescente(cptDes);

            CTronconRM* tronconD=new CTronconRM();
            tronconD->numero=currentNumero+1;
            tronconD->SetType(CTronconRM::TRONCON_LIGNE);
            //si je traite le 1er pylone
            if(cptDes==arrPylones.size()-1) {
                tronconD->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                v1=nodePoulieArrivee->getAbsolutePosition();
                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKF_MONTEE_DESCENTE) {
                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition();
                    u32 idnext2=findNextPyloneWithDescente(cptDes);
                    if(idnext2==-1) {
                        v2=nodePoulieDepart->getAbsolutePosition();
                    } else {
                        v2=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition();
                    }
                    arrPylones[cptDes]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[cptDes]->posDescentePylone->getAbsolutePosition(),v2);
                    //Ajuste selon compression/support
                    if(arrPylones[cptDes]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                } else {
                    u32 idnext2=findNextPyloneWithDescente(cptDes);
                    if(idnext2==-1) {
                        tronconD->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                        cptDes=-1;
                    } else {
                        tronconD->positionFin=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition();
                        u32 idnext3=findNextPyloneWithDescente(idnext2);
                        if(idnext3==-1) {
                            v2=nodePoulieDepart->getAbsolutePosition();
                        } else {
                            v2=arrPylones[idnext3]->posDescentePylone->getAbsolutePosition();
                        }
                        arrPylones[idnext2]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[idnext2]->posMonteePylone->getAbsolutePosition(),v2);
                        //Ajuste selon compression/support
                        if(arrPylones[idnext2]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD->positionFin=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        cptDes=idnext2;
                        continue;
                    }

                }


            } else {
                //sinon pylone normal
                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKF_MONTEE_DESCENTE) {
                    if(idprevious==-1) {
                        tronconD->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                        v1=posDescentePoulieArrivee->getAbsolutePosition();
                    } else {
                        tronconD->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                        if(arrPylones[idprevious]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        v1=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                    }

                    if(idnext==-1) {
                         v2=posDescentePoulieDepart->getAbsolutePosition();
                    } else {
                         v2=arrPylones[idnext]->posDescentePylone->getAbsolutePosition();
                    }

                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition();

                    arrPylones[cptDes]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[cptDes]->posDescentePylone->getAbsolutePosition(),v2);
                    //Ajuste selon compression/support
                    if(arrPylones[cptDes]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                } else {
                    if(idprevious==-1) {
                        tronconD->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                        v1=nodePoulieArrivee->getAbsolutePosition();
                    } else {
                        tronconD->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();

                        u32 idprevious2=findPreviousPyloneWithDescente(idprevious);
                        if(idprevious2==-1) {
                            v1=nodePoulieArrivee->getAbsolutePosition();
                        } else {
                            v1=arrPylones[idprevious2]->posDescentePylone->getAbsolutePosition();
                            if(arrPylones[idprevious2]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            tronconD->positionDebut=arrPylones[idprevious2]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        }

                    }

                    if(idnext==-1) {
                        tronconD->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                        v2=nodePoulieDepart->getAbsolutePosition();
                        cptDes=-1;
                        continue;
                    } else {
                        tronconD->positionFin=arrPylones[idnext]->posDescentePylone->getAbsolutePosition();
                        u32 idnext2=findNextPyloneWithDescente(idnext);

                        if(idnext2==-1) {
                           v2=nodePoulieDepart->getAbsolutePosition();
                        } else {
                           v2=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition();
                        }

                        arrPylones[idnext]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[idnext]->posDescentePylone->getAbsolutePosition(),v2);
                        //Ajuste selon compression/support
                        if(arrPylones[idnext]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD->positionFin=arrPylones[idnext]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        cptDes=idnext;
                        continue;

                    }

                }
            }

            if(cptDes==0) { //Sinon si je traite le dernier pylone
                CTronconRM* tronconD2=new CTronconRM();
                tronconD2->numero=currentNumero+2;
                tronconD2->SetType(CTronconRM::TRONCON_LIGNE);

                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKF_MONTEE_DESCENTE) {
                    tronconD2->positionDebut=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition();
                    tronconD2->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                    v2=nodePoulieDepart->getAbsolutePosition();
                    u32 idprevious2=findPreviousPyloneWithDescente(cptDes);
                    if(idprevious2==-1) {
                        v1=nodePoulieArrivee->getAbsolutePosition();
                    } else {
                        v1=arrPylones[idprevious2]->posDescentePylone->getAbsolutePosition();
                    }
                     arrPylones[cptDes]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[cptDes]->posDescentePylone->getAbsolutePosition(),v2);
                    //Ajuste selon compression/support
                    if(arrPylones[cptDes]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconD2->positionDebut=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                } else {
                    if(idprevious==-1) {
                        tronconD2->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                        tronconD2->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                        cptDes=-1;
                    } else {
                        tronconD2->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                        v2=nodePoulieDepart->getAbsolutePosition();
                        v1=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                        arrPylones[idprevious]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[idprevious]->posDescentePylone->getAbsolutePosition(),v2);
                        //Ajuste selon compression/support
                        if(arrPylones[idprevious]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD2->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        tronconD2->positionFin=posDescentePoulieDepart->getAbsolutePosition();

                        idprevious--;
                    }

                }
                printf("troncon %i LIGNE descente debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconD2->numero,tronconD2->positionDebut.X,tronconD2->positionDebut.Y,tronconD2->positionDebut.Z,tronconD2->positionFin.X,tronconD2->positionFin.Y,tronconD2->positionFin.Z);
                tronconD2->enMontee=false;
                arrTroncons.push_back(tronconD2);

            }

            printf("troncon %i LIGNE descente debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconD->numero,tronconD->positionDebut.X,tronconD->positionDebut.Y,tronconD->positionDebut.Z,tronconD->positionFin.X,tronconD->positionFin.Y,tronconD->positionFin.Z);
            tronconD->enMontee=false;
            arrTroncons.push_back(tronconD);

            currentNumero++;
            cptDes--;
        }



        printf("------------------------\n");
        RedrawCable();
    }
}

//Permet de placer les perches sur le TK
void CTKFixe::PlaceVehicules()
{
    //La première perche commence toujours au troncon 1 (le 0 etant la poulie de départ)
    /*CVehicule *perche=new CVehicule(smgr,driver);
    perche->SetType(CVehicule::VEHICULE_TKF);
    perche->numeroTroncon=1;
    perche->pas=0;
    perche->DrawVehicule(posMonteePoulieDepart->getAbsolutePosition(),core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
    arrVehicules.push_back(perche);

    CTronconRM* t=GetTronconByNumero(1);
    core::vector3df difference=t->positionFin-t->positionDebut;
    f32 longueur=difference.getLength();

    CVehicule *perche2=new CVehicule(smgr,driver);
    perche2->SetType(CVehicule::VEHICULE_TKF);
    perche2->numeroTroncon=1;

    //Trouver pas E [tq
    perche2->pas=espacementVehicules/longueur;
    core::vector3df vecPosPerche;
    vecPosPerche.X=(t->positionDebut.X+difference.X*perche2->pas);
    vecPosPerche.Y=(t->positionDebut.Y+difference.Y*perche2->pas);
    vecPosPerche.Z=(t->positionDebut.Z+difference.Z*perche2->pas);
    perche2->DrawVehicule(vecPosPerche,core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
    arrVehicules.push_back(perche2);*/

    if(arrTroncons.size()>0) {

        f32 deltatroncon=0;
        for(unsigned int i=0;i<arrTroncons.size();i++){
            CTronconRM* tr=GetTronconByNumero(i);

            //On prend en compte que les troncons de type ligne
            if(tr->GetType()==CTronconRM::TRONCON_LIGNE) {
                core::vector3df difference=tr->positionFin-tr->positionDebut;
                f32 longueur=difference.getLength()-deltatroncon;
                u32 nbPerchesTr=(u32)(longueur/espacementVehicules);
                deltatroncon=longueur-nbPerchesTr*espacementVehicules;
                difference=difference.normalize();
                printf("troncon->%i nbperches=%i difference=%f ; %f ; %f\n",i,nbPerchesTr,difference.X,difference.Y,difference.Z);

                for(int j=1 ;j<=nbPerchesTr;j++)
                {
                    core::vector3df vecPosPerche;
                    //vecPosPerche.X=(tr->positionDebut.X+difference.X*j*20)-(difference.X*deltatroncon);
                    //vecPosPerche.Y=(tr->positionDebut.Y+difference.Y*j*20)-(difference.Y*deltatroncon);
                    //vecPosPerche.Z=(tr->positionDebut.Z+difference.Z*j*20)-(difference.Z*deltatroncon);

                    vecPosPerche.X=(tr->positionDebut.X+difference.X*j*espacementVehicules)-(difference.X*deltatroncon);
                    vecPosPerche.Y=(tr->positionDebut.Y+difference.Y*j*espacementVehicules)-(difference.Y*deltatroncon);
                    vecPosPerche.Z=(tr->positionDebut.Z+difference.Z*j*espacementVehicules)-(difference.Z*deltatroncon);

                    CVehicule *perche=new CVehicule(smgr,driver);
                    perche->SetType(CVehicule::VEHICULE_TKF);
                    //printf("perche %i ->setPosition %f,%f,%f troncon:%i\n",j,vecPosPerche.X,vecPosPerche.Y,vecPosPerche.Z,tr->numero);
                    perche->numeroTroncon=tr->numero;
                    //perche->pas=j*40/nbPerchesTr;
                    perche->pas=((j*espacementVehicules)-deltatroncon)/longueur;
                    if(tr->enMontee)
                    {
                        perche->DrawVehicule(vecPosPerche,core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
                    } else {
                        perche->DrawVehicule(vecPosPerche,core::vector3df(0.0f,nodeDepart->getRotation().Y+180.0f,0.0f));
                    }

                    //perche->setPosition(posMonteePoulieDepart->getAbsolutePosition());
                    arrVehicules.push_back(perche);
                }

            }
        }
    }

}

void CTKFixe::PlaySoundMoteur(bool play)
{
    if(play){
        soundMoteur = OpenSound(deviceA.get(), "media/tkf-sound.mp3", true);
        if (!soundMoteur)
        {
            printf("Erreur chargement son moteur TKF\n");
            return;
        } else {
            soundMoteur->setRepeat(true);
            soundMoteur->setVolume(1.0f); // 50% volume
            soundMoteur->play();
        }
    } else {
        if (soundMoteur) {
            soundMoteur->stop();
        }
    }



}

CTKFixe::~CTKFixe()
{
    if(meshDepart!=0) {
        delete meshDepart;
        meshDepart=0;
    }

    if(nodeDepart!=0) {
        delete nodeDepart;
        nodeDepart=0;
    }

    if(meshArrivee!=0) {
        delete meshArrivee;
        meshArrivee=0;
    }

    if(nodeArrivee!=0) {
        delete nodeArrivee;
        nodeArrivee=0;
    }

    if(meshPoulieDepart!=0) {
        delete meshPoulieDepart;
        meshPoulieDepart=0;
    }

    if(nodePoulieDepart!=0) {
        delete nodePoulieDepart;
        nodePoulieDepart=0;
    }

    if(posMonteePoulieDepart!=0) {
        delete posMonteePoulieDepart;
        posMonteePoulieDepart=0;
    }

    if(posDescentePoulieDepart!=0) {
        delete posDescentePoulieDepart;
        posDescentePoulieDepart=0;
    }



}
