XNA Creators Club Online
Page 1 of 1 (1 items)
Sort Posts: Previous Next

Need help with Force Feedback to Joystick

Last post 6/22/2007 1:34 PM by Sumit. 0 replies.
  • 6/22/2007 1:34 PM

    Need help with Force Feedback to Joystick

    Hi,

    I have written a windows forms application to capture the position of joystick and give force feedback to it.

    I can read the joystick state information but I am not able to give force feedback to it. I have included the code for the form below. Can anyone please point out the mistake / problem.

    Thanks in advance

    Sumit

    #pragma once

    #using <mscorlib.dll>

    #using <system.dll>

    #using <system.drawing.dll>

    namespace ManagedJoystick {

    using namespace System;

    using namespace System::ComponentModel;

    using namespace System::Collections;

    using namespace System::Windows::Forms;

    using namespace System::Data;

    using namespace System::Drawing;

    using namespace System::Timers;

    using namespace Microsoft::DirectX;

    using namespace Microsoft::DirectX::DirectInput;

     

     

    /// <summary>

    /// Summary for Form1

    ///

    /// WARNING: If you change the name of this class, you will need to change the

    /// 'Resource File Name' property for the managed resource compiler tool

    /// associated with all .resx files this class depends on. Otherwise,

    /// the designers will not be able to interact properly with localized

    /// resources associated with this form.

    /// </summary>

    public ref class Form1 : public System::Windows::Forms::Form

    {

    public:

    Form1(void)

    {

    InitializeComponent();

    //

    //TODO: Add the constructor code here

    //

    // Create force feedback capable joystick device

    for each(DeviceInstance di in Manager::GetDevices(DeviceClass::GameControl, EnumDevicesFlags::ForceFeeback))

    {

    device = gcnew Device(di.InstanceGuid);

    //MessageBox::Show("Yahoo!!!!! Joystick found");

    break;

    }

    if(device == nullptr)

    {

    // Error notification if joystick is not found

    MessageBox::Show("No joystick found");

    }

    // Set data format

    device->SetDataFormat(DeviceDataFormat::Joystick);

    // Set Cooperative level

    device->SetCooperativeLevel(this, CooperativeLevelFlags::Exclusive | CooperativeLevelFlags::Background);

    // Set axis mode absolute

    device->Properties->AxisModeAbsolute = true;

    //Acquire joystick for capturing

    device->Acquire();

    // Configure axes

    for each (DeviceObjectInstance ^doi in device->Objects)

    {

    // Set axes range

    if((doi->ObjectId) && (int)DeviceObjectTypeFlags::Axis != 0)

    {

    device->Properties->SetRange(ParameterHow::ById, doi->ObjectId, InputRange(0,127));

    }

    // Temporary variable

    array<int> ^temp;

    // Get info about first two FF axii on the device

    if((doi->Flags & (int)ObjectInstanceFlags::Actuator) != 0)

    {

    if(axis != nullptr)

    {

    temp = gcnew array<int,1> (axis->Length +1);

    axis->CopyTo(temp, 0);

    axis = temp;

    }

    else

    {

    axis = gcnew array<int, 1> (1);

    }

    // Store the offset of each axis

    axis[axis->Length - 1] = doi->Offset;

    if(axis->Length == 2)

    {

    break;

    }

    }

    }

    }

    protected:

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    ~Form1()

    {

    if (components)

    {

    delete components;

    }

    }

    private: System::ComponentModel::IContainer^ components;

    protected:

    private:

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private: System::Windows::Forms::Button^ button1;

    DirectInput::Device ^device;

    DirectInput::JoystickState ^joystickState;

    DirectInput::EffectObject ^effObj;

    DirectInput::Effect ^eff;

    private: System::Windows::Forms::Label^ labelX;

    private: System::Windows::Forms::Label^ labelY;

    private: System::Windows::Forms::Label^ label1;

    private: System::Windows::Forms::Label^ label2;

    private: System::Windows::Forms::Timer^ timer1;

    private: System::Windows::Forms::Label^ label3;

    private: System::Windows::Forms::Label^ labelZ;

    array<int> ^axis;

    #pragma region Windows Form Designer generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    void InitializeComponent(void)

    {

    this->components = (gcnew System::ComponentModel::Container());

    this->button1 = (gcnew System::Windows::Forms::Button());

    this->labelX = (gcnew System::Windows::Forms::Label());

    this->labelY = (gcnew System::Windows::Forms::Label());

    this->label1 = (gcnew System::Windows::Forms::Label());

    this->label2 = (gcnew System::Windows::Forms::Label());

    this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));

    this->label3 = (gcnew System::Windows::Forms::Label());

    this->labelZ = (gcnew System::Windows::Forms::Label());

    this->SuspendLayout();

    //

    // button1

    //

    this->button1->Location = System::Drawing::Point(128, 104);

    this->button1->Name = L"button1";

    this->button1->Size = System::Drawing::Size(75, 23);

    this->button1->TabIndex = 0;

    this->button1->Text = L"button1";

    this->button1->UseVisualStyleBackColor = true;

    this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

    //

    // labelX

    //

    this->labelX->AutoSize = true;

    this->labelX->Location = System::Drawing::Point(39, 184);

    this->labelX->Name = L"labelX";

    this->labelX->Size = System::Drawing::Size(35, 13);

    this->labelX->TabIndex = 1;

    this->labelX->Text = L"label1";

    //

    // labelY

    //

    this->labelY->AutoSize = true;

    this->labelY->Location = System::Drawing::Point(148, 184);

    this->labelY->Name = L"labelY";

    this->labelY->Size = System::Drawing::Size(35, 13);

    this->labelY->TabIndex = 2;

    this->labelY->Text = L"label2";

    //

    // label1

    //

    this->label1->AutoSize = true;

    this->label1->Location = System::Drawing::Point(42, 154);

    this->label1->Name = L"label1";

    this->label1->Size = System::Drawing::Size(14, 13);

    this->label1->TabIndex = 3;

    this->label1->Text = L"X";

    //

    // label2

    //

    this->label2->AutoSize = true;

    this->label2->Location = System::Drawing::Point(151, 154);

    this->label2->Name = L"label2";

    this->label2->Size = System::Drawing::Size(14, 13);

    this->label2->TabIndex = 4;

    this->label2->Text = L"Y";

    //

    // timer1

    //

    this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);

    //

    // label3

    //

    this->label3->AutoSize = true;

    this->label3->Location = System::Drawing::Point(240, 154);

    this->label3->Name = L"label3";

    this->label3->Size = System::Drawing::Size(14, 13);

    this->label3->TabIndex = 5;

    this->label3->Text = L"Z";

    //

    // labelZ

    //

    this->labelZ->AutoSize = true;

    this->labelZ->Location = System::Drawing::Point(243, 184);

    this->labelZ->Name = L"labelZ";

    this->labelZ->Size = System::Drawing::Size(35, 13);

    this->labelZ->TabIndex = 6;

    this->labelZ->Text = L"label4";

    //

    // Form1

    //

    this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

    this->ClientSize = System::Drawing::Size(292, 273);

    this->Controls->Add(this->labelZ);

    this->Controls->Add(this->label3);

    this->Controls->Add(this->label2);

    this->Controls->Add(this->label1);

    this->Controls->Add(this->labelY);

    this->Controls->Add(this->labelX);

    this->Controls->Add(this->button1);

    this->Name = L"Form1";

    this->Text = L"Form1";

    this->ResumeLayout(false);

    this->PerformLayout();

    }

    #pragma endregion

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

    //// Display some samples

    //MessageBox::Show(Convert::ToString(joystickState->X));

    //MessageBox::Show(Convert::ToString(joystickState->Y));

    timer1->Enabled = true;

     

    }

    private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {

    // Poll the device

    device->Poll();

    // Get joystick state

    joystickState = device->CurrentJoystickState;

    // Display the data

    labelX->Text = Convert::ToString(joystickState->X);

    labelY->Text = Convert::ToString(joystickState->Y);

    labelZ->Text = Convert::ToString((joystickState->Rz)/512);

    for each(EffectInformation ^ei in device->GetEffects(EffectType::All))

    {

    // If joystick supports constant force then apply it

    if(DInputHelper::GetTypeCode(ei->EffectType) == (int)EffectType::ConstantForce)

    {

    // Fill some generic values for effect

    eff = gcnew Effect();

    eff->SetDirection(gcnew array<int,1> (axis->Length));

    eff->SetAxes(gcnew array<int,1> (axis->Length));

    eff->ConditionStruct = gcnew array<Condition,1> (axis->Length);

    eff->EffectType = EffectType::ConstantForce;

    eff->Duration = (int)DI::Infinite;

    eff->Gain = 25000;

    eff->SamplePeriod = 0;

    eff->TriggerButton = (int)Microsoft::DirectX::DirectInput::Button::NoTrigger;

    eff->TriggerRepeatInterval = (int)DI::Infinite;

    eff->Flags = EffectFlags::ObjectOffsets | EffectFlags::Cartesian;

    eff->SetAxes(axis);

    // Create the effect using passed guid

    effObj = gcnew EffectObject(ei->EffectGuid, *eff, device);

    }

    }

    if (effObj == nullptr)

    {

    MessageBox::Show("ConstantForce is not supported.");

    }

    }

    };

    }

Page 1 of 1 (1 items) Previous Next