onyx::math

Struct Vec2

Source
pub struct Vec2<R: Real = f32> {
    pub x: R,
    pub y: R,
}

Fields§

§x: R§y: R

Implementations§

Source§

impl<R: Real> Vec2<R>

Source

pub fn new(x: R, y: R) -> Self

Examples found in repository?
examples/example-pong.rs (line 48)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    fn new(state: &mut onyx::State<Action>, center: Vec2, size: Vec2, velocity: Vec2) -> Rect {
        let transform = state.graph().add(center, Vec2::new(1.0, 1.0));
        let child = state.graph().add_child(transform, -0.5 * size, size);
        state.renderer().add(child);
        Self { center, size, velocity, transform }
    }

    fn min(&self) -> Vec2 {
        self.center - 0.5 * self.size
    }

    fn max(&self) -> Vec2 {
        self.center + 0.5 * self.size
    }
}

impl onyx::Scene for Scene {
    type Action = Action;

    fn new(state: &mut onyx::State<Action>) -> Self {
        let size = state.context().size();
        let plank_size = Vec2::new(10.0, 100.0);
        let left_plank = Rect::new(state, 0.5 * Vec2::new(plank_size.x, size.y), plank_size, Vec2::zero());
        let right_plank = Rect::new(state, Vec2::new(size.x - 0.5 * plank_size.x, 0.5 * size.y), plank_size, Vec2::zero());
        let ball_size = Vec2::new(20.0, 20.0);
        let ball = Rect::new(state, 0.5 * size, ball_size, Vec2::new(100.0, 100.0));
        state.ui().build(onyx::ui::panel()
            .layout(onyx::ui::Layout::Horizontal)
            .alignment(onyx::ui::Alignment::Center, onyx::ui::Alignment::Min)
            .child(onyx::ui::label("0").id("left"))
            .child(onyx::ui::label(":"))
            .child(onyx::ui::label("0").id("right"))
        );
        let mut keys = HashMap::new();
        keys.insert(onyx::Key::W, (Direction::Up, Plank::Left));
        keys.insert(onyx::Key::S, (Direction::Down, Plank::Left));
        keys.insert(onyx::Key::Up, (Direction::Up, Plank::Right));
        keys.insert(onyx::Key::Down, (Direction::Down, Plank::Right));
        Scene { 
            left_plank,
            right_plank,
            ball,
            left_score: 0,
            right_score: 0,
            keys,
        }
    }

    fn event(&mut self, event: onyx::Event) -> Option<Action> {
        match event {
            onyx::Event::KeyPressed(key) => {
                if let Some(&(direction, plank)) = self.keys.get(&key) {
                    Some(Action { direction, plank, kind: Kind::Start })
                } else {
                    None
                }
            },
            onyx::Event::KeyReleased(key) => {
                if let Some(&(direction, plank)) = self.keys.get(&key) {
                    Some(Action { direction, plank, kind: Kind::Stop })
                } else {
                    None
                }
            },
            _ => None,
        }
    }

    fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
        let plank = match action.plank {
            Plank::Left => &mut self.left_plank,
            Plank::Right => &mut self.right_plank,
        };
        let velocity = match action.direction {
            Direction::Down => Vec2::new(0.0, 100.0),
            Direction::Up => Vec2::new(0.0, -100.0),
        };
        match action.kind {
            Kind::Start => plank.velocity += velocity,
            Kind::Stop => plank.velocity -= velocity,
        }
        state.none()
    }
Source§

impl<R: Real> Vec2<R>

Source

pub fn zero() -> Self

