Road system

Started by
4 comments, last by DonceLT 14 years, 9 months ago
I am creating simple road system. It should do this: 1)Do line of road(adding points) 2)Get sides of road(from road points, vector) 3)Connect road sides and make a road. So my problem is with 2 part. I calculate angle of road point side with this: k1 = atan2((- t.x + t.x), (- t<span style="font-weight:bold;">.y + t.y)); k2 = atan2(( t<span style="font-weight:bold;">.x - t.x), ( t<span style="font-weight:bold;">.y - t.y)); k1 = k1 * 180 / 3.14; k2 = k2 * 180 / 3.14; k = (k1 + k2) / 2; It calculates angle with point of next and previous points in radian, then converts to degrees and calculates average of them. But the problem is this: <a href='http://img8.imageshack.us/i/screendiv.jpg/'><img src='http://img8.imageshack.us/img8/8231/screendiv.jpg' border='0' alt='Image Hosted by ImageShack.us'/></a>&lt;br/&gt; The rodes collides (3, 11 and 16 points). In those points just need to reverse vectors, but I dont know how to find those points, which need this. The calculations dont have any mistakes, but i this i need something more. Do I need to check collision or there is some easier way, or miskates? Any thoughts? Sorry for English, its not easy to talk about mathematic in not my born language :) <!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - DonceLT on June 22, 2009 3:53:53 AM]<!–EDIT–></span><!–/EDIT–>
Advertisement
I would use vector math for this instead of angles.

Consider that each segment of the road has a normal vector. If I'm thinking about this correctly, the vector that you're looking for will be the normalized average of the normals of the two segments that meet at a given intersection. (A diagram would probably help make this more clear - drawing a couple of example cases might help clarify things.)

If you want to stick with angles, it shouldn't be too hard to determine when the angle needs to be negated. From looking at the image you posted, it seems you could simply check to see if a road edge crosses the hyperplane associated with the corresponding medial segment.

I'll also mention that the method you're using to build the road mesh may not always yield the best results, in that the width of the road sections will vary depending on the angles of the turns. Another approach would be to build lines parallel to the segments and displaced along the segment normals by a specific distance (the desired 'radius' of the road), and then intersect these lines to yield the corners where the road sections meet.
Quote:Original post by jyk
I would use vector math for this instead of angles.

Consider that each segment of the road has a normal vector. If I'm thinking about this correctly, the vector that you're looking for will be the normalized average of the normals of the two segments that meet at a given intersection. (A diagram would probably help make this more clear - drawing a couple of example cases might help clarify things.)

If you want to stick with angles, it shouldn't be too hard to determine when the angle needs to be negated. From looking at the image you posted, it seems you could simply check to see if a road edge crosses the hyperplane associated with the corresponding medial segment.

I'll also mention that the method you're using to build the road mesh may not always yield the best results, in that the width of the road sections will vary depending on the angles of the turns. Another approach would be to build lines parallel to the segments and displaced along the segment normals by a specific distance (the desired 'radius' of the road), and then intersect these lines to yield the corners where the road sections meet.


Thanks, i think i'll continue with angles. The size of road width of the road sections varies depending on angles, but can create very detail road with low angles, so this variation would be very small, and drawing 3d (already done with angles) its faster I think :)
I also suggest that you use vectors. Unless you're targeting the Ti-83 or commodore, I think you're going to want some smooth roads.

Consider the road a spline, a vector of piecewise polynomials. This will give you nice smooth curved segments. The spline can be sampled at any frequency, if you sampled it at one sample per segment then it would come out with straight roads like you have.

You'll need a function which is capable of generating a coordinate system at any point along the spline. This 3d coordinate system would have a basis vector pointing along the road direction, perpindicular to the road (to the side) and one in the road normal direction, which is perpindicular to the previous two.

bool SampleSpline(const Spline &road, float t, Vector &roadPositionOut, Vector &roadDirOut, Vector &roadSideDirOut, Vector &roadNormalOut);

