Microsoft DirectX 8.1 (Visual Basic)

Effect Direction

Microsoft® DirectX® for Microsoft Visual Basic® supports two-axis effects on the x-axis and y-axis.

The direction of an effect is the direction from which it comes. An effect with a direction along the negative y-axis tends to push the stick along the positive y-axis (toward the user). If the user must push the stick toward the left in order to counteract an effect, the effect has a left direction; that is, it lies on the negative x-axis.

Direction can be expressed in polar or Cartesian coordinates. By default, polar coordinates are used, and the direction is specified in the x member of the DIEFFECT type, with y always 0. To use Cartesian coordinates, you must specify DIEFF_CARTESIAN in the lFlags member and supply values in both x and y.

Polar coordinates are expressed as a single angle, in hundredths of a degree clockwise from whatever zero-point, or true north, has been established for the effect. Normally this is the negative y-axis; that is, away from the user. Thus an effect with a polar coordinate of 9,000 has a direction of east, or to the user's right, and the user must exert force to the right to counteract it.

Cartesian coordinates are similar to 3-D vectors and are most useful for matching a force to the user's orientation in a 3-D environment. If you draw a straight line on graph paper with an origin of (0, 0) at the center of the page, the direction of the line can be defined by the coordinates of any intersection that it crosses, regardless of the distance from the origin. A direction of (1, –2) and a direction of (5, –10) are exactly the same.

Note  The coordinates used in creating force-feedback effects define only direction, not magnitude or distance.

In the example in Creating an Effect, the direction of a two-dimensional force is defined in polar coordinates. The force has a south direction—it comes from the direction of the user, so the user has to pull the stick to counteract it. The direction is 180 degrees clockwise from north, and was assigned as follows, where effectInfo is a valid DIEFFECT type.

effectInfo.x = 18000

The effectInfo.y member must be set to 0, which it is by default. For greater clarity, the assignment could also be expressed this way:

effectInfo.x = 180 * DI_DEGREES

How do you accomplish the same thing with Cartesian coordinates? Presuming that you have used the DIEFF_CARTESIAN flag in the lFlags member, you would specify the direction like this:

effectInfo.x = 0
effectInfo.y = 1

As long as the x value is 0, any number in the y value will give the same effect because only direction is indicated, not magnitude. The theory of effect directions can be difficult to grasp, but the practice is fairly straightforward.

The following code example reverses the direction of a force represented by the DirectInputEffect object dieff, which was created using the global DIEFFECT type effectInfo:

effectInfo.lFlags = DIEFF_CARTESIAN
effectInfo.X = effectInfo.X * -1
effectInfo.Y = effectInfo.Y * -1
Call dieff.SetParameters(effectInfo, DIEP_DIRECTION)

Examples of Setting Effect Direction

Single-Axis Effects with Cartesian Coordinates

Setting up the direction for a single-axis effect is easy because you need to specify only the direction of the axis. Put the DIEFF_CARTESIAN flag in the lFlags member of the DIEFFECT structure and set the x or y member to either 1 or -1, depending on the direction you want the effect to come from.

The following code example sets an x-axis effect with a right to left direction:

Dim eff As DIEFFECT
    
eff.lFlags = DIEFF_CARTESIAN Or DIEFF_OBJECTOFFSETS
eff.x = 1
eff.y = 0

Single-Axis Effects with Polar Coordinates

The same effect could be set up just as easily using polar coordinates. Put the DIEFF_POLAR flag in the lFlags member of the DIEFFECT structure and set the x member to either 0, 90, 180, or 270 degrees.

The following code example sets the same x-axis effect as in the previous example:

Dim eff As DIEFFECT
    
eff.lFlags = DIEFF_POLAR Or DIEFF_OBJECTOFFSETS
eff.x = 90 * DI_DEGREES
eff.y = 0

Two-Axis Effects with Polar Coordinates

Setting up the direction for a polar two-axis effect is no different than a polar one-axis effect. The only difference is that the range of degree values is not restricted to the four axis values.

The following code example sets an effect originating from the user's upper right:

Dim eff As DIEFFECT
    
eff.lFlags = DIEFF_POLAR Or DIEFF_OBJECTOFFSETS
eff.x = 45 * DI_DEGREES
eff.y = 0

Two-Axis Effects with Cartesian Coordinates

Setting up the direction for a Cartesian two-axis effect is straightforward as well. First, set the DIEFF_CARTESIAN flag in lFlags. The x and y members will be filled with the x and y coordinates of any point on a line of direction through that point and the origin (0,0).

The following code example sets the same effect as in the previous sample:

Dim eff As DIEFFECT
    
eff.lFlags = DIEFF_CARTESIAN Or DIEFF_OBJECTOFFSETS
eff.x = 1
eff.y = 2