0

I have some elements with a 2D position. Since most of them are (almost) round, to simplify I use the distance to check if two of them have collided.

While I can call a method from a "parent" trait, I cannot use it as a parameter where a parent trait is required.

trait Point {
    fn x(&self) -> f64;
    fn y(&self) -> f64;
    fn distance(&self, other: &Point) -> f64 {
        ((other.x() - self.x()).powi(2) + (other.y() - self.y()).powi(2)).sqrt()
    }
}

trait Round: Point {
    fn radius(&self) -> f64;

    fn collision(&self, other: &Round) -> bool {
        let distance: f64 = self.distance(other);
        distance < self.radius() + other.radius()
    }
}
error[E0308]: mismatched types
  --> src/main.rs:13:43
   |
13 |         let distance: f64 = self.distance(other);
   |                                           ^^^^^ expected trait `Point`, found trait `Round`
   |
   = note: expected type `&Point`
              found type `&Round`

What is the correct way to achieve this?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
thamurath
  • 765
  • 8
  • 24
  • Thanks to point me to the correct answer ... @Shepmaster. I have search for a while but did not use the correct terms ... – thamurath Jul 31 '17 at 20:40
  • 2
    No worries; that's why Stack Overflow has duplicates. This question will remain as a signpost for everyone else who searches using the same terms you did. – Shepmaster Jul 31 '17 at 20:41

0 Answers0