# Calculate corner rotation to fit between 2 waypoints

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.

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.

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?

• 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? – Jay Feb 2 '18 at 0:29

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

• 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? – Scott Feb 2 '18 at 17:29
• 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 )? – Kyy13 Feb 2 '18 at 17:33
• 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. – Scott Feb 2 '18 at 18:07
• if you take a look at the 2nd image I posted, the first corner is the default orientation (top left, 3rd block down) – Scott Feb 2 '18 at 18:08
• 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 :) – Kyy13 Feb 2 '18 at 18:26
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}