Vector math help

What kind of math would be needed to turn the green points into the black lines I have? I’ve tried with transform.left but that doesn’t angle stuff right :frowning:

I don’t see how transform.left would have anything to do with these green points… primarily because they aren’t transforms.

If you mean, this is a drawn representation of gameobjects, where the green dots are gameobjects in your game… well, as you can tell, since you can draw what you want without the need of the rotation of said points, then something like ‘transform.left’ is unnecessary in defining it.

Now some issues that we have in your conveying your problem to us. WHAT are these black lines?

First and foremost, they aren’t consistently drawn. It appears slap dashed, probably in MSPaint. Various lines overlap, and others don’t connect, all arbitrarily. Nor is the scales in spacing from the green dots consistent. And really… I shouldn’t expect your drawing to be accurate to perfect detail… this is probably a representation of something.

But do you remember in geometry class in middle school, when you’d be given the task to determine the length of a line segment. Or the angle between 2 line segments. And if you attempted to just ‘measure’ the value with a ruler or compass, you’d get the answer wrong, often because the images were representations and weren’t accurate to perfect detail. Instead the drawing had some information on it, usually the lengths/angles/etc of neighbouring line segments. And you had to calculate everything based on the rules you learned in geometry class.

Yeah… that information describing what we’re looking at… that’s something needed to solve the problem.

For example, in the case of your image.

Am I to assume that the red lines are straight and directly connect to the middle of the green dots?

Am I to assume that the black lines NOT passing through the green dots are parallel to the red lines?

What about the blacklines that pass through the green dots? Are they perpendicular to the red lines? And if they are… to which red line??? Listing the dots 1,2,3 from bottom to top, and the red lines A,B,C from bottom to top, the black line that passes through 2 appears perpendicular to A, the preceeding red line. While black line through 3 appears perpendicular to C, the following red line.

Or are the black lines passing through the green dots supposed to be the averages of the normals to the red lines coming into the green dot from either side?

Oh, and, your rudimentary question of:

Well, this whole time I’ve been talking about it. The math is called geometry.

1 Like

given the forum logo… you’ve got a path and you want to make a “road”?

At each point you need to work out the “Direction” towards the next point, and the previous point (handle end points where there isn’t a previous/next). Take the angle between those two directions to figure out which way is “outwards” (note angle will give you the smaller of the two possible angles), halve it and set points at whatever distance you need in that direction (trick would be one at a positive distance towards the inside of the curve and one at a negative distance towards the outside of the curve).

alternatively, look at the line renderers… they do something similar, but I’ve always found them to not handle corners very well.

Yeah basically I’m trying to generate a line renderer type mesh with a list of points.

@aaro4130 : What you’re wanting to do are create vertices at the black intersections so you can connect them all up with lines or a mesh, right? If so, you can do this with some simple vector math.

I’d start by looping through all the green points from start to finish, starting with the second one. Compute a vector from that point to the point before it and normalize it so you know the direction of the red line. Then just do a cross product on that vector to get the “left or right” direction you’re talking about but without anything like transform.left in there. A cross product gives you a new vector that’s perpendicular to the original one. In this case you’re wanting a vector that’s perpendicular to the red ones.

Starting at the green point, shoot out along that vector by the same distance (one positive and one negative) and you will have your two points to connect the black line going through the green dots.

It won’t look exactly like you drew it, but think it’ll probably be more or less what you want. Starting at that top green dot in your diagram, you can see the black left/right line is perpendicular to that first red line. That’s what’ll happen at every intersection. I.e., the second green point you drew shows the black line going through it being perpendicular to the next point instead of the current one. With my suggestion it would not be exactly like that but at the end of the day you probably won’t care that much. :wink:

Back in 2000 or 2001 probably I did something similar for a racing simulator I was working on and it worked well for roads, so I’d start with that. If for some reason you really wanted it to work like your diagram where the perpendiculars alternated directions, you could probably do that by alternating between having the cross product computation use the vector to the next green point and the previous one. That should alternate the angles like you drew but make it a little more complicated. I didn’t find it necessary in my road system back in the day, you might not either.

Could you help out with some code? I can understand code very well but 3d math/math in general is a very weak strength for me :frowning:

EDIT : All I would need is a simple code to get the left/right points of a road from a vector, I can take the rest from there :slight_smile:

This is just off the top of my head, not tested and may have a case error or two (using Wordpad at the moment). Hopefully it’ll give you the basic idea. Instead of using previousGreenPoint and nextGreenPoint I’d just use an array and use index i and i-1 (or i and i+1). It’s just to illustrate the idea:

Vector3 previousGreenPoint; //Set to one of the green dot positions
Vector3 nextGreenPoint; //Set to the next green dot position

Vector3 redLineVector = nextGreenPoint - previousGreenPoint;
Vector3 redLineVectorNormalized = redLineVector.normalized;
Vector3 upVector = ..... //This should be up relative to the surface.  If it's flat ground or just to get things going you could just use 0,1,0 for this.

Vector3 leftRightVector = Vector3.Cross(redLineVectorNormalized,upVector);

//Now starting at the previousGreenPoint, shoot out left and right by whatever distance you want to find points for the left and right sides:

float width = .... ; //
float halfWidth = width / 2;

Vector3 leftPoint = previousGreenPoint + leftRightVector * halfWidth;
Vector3 rightPoint = previousGreenPoint - leftRightVector * halfWidth;

The leftPoint and rightPoint should be the points you’re looking for.

1 Like

Thank you!

1 Like

My pleasure, and thanks for showing the first results. Looks like you’re off to a good start there. :slight_smile:

1 Like