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