# path finding – Ponderate dijkstra relative to previous position

I use dijkstra in my rust game to find path to a destination. My game is in a square grid like this:

There is, for illustration, the Rust code which use dijkstra:

``````use crate::map::Map;
use crate::physics::GridPoint;
use pathfinding::prelude::dijkstra;

pub fn find_path(map: &Map, from: &GridPoint, to: &GridPoint) -> Option<Vec<GridPoint>> {
match dijkstra(from, |p| map.successors(p), |p| *p == *to) {
None => None,
Some(path) => Some(path.0),
}
}

// ...

pub fn successors(&self, from: &GridPoint) -> Vec<(GridPoint, i32)> {
let mut successors = vec!();

for (mod_x, mod_y) in (
(-1, -1),
(0, -1),
(1, -1),
(-1, 0),
(0, 0),
(1, 0),
(-1, 1),
(0, 1),
(1, 1),
)
.iter()
{
let new_x = from.x + mod_x;
let new_y = from.y + mod_y;

if new_x < 0 || new_y < 0 {
continue;
}

if let Some(next_tile) = self.terrain.tiles.get(&(new_x as u32, new_y as u32)) {
successors.push((GridPoint::new(new_x, new_y), next_tile.pedestrian_cost))
}
}

successors
}
// ...

``````

And visualization of found path (white is found path, pink is goal):

The found path is not a direct line. I’m not surprised, because the weight of diagonal is the same as lateral. With what I understand with dijkstra is I can return successor with weight for a coordinate. But I can’t increase weight of diagonal/lateral according to previous position (ex: comming from West -> moving to Est will be less weight; maybe it is not the solution …).

How can I achieve that ? With another algorithm than dijkstra ?