switched over to glam and simplified some imports

This commit is contained in:
Luuk van Oijen
2023-11-30 15:35:22 +01:00
parent dc61d4649c
commit 8e1d235dee
6 changed files with 33 additions and 158 deletions

View File

@@ -1,15 +1,15 @@
use nalgebra::*;
use glam::*;
use std::time::{Instant, Duration};
use std::time::Instant;
#[derive(Default, Clone, Debug)]
pub struct Car {
pub car_json: String,
pub pos: Vector3<f64>,
pub rot: Quaternion<f64>,
pub vel: Vector3<f64>,
pub rvel: Vector3<f64>,
pub pos: DVec3,
pub rot: DQuat,
pub vel: DVec3,
pub rvel: DVec3,
pub tim: f64,
pub ping: f64,
pub last_pos_update: Option<Instant>,
@@ -24,12 +24,12 @@ impl Car {
}
}
pub fn pos(&self) -> Vector3<f64> {
pub fn position(&self) -> DVec3 {
self.pos + self.vel * self.last_pos_update.map(|t| t.elapsed().as_secs_f64()).unwrap_or(0.0)
}
pub fn rotation(&self) -> Quaternion<f64> {
pub fn rotation(&self) -> DQuat {
let t = self.last_pos_update.map(|t| t.elapsed().as_secs_f64()).unwrap_or(0.0);
self.rot + UnitQuaternion::from_euler_angles(self.rvel.x * t, self.rvel.y * t, self.rvel.z * t).quaternion()
self.rot + DQuat::from_euler(glam::EulerRot::YXZ, self.rvel.x * t, self.rvel.y * t, self.rvel.z * t)
}
}

View File

@@ -2,9 +2,7 @@ use std::collections::HashMap;
use std::net::SocketAddr;
use std::ops::DerefMut;
use std::path::Path;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use std::time::Instant;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{
@@ -15,10 +13,7 @@ use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use nalgebra::*;
use serde::Deserialize;
use serde_aux::prelude::*;
use crate::fs_util;
use super::backend::*;

View File

@@ -1,16 +1,14 @@
use std::net::SocketAddr;
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
use std::sync::Arc;
use std::time::Instant;
use std::collections::HashMap;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, UdpSocket};
use tokio::task::{JoinHandle, JoinSet};
use tokio::sync::{Mutex, mpsc, oneshot};
use tokio::sync::{mpsc, oneshot};
use num_enum::IntoPrimitive;
use nalgebra::*;
use glam::*;
mod backend;
mod car;
@@ -27,7 +25,6 @@ pub use plugins::*;
pub use http::*;
pub use crate::config::Config;
use crate::config::GeneralSettings;
fn load_plugins(server_resource_folder: String) -> Vec<Plugin> {
let mut plugins = Vec::new();
@@ -686,11 +683,11 @@ impl Server {
ServerBoundPluginEvent::RequestPositionRaw((pid, vid, responder)) => {
if let Some(client) = self.clients.iter().find(|client| client.id == pid) {
if let Some((_id, vehicle)) = client.cars.iter().find(|(id, _car)| *id == vid) {
// let pos_rot = PositionRaw {
// pos: vehicle.pos().
// };
todo!()
// let _ = responder.send(PluginBoundPluginEvent::PositionRaw(pos_rot));
let pos_rot = PositionRaw {
pos: vehicle.position().as_vec3().to_array(),
rot: vehicle.rotation().as_f32().to_array(),
};
let _ = responder.send(PluginBoundPluginEvent::PositionRaw(pos_rot));
} else {
let _ = responder.send(PluginBoundPluginEvent::None);
}
@@ -972,11 +969,11 @@ impl Server {
.get_car_mut(car_id)
.ok_or(ServerError::CarDoesntExist)?;
car.pos = pos_data.pos.into();
car.rot = Quaternion::new(
pos_data.rot[3],
car.rot = DQuat::from_xyzw(
pos_data.rot[0],
pos_data.rot[1],
pos_data.rot[2],
pos_data.rot[3],
);
car.vel = pos_data.vel.into();
car.rvel = pos_data.rvel.into();

View File

@@ -174,10 +174,13 @@ impl UserData for Context {
let message = rx.blocking_recv();
if let Ok(message) = message {
if let PluginBoundPluginEvent::PositionRaw(pos_raw) = message {
let pos_table = lua.create_table()?;
pos_table.set("x", pos_raw.pos[0])?;
pos_table.set("y", pos_raw.pos[1])?;
pos_table.set("z", pos_raw.pos[2])?;
let table = lua.create_table()?;
// for (vid, veh_json) in vehicles {
// table.set(vid, veh_json)?;
// }
table.set("pos", pos_table)?;
Ok(table)
} else {
unreachable!() // This really should never be reachable