Try to work with car orientation

This commit is contained in:
hodasemi 2023-01-15 19:58:23 +01:00
parent e225531e6c
commit e991cb7af1

View file

@ -89,12 +89,12 @@ impl RFactorData {
device.clone(), device.clone(),
descriptor_layout, descriptor_layout,
PositionOnlyVertex::from_2d_corners( PositionOnlyVertex::from_2d_corners(
ortho, ortho * Matrix4::from_translation(radar_center.extend(0.0)),
[ [
vec2(radar_center.x - radar_extent, radar_center.y - radar_extent), vec2(-radar_extent, -radar_extent),
vec2(radar_center.x - radar_extent, radar_center.y + radar_extent), vec2(-radar_extent, radar_extent),
vec2(radar_center.x + radar_extent, radar_center.y + radar_extent), vec2(radar_extent, radar_extent),
vec2(radar_center.x + radar_extent, radar_center.y - radar_extent), vec2(radar_extent, -radar_extent),
], ],
), ),
[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5],
@ -103,12 +103,12 @@ impl RFactorData {
device.clone(), device.clone(),
descriptor_layout, descriptor_layout,
PositionOnlyVertex::from_2d_corners( PositionOnlyVertex::from_2d_corners(
ortho, ortho * Matrix4::from_translation(radar_center.extend(0.0)),
[ [
vec2(radar_center.x - car_width, radar_center.y - car_height), vec2(-car_width, -car_height),
vec2(radar_center.x - car_width, radar_center.y + car_height), vec2(-car_width, car_height),
vec2(radar_center.x + car_width, radar_center.y + car_height), vec2(car_width, car_height),
vec2(radar_center.x + car_width, radar_center.y - car_height), vec2(car_width, -car_height),
], ],
), ),
[0.0, 0.9, 0.0, 0.9], [0.0, 0.9, 0.0, 0.9],
@ -140,11 +140,11 @@ impl RFactorData {
self.device.clone(), self.device.clone(),
&self.descriptor_layout, &self.descriptor_layout,
Self::create_car_vertices( Self::create_car_vertices(
self.ortho, self.ortho
self.radar_center, * Matrix4::from_translation(self.radar_center.extend(0.0))
* Matrix4::from_translation(offset.extend(0.0)),
self.car_width, self.car_width,
self.car_height, self.car_height,
offset,
), ),
color, color,
) )
@ -152,30 +152,16 @@ impl RFactorData {
fn create_car_vertices( fn create_car_vertices(
mvp: Matrix4<f32>, mvp: Matrix4<f32>,
radar_center: Vector2<f32>,
car_width: f32, car_width: f32,
car_height: f32, car_height: f32,
offset: Vector2<f32>,
) -> [PositionOnlyVertex; 6] { ) -> [PositionOnlyVertex; 6] {
PositionOnlyVertex::from_2d_corners( PositionOnlyVertex::from_2d_corners(
mvp, mvp,
[ [
vec2( vec2(-car_width, -car_height),
radar_center.x - car_width + offset.x, vec2(-car_width, car_height),
radar_center.y - car_height + offset.y, vec2(car_width, car_height),
), vec2(car_width, -car_height),
vec2(
radar_center.x - car_width + offset.x,
radar_center.y + car_height + offset.y,
),
vec2(
radar_center.x + car_width + offset.x,
radar_center.y + car_height + offset.y,
),
vec2(
radar_center.x + car_width + offset.x,
radar_center.y - car_height + offset.y,
),
], ],
) )
} }
@ -230,22 +216,19 @@ impl RFactorData {
let mut other_positions = Vec::new(); let mut other_positions = Vec::new();
for telemetry in telemetries { for telemetry in telemetries {
if telemetry.id == *player_id { let car = CarPosition::new(
player_position.position = convert_vec(telemetry.position); convert_vec(telemetry.position),
player_position.orientation = [ [
convert_vec(telemetry.orientation[0]),
convert_vec(telemetry.orientation[1]),
convert_vec(telemetry.orientation[2]),
];
} else {
other_positions.push(CarPosition {
position: convert_vec(telemetry.position),
orientation: [
convert_vec(telemetry.orientation[0]), convert_vec(telemetry.orientation[0]),
convert_vec(telemetry.orientation[1]), convert_vec(telemetry.orientation[1]),
convert_vec(telemetry.orientation[2]), convert_vec(telemetry.orientation[2]),
], ],
}); );
if telemetry.id == *player_id {
player_position = car
} else {
other_positions.push(car);
} }
} }
@ -257,7 +240,7 @@ impl RFactorData {
let diff = player_position.position - other_position.position; let diff = player_position.position - other_position.position;
let distance = diff.magnitude(); let distance = diff.magnitude();
// check if car is close enough the players car // check if car is close enough to the players car
if distance < self.config.radar_car_distance { if distance < self.config.radar_car_distance {
let offset = let offset =
diff.xz() * (self.radar_extent / self.config.radar_car_distance); diff.xz() * (self.radar_extent / self.config.radar_car_distance);
@ -267,7 +250,7 @@ impl RFactorData {
buffered_car.update( buffered_car.update(
self.ortho, self.ortho,
offset, offset,
Self::car_orientation(&other_position), other_position.rotation - player_position.rotation,
self.radar_center, self.radar_center,
self.car_width, self.car_width,
self.car_height, self.car_height,
@ -304,14 +287,6 @@ impl RFactorData {
objects objects
} }
fn car_orientation(car: &CarPosition) -> Rad<f32> {
const DEGREES_IN_RADIAN: f32 = 57.2957795;
let xz_val = car.orientation[2].xz();
Rad(xz_val.x.atan2(xz_val.y) * DEGREES_IN_RADIAN)
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -370,11 +345,12 @@ impl RadarObject {
) -> Result<()> { ) -> Result<()> {
self.position_buffer self.position_buffer
.fill(&RFactorData::create_car_vertices( .fill(&RFactorData::create_car_vertices(
ortho * Matrix4::from_angle_z(rotation.into()), ortho
radar_center, * Matrix4::from_translation(radar_center.extend(0.0))
* Matrix4::from_translation(offset.extend(0.0))
* Matrix4::from_angle_z(rotation.into()),
car_width, car_width,
car_height, car_height,
offset,
))?; ))?;
self.color_buffer.fill(&color) self.color_buffer.fill(&color)
} }
@ -393,6 +369,22 @@ impl RenderObject for RadarObject {
struct CarPosition { struct CarPosition {
pub position: Vector3<f32>, pub position: Vector3<f32>,
pub orientation: [Vector3<f32>; 3], pub orientation: [Vector3<f32>; 3],
pub rotation: Rad<f32>,
}
impl CarPosition {
fn new(position: Vector3<f32>, orientation: [Vector3<f32>; 3]) -> Self {
Self {
position,
rotation: {
const DEGREES_IN_RADIAN: f32 = 57.2957795;
let xz_val = orientation[2].xz();
Rad(xz_val.x.atan2(xz_val.y) * DEGREES_IN_RADIAN)
},
orientation,
}
}
} }
impl Default for CarPosition { impl Default for CarPosition {
@ -400,6 +392,7 @@ impl Default for CarPosition {
Self { Self {
position: vec3(0.0, 0.0, 0.0), position: vec3(0.0, 0.0, 0.0),
orientation: [vec3(0.0, 0.0, 0.0); 3], orientation: [vec3(0.0, 0.0, 0.0); 3],
rotation: Rad(0.0),
} }
} }
} }