DirectX SDK |
Create an effect object by using the IDirectInputDevice7::CreateEffect method, as in the following code example, where pdev2 points to an instance of the interface. This example creates a very simple effect that pulls the joystick away from the user at full force for half a second.
HRESULT hr; LPDIRECTINPUTEFFECT lpdiEffect; // receives pointer to created effect DIEFFECT diEffect; // parameters for created effect DWORD dwAxes[2] = { DIJOFS_X, DIJOFS_Y }; LONG lDirection[2] = { 18000, 0 }; DICONSTANTFORCE diConstantForce; diConstantForce.lMagnitude = DI_FFNOMINALMAX; // Full force diEffect.dwSize = sizeof(DIEFFECT); diEffect.dwFlags = DIEFF_POLAR | DIEFF_OBJECTOFFSETS; diEffect.dwDuration = 0.5 * DI_SECONDS; diEffect.dwSamplePeriod = 0; // = default diEffect.dwGain = DI_FFNOMINALMAX; // No scaling diEffect.dwTriggerButton = DIEB_NOTRIGGER; // Not a button response diEffect.dwTriggerRepeatInterval = 0; // Not applicable diEffect.cAxes = 2; diEffect.rgdwAxes = &dwAxes; diEffect.rglDirection = &lDirection; diEffect.lpEnvelope = NULL; diEffect.cbTypeSpecificParams = sizeof(DICONSTANTFORCE); diEffect.lpvTypeSpecificParams = &diConstantForce; hr = pdev2->CreateEffect(GUID_ConstantForce, &diEffect, &lpdiEffect, NULL);
In the method call, the first parameter identifies the supported effect with which the created effect is to be associated. The example uses one of the predefined GUIDs found in Dinput.h. If you use a predefined GUID, the call fails if the device does not support the effect.
The second parameter sets the parameters as specified in the DIEFFECT structure.
The third parameter receives a pointer to the effect object if the call is successful.
The DIEFF_POLAR flag specifies the type of coordinates used for the direction of the force. (See Effect Direction.) It is combined with DIEFF_OBJECTOFFSETS, which indicates that any buttons or axes used in other members are identified by their offsets within the DIDATAFORMAT structure for the device. The alternative is to use the DIEFF_OBJECTIDS flag, signifying that buttons and axes are identified by the dwType member of the DIDEVICEOBJECTINSTANCE structure returned for the object when it was enumerated with the IDirectInputDevice7::EnumObjects method.
For more information on the members of the DIEFFECT structure, see Effect Direction.
You create an effect object by using the DirectInputDevice.CreateEffect method, as in the following code example, where didev is a DirectInputDevice object. This example creates a very simple effect that pulls the joystick away from the user at full force for half a second.
Dim effectInfo As DIEFFECT Dim objDIEffect As DirectInputEffect With effectInfo .constantForce.lMagnitude = 10000 .lGain = 10000 .lDuration = 500000 .x = 18000 .lTriggerButton = -1 ' No trigger button End With didev.Acquire Set objDIEffect = didev.CreateEffect("GUID_ConstantForce", effectInfo)
The first parameter can be one of the predefined GUID aliases, or an actual GUID in string form known from hardware documentation or retrieved for an enumerated effect by using the DirectInputEnumEffects.GetEffectGuid method.
The relevant members of the DIEFFECT type vary according to the kind of effect. Constant forces are the simplest kind, requiring only a single type-specific parameter. The lGain, lDuration, and lTriggerButton members should be set for all effects, since the default values of 0 are not usually suitable.
By default, the direction of the effect is expressed in polar coordinates, meaning that DIEFFECT.x holds the direction from which the force comes, in hundredths of a degree, and DIEFFECT.y must be 0.
Effects are automatically downloaded to the device when created, provided the device is not full and is acquired at the exclusive cooperative level.