Microsoft DirectX 8.1 (Visual Basic) |
As the user moves the iron ball around using the joystick, the application adjusts the direction and magnitude of the effect. First, however, you should check whether the device enables changing the direction of the effect without stopping and restarting the effect. Microsoft® DirectInput® will stop and restart the effect automatically if necessary, but by doing this yourself, you might avoid unseemly breaks in continuity.
In order to examine the capabilities for any effect, you first enumerate supported effects. To do so, use the DirectInputDevice8.GetEffectsEnum method, which resolves to a DirectInputEnumEffects type. The DIEFT_CONSTANTFORCE flag enables you to restrict the enumeration to supported only constant force effects.
Dim diEnumEffects As DirectInputEnumEffects Set diEnumEffects = didev.GetEffectsEnum(DIEFT_CONSTANTFORCE)
Next, examine the enumeration for the particular effect you are interested in. If the enumerated effect meets the criteria, call the DirectInputEnumEffects.GetDynamicParams method to examine the effect for parameters that can be changed without stopping and restarting. Examine the flags returned by GetDynamicParams, comparing them against known values from CONST_DIEPFLAGS. In the case of the following example, the params variable containing the result flags is compared against DIEP_DIRECTION to ensure that the direction of the effect can be changed while the effect is running.
Dim params As Long Dim i As Long For i = 1 To diEnumEffects.GetCount ' Look for the standard constant force. There could be others. If diEnumEffects.GetEffectGuid(i) = "GUID_ConstantForce" Then params = diEnumEffects.GetDynamicParams(i) Exit For End If Next i If Not (params And DIEP_DIRECTION) Then ' Cannot change direction dynamically. Take remedial action such ' as limiting points at which modifications will be made. End If
Similarly, to learn whether you can change the magnitude of the effect dynamically, perform the following check.
If Not (params And DIEP_TYPESPECIFICPARAMS) ' Cannot change type-specific parameters dynamically. End If
The application modifies the effect each time the device has been polled for input and the iron ball has moved. Assume that the new magnitude is stored in CurrentMag, and the new direction (in degrees) in CurrentBearing. To make the necessary changes, you need to initialize only those members of DIEFFECT that are relevant.
EffectInfo.constantForce.lMagnitude = CurrentMag EffectInfo.x = CurrentBearing * DI_DEGREES
You now pass the DIEFFECT type with the new data to DirectInputEffect.SetParameters along with flags indicating which members contain valid data.
di_effect.SetParameters(EffectInfo, _ DIEP_DIRECTION Or DIEP_TYPESPECIFICPARAMS)
The DIEP_TYPESPECIFICPARAMS flag indicates that any member containing information particular to that type of effect is valid. In the case of a constant force, this means the constantForce member.