Improvement to intersection data calculation
This commit is contained in:
@@ -22,3 +22,12 @@ pub fn compareIntersections(ctx: *const Node, inter_a: st.IntersectionData, inte
|
|||||||
|
|
||||||
return distance_a < distance_b;
|
return distance_a < distance_b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// felt cute, might delete it later idk
|
||||||
|
pub fn listContains(comptime T: type, element: *T, list: *[]*T) bool {
|
||||||
|
for (0..list.len) |i| {
|
||||||
|
if (list[i] == element) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
@@ -83,6 +83,15 @@ pub const Node = struct {
|
|||||||
pub fn getRelativeInterDistance(self: *const Node, intersection: st.IntersectionData) f32 {
|
pub fn getRelativeInterDistance(self: *const Node, intersection: st.IntersectionData) f32 {
|
||||||
return ut.calculate_length(intersection.pos, self.pos);
|
return ut.calculate_length(intersection.pos, self.pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Searches for the road pointer within roads list
|
||||||
|
pub fn roadsContains(self: *const Node, road_to_search: *const Road) bool {
|
||||||
|
for (self.roads.items) |road| {
|
||||||
|
if (road == road_to_search) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO tests
|
// TODO tests
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ pub const Road = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Checks whether pos coordinate is on the referenced road
|
/// Checks whether pos coordinate is on the referenced road
|
||||||
pub fn isHighlighted(self: *const Road, pos: rl.Vector2) bool {
|
pub fn collides(self: *const Road, pos: rl.Vector2) bool {
|
||||||
return rl.checkCollisionPointLine(pos, self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE);
|
return rl.checkCollisionPointLine(pos, self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ pub const RoadManager = struct {
|
|||||||
/// Returns if pos is pointing at a road, or null if it isn't at any
|
/// Returns if pos is pointing at a road, or null if it isn't at any
|
||||||
pub fn getHighlightedRoad(self: *const RoadManager, pos: Vector2) ?*Road {
|
pub fn getHighlightedRoad(self: *const RoadManager, pos: Vector2) ?*Road {
|
||||||
for (self.roads.items) |road| {
|
for (self.roads.items) |road| {
|
||||||
if (road.isHighlighted(pos)) return road;
|
if (road.collides(pos)) return road;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -156,6 +156,8 @@ pub const Simulator = struct {
|
|||||||
const intersections = self.getIntersectingRoads(self.allocator, temp, cur_node) catch |err| {
|
const intersections = self.getIntersectingRoads(self.allocator, temp, cur_node) catch |err| {
|
||||||
std.debug.panic("Intersection selection failure: {}\n", .{err});
|
std.debug.panic("Intersection selection failure: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
defer self.allocator.free(intersections);
|
||||||
|
|
||||||
// DEBUG TODO REMOVE
|
// DEBUG TODO REMOVE
|
||||||
std.debug.print("Displaying intersection position and the intersected road:\n", .{});
|
std.debug.print("Displaying intersection position and the intersected road:\n", .{});
|
||||||
for (0..intersections.len) |i| {
|
for (0..intersections.len) |i| {
|
||||||
@@ -163,7 +165,6 @@ pub const Simulator = struct {
|
|||||||
std.debug.print("Road ID={d} Pos: ({d}, {d})\n", .{int.road.id, int.pos.x, int.pos.y});
|
std.debug.print("Road ID={d} Pos: ({d}, {d})\n", .{int.road.id, int.pos.x, int.pos.y});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
self.node_man.temp_node = if (self.auto_continue) cur_node else null;
|
self.node_man.temp_node = if (self.auto_continue) cur_node else null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -217,8 +218,32 @@ pub const Simulator = struct {
|
|||||||
var intersections: std.ArrayList(st.IntersectionData) = .empty;
|
var intersections: std.ArrayList(st.IntersectionData) = .empty;
|
||||||
var collision_point: rl.Vector2 = undefined;
|
var collision_point: rl.Vector2 = undefined;
|
||||||
|
|
||||||
|
var start_node_collision: ?*Road = null;
|
||||||
|
var end_node_collision: ?*Road = null;
|
||||||
|
|
||||||
|
// Here we will check if any road collides with start and end node
|
||||||
|
for (self.road_man.roads.items) |road| {
|
||||||
|
if (start_node_collision == null and road.collides(start.pos) and !start.roadsContains(road))
|
||||||
|
start_node_collision = road;
|
||||||
|
if (end_node_collision == null and road.collides(end.pos) and !end.roadsContains(road))
|
||||||
|
end_node_collision = road;
|
||||||
|
|
||||||
|
if (start_node_collision != null and end_node_collision != null) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if road node is placed on the road it is added as a collision with said road
|
||||||
|
if (start_node_collision) |road| {
|
||||||
|
try intersections.append(self.allocator, .{
|
||||||
|
.road = road,
|
||||||
|
.pos = start.pos,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
outer: for (self.road_man.roads.items) |road| {
|
outer: for (self.road_man.roads.items) |road| {
|
||||||
if (!rl.checkCollisionLines(start.pos, end.pos, road.nodes[0].pos, road.nodes[1].pos, &collision_point))
|
if (!rl.checkCollisionLines(
|
||||||
|
start.pos,end.pos,
|
||||||
|
road.nodes[0].pos, road.nodes[1].pos,
|
||||||
|
&collision_point))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const intersection = st.IntersectionData {
|
const intersection = st.IntersectionData {
|
||||||
@@ -226,7 +251,8 @@ pub const Simulator = struct {
|
|||||||
.pos = collision_point,
|
.pos = collision_point,
|
||||||
};
|
};
|
||||||
|
|
||||||
// We put a 0 here, just to satisfy the constructor function
|
// We put a 0 here, just to satisfy the constructor function,
|
||||||
|
// it is not getting appended to the node list anyways
|
||||||
const node: Node = .init(0, intersection.pos);
|
const node: Node = .init(0, intersection.pos);
|
||||||
// If the newly acquired intersection node is within the snapping radius of already existing nodes,
|
// If the newly acquired intersection node is within the snapping radius of already existing nodes,
|
||||||
// we don't add it to the list
|
// we don't add it to the list
|
||||||
@@ -236,22 +262,37 @@ pub const Simulator = struct {
|
|||||||
|
|
||||||
// If there is an existing node that covers our position within its snapping radius,
|
// If there is an existing node that covers our position within its snapping radius,
|
||||||
// then such position will not be saved as intersection
|
// then such position will not be saved as intersection
|
||||||
if (self.node_man.getNodeIfExists(node.pos) != null) continue :outer;
|
if (self.node_man.getNodeIfExists(node.pos) != null) continue;
|
||||||
|
|
||||||
try intersections.append(allocator, intersection);
|
try intersections.append(allocator, intersection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if end node is placed on the road it is added as a collision with said road
|
||||||
|
if (end_node_collision) |road| {
|
||||||
|
try intersections.append(self.allocator, .{
|
||||||
|
.road = road,
|
||||||
|
.pos = end.pos,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const sorted_intersection = try intersections.toOwnedSlice(allocator);
|
const sorted_intersection = try intersections.toOwnedSlice(allocator);
|
||||||
std.sort.block(st.IntersectionData, sorted_intersection, start, ut.compareIntersections);
|
std.sort.block(st.IntersectionData, sorted_intersection, start, ut.compareIntersections);
|
||||||
|
|
||||||
return sorted_intersection;
|
return sorted_intersection;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn splitRoadsByIntersections(self: *Simulator, intersections: st.IntersectionData, start: *Node, end: *Node) void {
|
fn splitRoadsByIntersections(self: *Simulator, intersections: []st.IntersectionData, start: *Node, end: *Node) void {
|
||||||
_ = intersections; // autofix
|
if (intersections.len == 0) {
|
||||||
_ = end; // autofix
|
self.road_man.addRoad(self.allocator, start, end) catch |err| {
|
||||||
_ = start; // autofix
|
std.debug.panic("Failed to create a road connecting the origin nodes: {}\n", .{err});
|
||||||
_ = self; // autofix
|
};
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (0..intersections.len) |i| {
|
||||||
|
_ = i; // autofix
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user