Skip to content

Commit

Permalink
Slightly better way to get car world location #14
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Nov 21, 2016
1 parent 0b14d95 commit a5de452
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Expand Up @@ -18,4 +18,7 @@ path = "./lib/kay_macros/"
path = "./lib/descartes/"

[dependencies.monet]
path = "./lib/monet/"
path = "./lib/monet/"

[profile.release]
debug = true
25 changes: 16 additions & 9 deletions src/game/lanes_and_cars/lane_rendering.rs
@@ -1,8 +1,9 @@
use descartes::{Band, FiniteCurve, WithUniqueOrthogonal, Norm};
use descartes::{Band, FiniteCurve, WithUniqueOrthogonal, Norm, Path};
use kay::{Actor, CVec, Individual, Recipient, RecipientAsSwarm, ActorSystem, Swarm, Fate};
use monet::{Instance, Thing, Vertex};
use core::geometry::band_to_thing;
use super::{Lane, TransferLane, InteractionKind};
use itertools::Itertools;

#[path = "./resources/car.rs"]
mod car;
Expand Down Expand Up @@ -78,15 +79,21 @@ use ::monet::UpdateThing;
impl Recipient<RenderToScene> for Lane {
fn receive(&mut self, msg: &RenderToScene) -> Fate {match *msg {
RenderToScene{renderer_id, scene_id} => {
let car_instances = self.cars.iter().map(|car| {
let position2d = self.path.along(*car.position);
let direction = self.path.direction_along(*car.position);
Instance{
instance_position: [position2d.x, position2d.y, 0.0],
instance_direction: [direction.x, direction.y],
instance_color: [0.5, 0.5, 0.5]
let mut cars_iter = self.cars.iter();
let mut current_offset = 0.0;
let mut car_instances = CVec::with_capacity(self.cars.len());
for segment in self.path.segments().iter() {
for car in cars_iter.take_while_ref(|car| *car.position - current_offset < segment.length()) {
let position2d = segment.along(*car.position - current_offset);
let direction = segment.direction_along(*car.position - current_offset);
car_instances.push(Instance{
instance_position: [position2d.x, position2d.y, 0.0],
instance_direction: [direction.x, direction.y],
instance_color: [0.5, 0.5, 0.5]
})
}
}).collect::<CVec<_>>();
current_offset += segment.length;
}

renderer_id << AddSeveralInstances{
scene_id: scene_id,
Expand Down
20 changes: 11 additions & 9 deletions src/game/lanes_and_cars/planning/road_stroke_canvas.rs
Expand Up @@ -54,15 +54,17 @@ impl Recipient<Event3d> for RoadStrokeCanvas {
Fate::Live
},
Event3d::KeyDown(VirtualKeyCode::C) => {
Swarm::<Lane>::all() << ::game::lanes_and_cars::Add::Car(LaneCar{
trip: ID::invalid(),
as_obstacle: Obstacle{
position: OrderedFloat(0.0),
velocity: 0.0,
max_velocity: 20.0
},
acceleration: 0.0
});
for _i in 0..100 {
Swarm::<Lane>::all() << ::game::lanes_and_cars::Add::Car(LaneCar{
trip: ID::invalid(),
as_obstacle: Obstacle{
position: OrderedFloat(0.0),
velocity: 0.0,
max_velocity: 20.0
},
acceleration: 0.0
});
}
Fate::Live
},
Event3d::KeyDown(VirtualKeyCode::G) => {
Expand Down

0 comments on commit a5de452

Please sign in to comment.