dis_rust/common/linear_velocity_record.rs
1// dis-rust - A rust implementation of the DIS simulation protocol.
2// Copyright (C) 2022 Thomas Mann
3//
4// This software is dual-licensed. It is available under the conditions of
5// the GNU Affero General Public License (see the LICENSE file included)
6// or under a commercial license (email contact@coffeebreakdevs.com for
7// details).
8
9use bytes::{BytesMut, BufMut, Buf};
10
11#[derive(Copy, Clone, Debug, Default)]
12/// Linear Velocity Record as defined in IEEE 1278.1 standard. Used to communicate the velocity of an entity for dead-reckoning during the simulation.
13/// Typically uses metres per second as units
14 pub struct LinearVelocityRecord {
15 pub first_vector_component_field: f32,
16 pub second_vector_component_field: f32,
17 pub third_vector_component_field: f32
18 }
19
20 impl LinearVelocityRecord {
21 /// Provides a function to create a new LinearVelocityRecord.
22 ///
23 /// # Examples
24 ///
25 /// Creating a blank LinearVelocityRecord:
26 ///
27 /// ```
28 /// let linear_velocity_record = LinearVelocityRecord::new{
29 /// x: 0.0,
30 /// y: 0.0,
31 /// z: 0.0
32 /// };
33 /// ```
34 ///
35 pub fn new(x: f32, y: f32, z: f32) -> Self {
36 LinearVelocityRecord {
37 first_vector_component_field: x,
38 second_vector_component_field: y,
39 third_vector_component_field: z
40 }
41 }
42
43 /// Fills a BytesMut struct with a LinearVelocityRecord serialised into binary. This buffer is then ready to be sent via
44 /// UDP to the simluation network.
45 pub fn serialize(&self, buf: &mut BytesMut) {
46 buf.put_f32(self.first_vector_component_field);
47 buf.put_f32(self.second_vector_component_field);
48 buf.put_f32(self.third_vector_component_field);
49 }
50
51 pub fn decode(buf: &mut BytesMut) -> LinearVelocityRecord {
52 LinearVelocityRecord {
53 first_vector_component_field: buf.get_f32(),
54 second_vector_component_field: buf.get_f32(),
55 third_vector_component_field: buf.get_f32()
56 }
57 }
58 }