// Geometric Tools, Inc.
// http://www.geometrictools.com
// Copyright (c) 1998-2005.  All Rights Reserved
//
// The Wild Magic Library (WM3) source code is supplied under the terms of
// the license agreement
//     http://www.geometrictools.com/License/WildMagic3License.pdf
// and may not be copied or disclosed except in accordance with the terms
// of that agreement.

#ifndef MASSPULLEYSPRINGSYSTEM_H
#define MASSPULLEYSPRINGSYSTEM_H

#include "Wm3WindowApplication3.h"
#include "PhysicsModule.h"
using namespace Wm3;

class MassPulleySpringSystem : public WindowApplication3
{
    WM3_DECLARE_INITIALIZE;

public:
    MassPulleySpringSystem ();

    virtual bool OnInitialize ();
    virtual void OnTerminate ();
    virtual void OnIdle ();
    virtual bool OnKeyDown (unsigned char ucKey, int iX, int iY);

protected:
    void CreateScene ();
    TriMesh* CreateFloor ();
    TubeSurface* CreateCable ();
    TriMesh* CreateMass (float fRadius);
    void CreatePulley ();
    void CreateSpring ();
    TubeSurface* CreateHelix ();

    void DoPhysical ();
    void UpdatePulley ();
    void UpdateCable ();
    void UpdateHelix ();

    // root of scene and floor mesh
    NodePtr m_spkScene;
    TriMeshPtr m_spkFloor;
    WireframeStatePtr m_spkWireframe;

    // assembly to parent the cable root and pulley root
    NodePtr m_spkAssembly;

    // cable modeled as a tube surface, masses attached to ends
    NodePtr m_spkCableRoot;
    BSplineCurve3f* m_pkCableSpline;
    TubeSurfacePtr m_spkCable;
    static float CableRadial (float);
    TriMeshPtr m_spkMass1, m_spkMass2;

    // node to parent the pulley and spring
    NodePtr m_spkPulleyRoot;

    // pulley modeled as a disk with thickness
    NodePtr m_spkPulley;
    TriMeshPtr m_spkPlate0, m_spkPlate1, m_spkCylinder;
    TexturePtr m_spkMetal;

    // Spring modeled as a tube surface in the shape of a helix, then attached
    // to a U-bracket to hold the pulley disk.
    NodePtr m_spkSpring;
    TriMeshPtr m_spkSide0, m_spkSide1, m_spkTop;
    BSplineCurve3f* m_pkHelixSpline;
    TubeSurfacePtr m_spkHelix;
    static float HelixRadial (float);

    PhysicsModule m_kModule;

    // controlled frame rate
    float m_fLastIdle;
};

WM3_REGISTER_INITIALIZE(MassPulleySpringSystem);

#endif



