Skip to content

Commit

Permalink
finally realized that bevy uses right handed coords
Browse files Browse the repository at this point in the history
  • Loading branch information
Amatsugu committed Aug 7, 2024
1 parent 911cf8d commit a8409e5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 25 deletions.
1 change: 0 additions & 1 deletion engine/world_generation/src/heightmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use core::f32;

use bevy::math::{IVec2, UVec2};
use bevy::prelude::{FloatExt, Vec2};
use bevy::render::render_resource::encase::internal::BufferMut;
use bevy::utils::default;
use noise::{NoiseFn, SuperSimplex};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
Expand Down
8 changes: 4 additions & 4 deletions engine/world_generation/src/hex_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ impl Display for HexCoord {

impl HexCoord {
pub const DIRECTIONS: [IVec3; 6] = [
IVec3::new(1, -1, 0),
IVec3::new(1, 0, -1),
IVec3::new(0, 1, -1),
IVec3::new(-1, 1, 0),
IVec3::new(-1, 0, 1),
IVec3::new(1, 0, -1),
IVec3::new(1, -1, 0),
IVec3::new(0, -1, 1),
IVec3::new(-1, 0, 1),
IVec3::new(-1, 1, 0),
];

pub const ZERO: HexCoord = HexCoord { hex: IVec3::ZERO };
Expand Down
2 changes: 1 addition & 1 deletion engine/world_generation/src/map/mesh_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl MeshChunkData {
pub fn get_neighbors(&self, coord: &HexCoord) -> [f32; 6] {
let mut data = [0.; 6];
let n_tiles = coord.get_neighbors();
for i in 6..0 {
for i in 0..6 {
let n = n_tiles[i];
if !n.is_in_bounds(Chunk::SIZE, Chunk::SIZE) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions game/buildings/src/building_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ fn hq_placement(
if let Some((_e, dist)) = collision {
let contact_point = cam_ray.get_point(dist);
let contact_coord = HexCoord::from_world_pos(contact_point);
if !map.is_in_bounds(&contact_coord) {
return;
}
let positions = map.hex_select(&contact_coord, 3, true, |pos, h, _| pos.to_world(h));
show_indicators(positions, &mut commands, &indicator);

Expand Down
19 changes: 11 additions & 8 deletions game/main/src/camera_system/camera_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn setup(mut commands: Commands, mut msaa: ResMut<Msaa>) {
commands
.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0., 30., 0.).looking_to(Vec3::Z, Vec3::Y),
transform: Transform::from_xyz(0., 30., 0.).looking_to(Vec3::NEG_Z, Vec3::Y),
..default()
},
PhosCamera::default(),
Expand Down Expand Up @@ -138,15 +138,15 @@ fn rts_camera_system(
let mut cam_pos = cam.translation;

if key.pressed(KeyCode::KeyA) {
cam_move.x = 1.;
} else if key.pressed(KeyCode::KeyD) {
cam_move.x = -1.;
} else if key.pressed(KeyCode::KeyD) {
cam_move.x = 1.;
}

if key.pressed(KeyCode::KeyW) {
cam_move.z = 1.;
} else if key.pressed(KeyCode::KeyS) {
cam_move.z = -1.;
} else if key.pressed(KeyCode::KeyS) {
cam_move.z = 1.;
}

let move_speed = if key.pressed(KeyCode::ShiftLeft) {
Expand All @@ -156,7 +156,7 @@ fn rts_camera_system(
};

cam_move = cam_move.normalize_or_zero() * move_speed * time.delta_seconds();
cam_pos -= cam_move;
cam_pos += cam_move;

let mut scroll = 0.0;
for e in wheel.read() {
Expand Down Expand Up @@ -209,8 +209,11 @@ fn rts_camera_system(
cam_targets.rotate_time = cam_targets.rotate_time.min(1.);
}
let angle = cam_cfg.min_angle.lerp(cam_cfg.max_angle, t);
let rot = Quat::from_axis_angle(Vec3::X, -angle);
cam.rotation = rot;
let mut rot = cam.rotation.to_euler(EulerRot::XYZ);
rot.0 = -angle;
cam.rotation = Quat::from_euler(EulerRot::XYZ, rot.0, rot.1, rot.2);
// let rot = Quat::from_axis_angle(Vec3::X, -angle);
// cam.rotation = rot;

cam.translation = cam_pos;
}
Expand Down
2 changes: 1 addition & 1 deletion game/main/src/camera_system/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Default for PhosCameraTargets {
fn default() -> Self {
Self {
height: Default::default(),
forward: Vec3::Z,
forward: Vec3::NEG_Z,
last_height: Default::default(),
anim_time: Default::default(),
rotate_time: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion game/main/src/map_rendering/map_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Plugin for MapInitPlugin {
app.register_asset_reflect::<ExtendedMaterial<StandardMaterial, WaterMaterial>>();
app.add_plugins((
ChunkRebuildPlugin,
TerraFormingTestPlugin,
// TerraFormingTestPlugin,
MaterialPlugin::<ExtendedMaterial<StandardMaterial, ChunkMaterial>>::default(),
MaterialPlugin::<ExtendedMaterial<StandardMaterial, WaterMaterial>> {
prepass_enabled: false,
Expand Down
3 changes: 3 additions & 0 deletions game/main/src/map_rendering/terraforming_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ fn deform(
let span = info_span!("Deform Mesh").entered();
let contact_point = cam_ray.get_point(dist);
let contact_coord = HexCoord::from_world_pos(contact_point);
if !heightmap.is_in_bounds(&contact_coord) {
return;
}
let modified_tiles = heightmap.create_crater(&contact_coord, 5, 5. * multi);
let mut chunk_set: HashSet<usize> = HashSet::new();
for (tile, height) in modified_tiles {
Expand Down
27 changes: 18 additions & 9 deletions game/main/src/utlis/debug_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,34 @@ fn show_tile_heights(
if let Some((_e, dist)) = collision {
let contact_point = cam_ray.get_point(dist);
let contact_coord = HexCoord::from_world_pos(contact_point);
if map.is_in_bounds(&contact_coord) {
let height = map.sample_height(&contact_coord);
gizmos.primitive_3d(
&shape.0,
contact_coord.to_world(height + 0.01),
Quat::IDENTITY,
Color::WHITE,
);
if !map.is_in_bounds(&contact_coord) {
return;
}
let height = map.sample_height(&contact_coord);
gizmos.primitive_3d(
&shape.0,
contact_coord.to_world(height + 0.01),
Quat::IDENTITY,
Color::WHITE,
);
let nbors = map.get_neighbors(&contact_coord);
let contact_tile_pos = contact_coord.to_world(map.sample_height(&contact_coord));

for i in 0..6 {
if let Some(s) = nbors[i] {
let coord = contact_coord.get_neighbor(i);
let p = coord.to_world(s);
gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), Color::WHITE);
}

let p = HEX_CORNERS[i] + contact_tile_pos;
gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), LinearRgba::rgb(1.0, 0.0, 0.5));
}

gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, Srgba::rgb(1., 0., 0.5));
gizmos.line(contact_point, contact_point + Vec3::X, LinearRgba::RED);
gizmos.line(contact_point, contact_point + Vec3::Y, LinearRgba::GREEN);
gizmos.line(contact_point, contact_point + Vec3::Z, LinearRgba::BLUE);
//gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, LinearRgba::rgb(1., 0., 0.5));
}
}

Expand Down

0 comments on commit a8409e5

Please sign in to comment.