1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use bevy::prelude::*;
pub trait VecOp<T> {
/// Projects the vector on the given normal
fn project(
self,
normal: T,
) -> T;
/// Slides the vector on the given normal
fn slide(
self,
normal: T,
) -> T;
}
impl VecOp<Vec2> for Vec2 {
fn project(
self,
n: Vec2,
) -> Vec2 {
if n.is_normalized() {
self.dot(n) * n
}
else {
self // Just return the given a vector if n is not normalized
}
}
fn slide(
self,
n: Vec2,
) -> Vec2 {
if n.is_normalized() {
self - self.project(n)
}
else {
self
}
}
}