fix out of bounds write

This commit is contained in:
Felix Ableitner 2021-10-07 01:29:33 +02:00
parent 813d03adbb
commit 667c34fe92

View file

@ -6,6 +6,7 @@ use macroquad::prelude::*;
use macroquad::rand::RandomRange; use macroquad::rand::RandomRange;
use std::f32::consts::PI; use std::f32::consts::PI;
#[derive(Debug)]
pub(crate) struct Ant { pub(crate) struct Ant {
pos: Vec2, pos: Vec2,
dir: f32, dir: f32,
@ -31,9 +32,8 @@ impl Ant {
let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos());
self.pos += acceleration * ANT_SPEED * frame_time; self.pos += acceleration * ANT_SPEED * frame_time;
// TODO: this is not fixing out of bounds write self.pos.x = self.pos.x.clamp(0., (MAP_WIDTH - 1) as f32);
self.pos.x = self.pos.x.clamp(0., MAP_WIDTH as f32); self.pos.y = self.pos.y.clamp(0., (MAP_HEIGHT - 1) as f32);
self.pos.y = self.pos.y.clamp(0., MAP_HEIGHT as f32);
// TODO: change color over time // TODO: change color over time
trail_map.set_pixel(self.pos.x as u32, self.pos.y as u32, RED); trail_map.set_pixel(self.pos.x as u32, self.pos.y as u32, RED);