Microsoft DirectX 8.1 (Visual Basic)

Step 3: Creating an Effect

The effect created in this example simulates the pull of a stationary object, such as a magnet, on a movable object, such as an iron ball. The movable object is controlled by the joystick. At the beginning of the simulation, the iron ball is due south of the magnet and experiencing one-half the maximum pull.

The pull of the magnet is simulated by a constant force. The first step is to set up the DIEFFECT type that describes the force.

Dim EffectInfo as DIEFFECT
With EffectInfo
    .constantForce.lMagnitude = 5000
    .lDuration = -1       ' Infinite
    .x = 18000
    .lGain = 10000        ' Play at full magnitude
    .lTriggerButton = -1  ' No trigger button
End With

By setting the x parameter to 18,000, you specify a force originating from a spot 180 degrees from due north. In other words, a force moving from due south toward due north, away from the user. For an in-depth discussion of effect directions, see Effect Direction

All other members of DIEFFECT are either not relevant to a constant force or are valid with a value of 0.

Now that the effect has been defined, it can be created using the DirectInputDevice8.CreateEffect method.

Dim di_effect As DirectInputEffect
Set di_effect = didev.CreateEffect("GUID_ConstantForce", EffectInfo)

If the device is in an acquired state at this time, the effect is automatically downloaded. If not, the effect will be downloaded when it is started using the DirectInputEffect.Start method.

di_effect.Start(1, 0)

Only one iteration of the effect is needed because it has infinite duration and no flags are required.

While a constant unchanging effect can be useful, an effect might also change as it plays or in reaction to user input. This is discussed in Step 4: Modifying an Effect.