Examples found in repository?
examples/example-pong.rs (line 69)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    fn new(state: &mut onyx::State<Action>) -> Self {
        let size = state.context().size();
        let plank_size = Vec2::new(10.0, 100.0);
        let left_plank = Rect::new(state, 0.5 * Vec2::new(plank_size.x, size.y), plank_size, Vec2::zero());
        let right_plank = Rect::new(state, Vec2::new(size.x - 0.5 * plank_size.x, 0.5 * size.y), plank_size, Vec2::zero());
        let ball_size = Vec2::new(20.0, 20.0);
        let ball = Rect::new(state, 0.5 * size, ball_size, Vec2::new(100.0, 100.0));
        state.ui().build(onyx::ui::panel()
            .layout(onyx::ui::Layout::Horizontal)
            .alignment(onyx::ui::Alignment::Center, onyx::ui::Alignment::Min)
            .child(onyx::ui::label("0").id("left"))
            .child(onyx::ui::label(":"))
            .child(onyx::ui::label("0").id("right"))
        );
        let mut keys = HashMap::new();
        keys.insert(onyx::Key::W, (Direction::Up, Plank::Left));
        keys.insert(onyx::Key::S, (Direction::Down, Plank::Left));
        keys.insert(onyx::Key::Up, (Direction::Up, Plank::Right));
        keys.insert(onyx::Key::Down, (Direction::Down, Plank::Right));
        Scene { 
            left_plank,
            right_plank,
            ball,
            left_score: 0,
            right_score: 0,
            keys,
        }
    }

    fn event(&mut self, event: onyx::Event) -> Option<Action> {
        match event {
            onyx::Event::KeyPressed(key) => {
                if let Some(&(direction, plank)) = self.keys.get(&key) {
                    Some(Action { direction, plank, kind: Kind::Start })
                } else {
                    None
                }
            },
            onyx::Event::KeyReleased(key) => {
                if let Some(&(direction, plank)) = self.keys.get(&key) {
                    Some(Action { direction, plank, kind: Kind::Stop })
                } else {
                    None
                }
            },
            _ => None,
        }
    }

    fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
        let plank = match action.plank {
            Plank::Left => &mut self.left_plank,
            Plank::Right => &mut self.right_plank,
        };
        let velocity = match action.direction {
            Direction::Down => Vec2::new(0.0, 100.0),
            Direction::Up => Vec2::new(0.0, -100.0),
        };
        match action.kind {
            Kind::Start => plank.velocity += velocity,
            Kind::Stop => plank.velocity -= velocity,
        }
        state.none()
    }

    fn update(&mut self, state: &mut onyx::State<Action>) -> onyx::Transition {
        let min = Vec2::zero();
        let max = state.context().size();
        self.ball.center += self.ball.velocity * state.context().dt();
        self.left_plank.center += self.left_plank.velocity * state.context().dt();
        self.right_plank.center += self.right_plank.velocity * state.context().dt();
        if self.ball.max().x > max.x - self.right_plank.size.x && self.ball.max().x < max.x {
            if self.ball.min().y < self.right_plank.max().y && self.ball.max().y > self.right_plank.min().y {
                self.ball.center.x = max.x - self.right_plank.size.x - 0.5 * self.ball.size.x;
                self.ball.velocity.x = -self.ball.velocity.x;
            }
        }
        if self.ball.min().x > max.x {
            self.ball.center.x = 0.5 * (max.x - min.x);
            self.left_score += 1;
            state.ui().set_text("left", &self.left_score.to_string());
        }
        if self.ball.min().x < min.x + self.left_plank.size.x && self.ball.max().x > min.x {
            if self.ball.min().y < self.left_plank.max().y && self.ball.max().y > self.left_plank.min().y {
                self.ball.center.x = min.x + self.left_plank.size.x + 0.5 * self.ball.size.x;
                self.ball.velocity.x = -self.ball.velocity.x;
            }
        }
        if self.ball.max().x < min.x {
            self.ball.center.x = 0.5 * (max.x - min.x);
            self.right_score += 1;
            state.ui().set_text("right", &self.right_score.to_string());
        }
        if self.ball.min().y < min.y {
            self.ball.center.y = min.y + 0.5 * self.ball.size.y;
            self.ball.velocity.y = -self.ball.velocity.y;
        }
        if self.ball.max().y > max.y {
            self.ball.center.y = max.y - 0.5 * self.ball.size.y;
            self.ball.velocity.y = -self.ball.velocity.y;
        }
        state.graph().set_position(self.ball.transform, self.ball.center);
        state.graph().set_position(self.left_plank.transform, self.left_plank.center);
        state.graph().set_position(self.right_plank.transform, self.right_plank.center);
        state.none()
    }

Trait Implementations§

Source§

impl<R: Real> Add for Vec2<R>

Source§

type Output = Vec2<R>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<R: Real> AddAssign for Vec2<R>

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<R: Clone + Real> Clone for Vec2<R>

Source§

fn clone(&self) -> Vec2<R>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Debug + Real> Debug for Vec2<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, R> Deserialize<'de> for Vec2<R>
where R: Deserialize<'de> + Real,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<R: Real> From<Vec2<R>> for Vec3<R>

Source§

fn from(vec2: Vec2<R>) -> Self

Converts to this type from the input type.
Source§

impl<R: Real> Into<[R; 2]> for Vec2<R>

Source§

fn into(self) -> [R; 2]

Converts this type into the (usually inferred) input type.
Source§

impl<R: Real> Mul<R> for Vec2<R>

Source§

type Output = Vec2<R>

The resulting type after applying the * operator.
Source§

fn mul(self, other: R) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec2<f64>> for f64

Source§

type Output = Vec2<f64>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Vec2<f64>) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec2> for f32

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, other: Vec2<f32>) -> Self::Output

Performs the * operation. Read more
Source§

impl<R: PartialEq + Real> PartialEq for Vec2<R>

Source§

fn eq(&self, other: &Vec2<R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<R> Serialize for Vec2<R>
where R: Serialize + Real,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<R: Real> Sub for Vec2<R>

Source§

type Output = Vec2<R>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<R: Real> SubAssign for Vec2<R>

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<R: Copy + Real> Copy for Vec2<R>

Source§

impl<R: Real> StructuralPartialEq for Vec2<R>

Auto Trait Implementations§

§

impl<R> Freeze for Vec2<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Vec2<R>
where R: RefUnwindSafe,

§

impl<R> Send for Vec2<R>
where R: Send,

§

impl<R> Sync for Vec2<R>
where R: Sync,

§

impl<R> Unpin for Vec2<R>
where R: Unpin,

§

impl<R> UnwindSafe for Vec2<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> Content for T
where T: Copy,

Source§

type Owned = T

A type that holds a sized version of the content.
Source§

fn read<F, E>(size: usize, f: F) -> Result<T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

Prepares an output buffer, then turns this buffer into an Owned.
Source§

fn get_elements_size() -> usize

Returns the size of each element.
Source§

fn to_void_ptr(&self) -> *const ()

Produces a pointer to the data.
Source§

fn ref_from_ptr<'a>(ptr: *mut (), size: usize) -> Option<*mut T>

Builds a pointer to this type from a raw pointer.
Source§

fn is_size_suitable(size: usize) -> bool

Returns true if the size is suitable to store a type like this.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,