Where t is some value from 0 to 1 along the roadway.

So you'd iterate from 0 to 1 at some interval, say 0.01 to give you 100 samples. At each sample you create a coordinate space, then you project your geometry into this space. Imagine a triangle strip road that just goes straight. Now imagine we take that triangle strip and for each row of vertices we multiply them by a coordinate space generated at some point along the road. The triangle strip would then be projected onto the road path.

If you made the triangle strip more interesting, with much more triangles, you could have complex texture coordinates to have transitions between grass, gravel, cement, and different lane markings. Then project this uber strip onto the path.

Good luck!

I'll elaborate a little on what i mean by project.

Say we have a cross section of the road, it has three vertices, one along the left side of the road, one along the center, and one along the right side of the road.

float roadHalfWidth = 10;
int roadShapeSize = 3;
float roadShape[roadShapeSize ] = { -roadHalfWidth, 0, roadHalfWidth };

Say we called that function, for the start of the road, t = 0:

Vector roadPos, roadDir, roadSide, roadNormal;
SampleSpline(road, 0, roadPos, roadDir, roadSide, roadNormal);

This is how you'd project it into the coordinate space, for the most part we're only interested in the side direction and position of the road. Unless you wanna get fancy.
Vector sampleRoadShape[roadShapeSize];for (int i = 0; i < roadShapeSize ; ++i)   sampleRoadShape = roadPos + roadSide * roadShape;


sampleRoadShape now contains the vertex locations for this sample along the path. If you have 100 of these samples, you can make a triangle strip between them to make the connected roadway.

Also, if you wanted to have the roadway slope down to the sides, like a wedge, you'd need to store a height component for each vertex in the road cross section, multiply this by the roadNormal, and add it to the sampleRoadShape for that vertex. Or you can store the roadShape as a Vectors and build a matrix with the 4 vectors that the sample function retrieves and then just use matrix multiplication to do the projection "all at once"

[Edited by - bzroom on June 22, 2009 12:28:43 PM]
Quote:Original post by DonceLT
k1 = atan2((- t.x + t.x), (- t<span style="font-weight:bold;">.y + t.y));<br> k2 = atan2(( t<span style="font-weight:bold;">.x - t.x), ( t<span style="font-weight:bold;">.y - t.y));<br> k1 = k1 * 180 / 3.14;<br> k2 = k2 * 180 / 3.14;<br> k = (k1 + k2) / 2;<br>It calculates angle with point of next and previous points in radian, then converts to degrees and calculates average of them. <br><!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>Disregarding more advanced methods for a minute, why not just do this if you want to use angles?<br><br>k = atan2(( t.x - t.x), ( t.y - t.y));<br>k = k * 180 / 3.14;<br><br>The direction of the road at each point should be the direction from the previous point to the next point, which you can calculate directly rather than taking an average that'll get confused by the transition from 360 degrees to 0.
Quote:Original post by Fingers_
Quote:Original post by DonceLT
k1 = atan2((- t.x + t.x), (- t<span style="font-weight:bold;">.y + t.y));<br> k2 = atan2(( t<span style="font-weight:bold;">.x - t.x), ( t<span style="font-weight:bold;">.y - t.y));<br> k1 = k1 * 180 / 3.14;<br> k2 = k2 * 180 / 3.14;<br> k = (k1 + k2) / 2;<br>It calculates angle with point of next and previous points in radian, then converts to degrees and calculates average of them. <br><!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>Disregarding more advanced methods for a minute, why not just do this if you want to use angles?<br><br>k = atan2(( t.x - t.x), ( t.y - t.y));<br>k = k * 180 / 3.14;<br><br>The direction of the road at each point should be the direction from the previous point to the next point, which you can calculate directly rather than taking an average that'll get confused by the transition from 360 degrees to 0.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>This helps with angles. And now then I had done with angles, i can begin with vectors, thanks guys !! :)

This topic is closed to new replies.

Advertisement