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?