0
\$\begingroup\$

I'm currently working on a system where I create a linear path by spawning waypoints, but i'm struggling to figure out how to compute the rotation needed to render the corner waypoints correctly.

if I rotate it towards (nextSegment.Coordinate - myCoordinate) it almost seems correct, most corners are behaving how I want.

image of corner segments rotating in the direction of their next segment

but you can see the first 2 corners, and one near the end are incorrect.

if I rotate it towards (prevSegment.Coordinate - myCoordinate) it solves the rotation for the incorrect segments in the previous solution.

image of corner segments rotating in the direction of their previous segment

I know the coordinate of the next cell, and the previous cell and the cell i'm trying to rotate.

But then the other segments are incorrect. I cannot figure out the pattern on how to decide which segment to rotate towards. Any ideas?

\$\endgroup\$
  • \$\begingroup\$ Can you add some more detail? What are you rotating? I guess it's the red blocks and they are all the red blocks identical? What rotation values have you calculated in each case? \$\endgroup\$ – Jay Feb 2 '18 at 0:29
0
\$\begingroup\$

You cannot correctly angle the L shaped block without having the coordinate of both the previous block and next block. That being said...

The direction of the arrows is irrelevant to the problem. Each line on the L shaped block must point to a block next to it, so you only have 4 possibilities for corner shapes.

There are many ways to do it, but you could rotate it based on which quadrant the average of the coordinates of the next and previous tiles end up in. (relative to current)

Avg.x = (nextSeg.x + prevSeg.x)/2 - myCoord.x
Avg.y = (nextSeg.y + prevSeg.y)/2 - myCoord.y

if Avg.x > 0 && avg.y > 0
    Angle = 0
if Avg.x > 0 && avg.y < 0
    Angle = -90
if Avg.x < 0 && avg.y > 0
    Angle = 90
if Avg.x < 0 && avg.y <  0
    Angle = 180

Angles depend on your 0 deg position

EDIT: updated per response, multiplied by 2 to fix flooring

avg.x = (nextSeg.x + prevSeg.x - 2*myCoord.x);
avg.z = (nextSeg.z + prevSeg.z - 2*myCoord.z);

if avg.x > 0 && avg.z > 0
    angle = 0
if avg.x > 0 && avg.z < 0
    angle = -90
if avg.x < 0 && avg.z > 0
    angle = 90
if avg.x < 0 && avg.z <  0
    angle = 180
\$\endgroup\$
  • \$\begingroup\$ this doesn't seem to produce the correct behaviour. however I do have the both the previous block and the next block, so would that produce a different solution? \$\endgroup\$ – Scott Feb 2 '18 at 17:29
  • \$\begingroup\$ Are the positions unit integer values? i.e. 1,2? Also what is the default orientation of the L shaped block ( rotation of 0 deg )? \$\endgroup\$ – Kyy13 Feb 2 '18 at 17:33
  • \$\begingroup\$ yeah positions are integers on the X, Z plane. the default orientation is 0 deg, so its drawn exactly like the L shape to begin with. \$\endgroup\$ – Scott Feb 2 '18 at 18:07
  • \$\begingroup\$ if you take a look at the 2nd image I posted, the first corner is the default orientation (top left, 3rd block down) \$\endgroup\$ – Scott Feb 2 '18 at 18:08
  • \$\begingroup\$ I updated my response to fix flooring ( just multiplied everything by 2). Try that. And make sure you convert to radians if you need radians :) \$\endgroup\$ – Kyy13 Feb 2 '18 at 18:26

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.