I need to do this entirely with math, take a look at this picture:

I start at an origin point, say (1,1), then I move 1 unit to (2,2) on both axis, then rotate 20deg.  Now I want to raycast to the left and right of this rotation, with a separation of 0.5 units between each raycast.

What I'm doing is building a path finder algorithm that basically looks 30 units ahead, raycasts a few times perpendicular to the future point, finds the midpoint between the min/max vectors that found the dirt road, and puts a green cube in the middle.

I thought this would be rather simple, yet here I am trying to figure out how to plot the exact geometry of a rotated left/right projection.

As you can see, I'm using cubes right now to represent where my ray's will be cast, they're just visual aids and won't exist in the future (which is why i want to do this completely with math).  However, the green center WILL be a game object.  This object gets rotated along the Y axis to first look at the previous centerpoint, then flip 180deg so that it looks forward from it.  The problem is, I need my grey points to also take that rotation and plot perpendicular to the green centerpoint.  Right now it's just extending on the x axis and is why near the end of the path you see more grey points.  I want the grey points to be curving along the path essentially.

I hope I explained this well enough! I'm also open to suggestions on how to do this if my approach is silly. Eventually this path will split and the algorithm has to detect the split and have the user decide which direction to go.

EDIT: After applying the math below (I opted to use the trig version) I go this result on my algorithm. THANK YOU!!!

up vote 0 down vote accepted

A unit vector in the xz plane pointing at a radianAngle counter-clockwise from the positive x axis has the formula:

Vector3 tiltedRight = new Vector3(
                Mathf.Cos(radianAngle),
                0,
                Mathf.Sin(radianAngle)
);

And a unit vector at the same angle counter-clockwise from the positive z axis has the formula:

Vector3 tiltedForward = new Vector3(
               -Mathf.Sin(radianAngle),
                0,
                Mathf.Cos(radianAngle)
);

So you can compute your points like so:

Vector3 projectedPoint = startPoint 
                 + titledForward * distanceForward
                 +   tiltedRight * distanceRight;

Or you can construct a local basis like so and hide all the trig math:

Quaternion turn = Quaternion.Euler(0, degreeAngle, 0);

Vector3 localOffset = new Vector3 (distanceRight, 0, distanceForward);

Vector3 projectedPoint = startPoint + turn * localOffset;
  • Thank you this worked brilliantly! I knew there would be some sin/cos involved but wasn't sure how the foward + right worked together. Nice! – mythstified Aug 9 at 3:34
  • The Unit Circle to the rescue! :) – DMGregory Aug 9 at 3:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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