Skip to content

Commit

Permalink
relax roundabout a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Aug 13, 2023
1 parent 8060956 commit 15baefe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
28 changes: 18 additions & 10 deletions egregoria/src/map/objects/turn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ impl Turn {
let ang_b = b.angle_cossin();

if ang_a > ang_b {
ang_a -= TAU;
ang_a -= Radians::TAU;
}

let mut turn_radius = rp.radius * (1.0 - 0.5 * (ang_b - ang_a) / TAU);
let mut turn_radius = rp.radius * (1.0 - 0.5 * (ang_b.0 - ang_a.0) / TAU);

if matches!(self.kind, TurnKind::WalkingCorner) {
turn_radius = rp.radius + 3.0;
Expand Down Expand Up @@ -173,12 +173,20 @@ impl Turn {
let b = (pos_dst.xy() - center)
.normalize()
.rotated_by_angle(Radians::from_deg(-20.0));
let ang_b = b.angle_cossin();
let mut ang_b = b.angle_cossin();

if ang_a > ang_b {
ang_a -= TAU;
ang_a -= Radians::TAU;
}

let diff = (ang_b - ang_a).min(Radians::from_deg(50.0));

ang_a += diff * 0.4;
ang_b -= diff * 0.4;

let a = ang_a.vec2();
let b = ang_b.vec2();

let sp1 = Self::spline(
pos_src.xy(),
center + a * radius,
Expand All @@ -201,19 +209,19 @@ impl Turn {
/// Return points of a circular arc in counter-clockwise order from ang_a to ang_b, assuming ang_a < ang_b
pub fn circular_arc(
center: Vec2,
ang_a: f32,
ang_b: f32,
ang_a: Radians,
ang_b: Radians,
radius: f32,
) -> impl Iterator<Item = Vec2> {
const PRECISION: f32 = 1.0 / 0.1; // denominator is angular step in radians

let ang = ang_b - ang_a;
let n = (ang.abs() * PRECISION).ceil() as usize;
let ang_step = ang / n as f32;
let n = (ang.0.abs() * PRECISION).ceil() as usize;
let ang_step = Radians(ang.0 / n as f32);

(0..=n).map(move |i| {
let ang = ang_a + ang_step * i as f32;
center + Vec2::new(ang.cos(), ang.sin()) * radius
let ang = ang_a + Radians(ang_step.0 * i as f32);
center + ang.vec2() * radius
})
}

Expand Down
6 changes: 4 additions & 2 deletions geom/src/angle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Vec2;
use serde::{Deserialize, Serialize};
use std::f32::consts::PI;
use std::f32::consts::{PI, TAU};
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};

#[derive(Serialize, Deserialize, Copy, Clone, PartialOrd, PartialEq, Default)]
Expand All @@ -15,6 +15,8 @@ pub struct Radians(pub f32);

impl Radians {
pub const HALFPI: Self = Radians(std::f32::consts::FRAC_PI_2);
pub const PI: Self = Radians(PI);
pub const TAU: Self = Radians(TAU);

pub fn vec2(self) -> Vec2 {
Vec2 {
Expand All @@ -28,7 +30,7 @@ impl Radians {
}

pub fn normalize(&mut self) {
self.0 %= std::f32::consts::TAU;
self.0 %= TAU;
}

pub fn cos(self) -> f32 {
Expand Down
4 changes: 2 additions & 2 deletions geom/src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ impl Vec2 {
/// Returns the angle in range [-pi; pi] such that if v is unitary
/// v == (angle.cos(), angle.sin())
#[inline]
pub fn angle_cossin(self) -> f32 {
f32::atan2(self.y, self.x)
pub fn angle_cossin(self) -> Radians {
Radians(f32::atan2(self.y, self.x))
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion native_app/src/gui/roadeditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn roadeditor(goria: &Egregoria, uiworld: &mut UiWorld) {
}
}

imm_draw.circle(proj_pos, 10.0).color(proj_col);
imm_draw.circle(proj_pos.up(0.1), 10.0).color(proj_col);

if state.dirty {
if let Some(interc) = &state.inspect {
Expand Down
5 changes: 4 additions & 1 deletion native_app/src/rendering/map_rendering/map_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,10 @@ fn intersection_mesh(
if let Some(rp) = inter.turn_policy.roundabout {
let center = inter.pos.xy();

let ang = (-src_orient).angle(dst_orient).abs();
let ang = (left - center)
.normalize()
.angle((next_right - center).normalize())
.abs();
if ang >= Radians::from_deg(41.0).0 {
polygon.extend(Turn::gen_roundabout(
left.z(0.0),
Expand Down

0 comments on commit 15baefe

Please sign in to comment.