Can you help transform this function to move the point not only by Forward distance but also by a distance to the right or left?

#define FORWARD_DISTANCE 100.0

// Initialize vectors
static float vEntPosition[3]; static float vEntAngle[3]; 

// Gets the origin
....GetAbsOrigin(nEntity, vEntPosition);

// Gets the  eye angle
....GetEyeAngle(nEntity, vEntAngle);

// Calculate effect area origin
vEntPosition[0] = vEntPosition[0] + (FORWARD_DISTANCE * Cosine(DegToRad(vEntAngle[1])));
vEntPosition[1] = vEntPosition[1] + (FORWARD_DISTANCE * Sine(DegToRad(vEntAngle[1])));
  • \$\begingroup\$ Repeat the same thing with (angle + pi/2)? You might want to use the fact that sin(a+pi/2) = cos(a) and cos(a+pi/2) = -sin(a). \$\endgroup\$ – HolyBlackCat Dec 9 '17 at 12:10

Assuming your coordinate system has the y+ axis 90 degrees counter-clockwise from x+, then:

float angle = DegToRad(vEntAngle[1]);
float cosine = Cosine(angle);
float sine = Sine(angle);

vEntPosition[0] = vEntPosition[0]
                 + cosine * FORWARD_DISTANCE
                 + sine * RIGHTWARD_DISTANCE;

vEntPosition[1] = vEntPosition[1]
                 + sine * FORWARD_DISTANCE
                 - cosine * RIGHTWARD_DISTANCE;

This is a straightforward application of the unit circle - so brush up on your trigonometry and you'll find this kind of vector math can go very smoothly. :)

Diagram showing how to construct unit vectors from an angle


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.