-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RoadLane offset approx algorithm #155
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -262,10 +262,6 @@ func generate_lane_segments(_debug: bool = false) -> bool: | |||||
new_ln.lane_next_tag = this_match[3] | ||||||
new_ln.name = ln_name | ||||||
|
||||||
# Now decide where the two poitns should go, and their magnitudes. | ||||||
var in_pos: Vector3 = start_point.global_transform.origin | ||||||
var out_pos: Vector3 = end_point.global_transform.origin | ||||||
|
||||||
var tmp = get_transition_offset( | ||||||
ln_type, ln_dir, lane_shift, end_is_wider, max_rev_shift) | ||||||
var start_shift:float = tmp[0] | ||||||
|
@@ -274,9 +270,6 @@ func generate_lane_segments(_debug: bool = false) -> bool: | |||||
var in_offset = lanes_added * start_point.lane_width - start_offset + start_shift | ||||||
var out_offset = lanes_added * end_point.lane_width - end_offset + end_shift | ||||||
|
||||||
in_pos += start_point.global_transform.basis.x * in_offset * _start_flip_mult | ||||||
out_pos += end_point.global_transform.basis.x * out_offset * _end_flip_mult | ||||||
|
||||||
# Set direction | ||||||
# TODO: When directionality is made consistent, we should no longer | ||||||
# need to invert the direction assignment here. | ||||||
|
@@ -286,14 +279,7 @@ func generate_lane_segments(_debug: bool = false) -> bool: | |||||
# TODO(#46): Swtich to re-sampling and adding more points following the | ||||||
# curve along from the parent path generator, including its use of ease | ||||||
# in and out at the edges. | ||||||
new_ln.curve.add_point( | ||||||
new_ln.to_local(in_pos), | ||||||
new_ln.to_local(to_global(curve.get_point_in(0))), | ||||||
new_ln.to_local(to_global(curve.get_point_out(0)))) | ||||||
new_ln.curve.add_point( | ||||||
new_ln.to_local(out_pos), | ||||||
new_ln.to_local(to_global(curve.get_point_in(1))), | ||||||
new_ln.to_local(to_global(curve.get_point_out(1)))) | ||||||
offset_curve(self, new_ln, in_offset, out_offset, start_point, end_point) | ||||||
|
||||||
# Visually display. | ||||||
new_ln.draw_in_editor = container.draw_lanes_editor | ||||||
|
@@ -321,6 +307,77 @@ func generate_lane_segments(_debug: bool = false) -> bool: | |||||
return any_generated | ||||||
|
||||||
|
||||||
## Offsets a destination curve from a source curve by a specified distance. | ||||||
# Evaluates 4 points on source curve: Point 0 and 1 positions as well as | ||||||
# point-0-out and point-1-in handles. Requires transforms for point 0 | ||||||
# and point 1, which determine the direction of the handles. Calculates best | ||||||
# fit position for destination curve given the supplied curves, transforms, | ||||||
# and distance. | ||||||
func offset_curve(road_seg: Spatial, road_lane: Path, in_offset: float, out_offset: float, rp0: Spatial, rp1: Spatial): | ||||||
|
||||||
var src = road_seg.curve | ||||||
var dst = road_lane.curve | ||||||
var a_gbasis = rp0.global_transform.basis | ||||||
var d_gbasis = rp1.global_transform.basis | ||||||
var in_pos = start_point.global_transform.origin + (a_gbasis.x * in_offset * _start_flip_mult) | ||||||
var out_pos = end_point.global_transform.origin + (d_gbasis.x * out_offset * _end_flip_mult) | ||||||
|
||||||
# Get initial point locations | ||||||
var pt_a = to_global(src.get_point_position(0)) | ||||||
var pt_b = to_global(src.get_point_position(0) + src.get_point_out(0)) | ||||||
var pt_c = to_global(src.get_point_position(1) + src.get_point_in(1)) | ||||||
var pt_d = to_global(src.get_point_position(1)) | ||||||
|
||||||
# TTD: Project point(s) onto plane(s) | ||||||
|
||||||
# Project primary curve points to secondary curve points | ||||||
var pt_e = pt_a + (a_gbasis.x * in_offset) | ||||||
var pt_i = pt_b + (a_gbasis.x * in_offset) | ||||||
var pt_h = pt_d + (d_gbasis.x * out_offset) | ||||||
var pt_j = pt_c + (d_gbasis.x * out_offset) | ||||||
|
||||||
# Get vectors from points | ||||||
var vec_ab = pt_b - pt_a | ||||||
var vec_bc = pt_c - pt_b | ||||||
var vec_cd = pt_d - pt_c | ||||||
|
||||||
# Calculate secondary curve handles and setup curves | ||||||
var angle_q = -vec_ab.signed_angle_to(vec_bc, a_gbasis.y) * 0.5 | ||||||
var angle_s = vec_cd.signed_angle_to(vec_bc, d_gbasis.y) * 0.5 | ||||||
var offset_q = tan(angle_q) * in_offset | ||||||
var offset_s = tan(angle_s) * out_offset | ||||||
var pt_f = a_gbasis.z * (vec_ab.length() + offset_q) | ||||||
var pt_g = -d_gbasis.z * (vec_cd.length() + offset_s) | ||||||
var rad_ninety_deg = 1.5707963267949 # 90 degrees in radians | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May as well do something like this, I think it'll be faster on runtime anyways and more exact potentially
Suggested change
|
||||||
var margin = 0.1745329 # Margin to check above/below 90. 0.174 is roughly 10 degrees | ||||||
|
||||||
if abs(angle_q) > rad_ninety_deg - margin and abs(angle_q) < rad_ninety_deg + margin: | ||||||
# Angle is close to 90deg. Use default values. | ||||||
dst.add_point( | ||||||
road_lane.to_local(in_pos), | ||||||
road_lane.to_local(to_global(curve.get_point_in(0))), | ||||||
road_lane.to_local(to_global(curve.get_point_out(0)))) | ||||||
else: | ||||||
# Use calculated values | ||||||
dst.add_point( | ||||||
road_lane.to_local(pt_e), | ||||||
road_lane.to_local(to_global(src.get_point_in(0))), | ||||||
road_lane.to_local(to_global(pt_f))) | ||||||
|
||||||
if abs(angle_s) > rad_ninety_deg - margin and abs(angle_s) < rad_ninety_deg + margin: | ||||||
# Angle is close to 90deg. Use default values. | ||||||
dst.add_point( | ||||||
road_lane.to_local(out_pos), | ||||||
road_lane.to_local(to_global(curve.get_point_in(1))), | ||||||
road_lane.to_local(to_global(curve.get_point_out(1)))) | ||||||
else: | ||||||
# Use calculated values | ||||||
dst.add_point( | ||||||
road_lane.to_local(pt_h), | ||||||
road_lane.to_local(to_global(pt_g)), | ||||||
road_lane.to_local(to_global(src.get_point_out(1)))) | ||||||
|
||||||
|
||||||
## Offset the curve in/out points based on lane index. | ||||||
## | ||||||
## Track the init (for reverse) or the stacking (fwd) number of | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general here and below, would suggest implied typing or explicit where required. This is a somewhat heavy process and so having type hints will help godot make the more efficient compile choices.