Skip to content

Commit

Permalink
Format, clippy, add arm tip.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanders committed Jul 1, 2023
1 parent 37d5ad7 commit f8fe3ed
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 57 deletions.
16 changes: 8 additions & 8 deletions battleground_construct/src/components/gun_battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod test {
battery.update(t);
assert!(battery.is_ready());
battery.fired(t);
assert_eq!(battery.is_ready(), false); // not ready right now, in between the inter gun
assert!(!battery.is_ready()); // not ready right now, in between the inter gun
t += inter_gun_duration;
battery.update(t);
assert!(battery.is_ready());
Expand All @@ -188,20 +188,20 @@ mod test {
battery.update(t);
battery.fired(t);
battery.update(t);
assert_eq!(battery.is_ready(), false);
assert!(!battery.is_ready());

// Next shot is the last in the gun battery.
battery.fired(t);
assert_eq!(battery.is_ready(), false);
assert!(!battery.is_ready());
t += inter_gun_duration;
battery.update(t);
assert_eq!(battery.is_ready(), false);
assert!(!battery.is_ready());
t += inter_gun_duration;
battery.update(t);
assert_eq!(battery.is_ready(), false);
assert!(!battery.is_ready());
t += inter_gun_duration;
battery.update(t);
assert_eq!(battery.is_ready(), true); // at start again.
assert!(battery.is_ready()); // at start again.
assert_eq!(battery.gun_index(), 0); // at start again.
}

Expand Down Expand Up @@ -234,10 +234,10 @@ mod test {

// Next shot is the last in the gun battery.
battery.fired(t);
assert_eq!(battery.is_ready(), false);
assert!(!battery.is_ready());
t += battery_reload;
battery.update(t);
assert_eq!(battery.is_ready(), true); // at start again.
assert!(battery.is_ready()); // at start again.
assert_eq!(battery.gun_index(), 0); // at start again.
}
}
Expand Down
6 changes: 5 additions & 1 deletion battleground_construct/src/components/radar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ mod test {
radar.update_reflections(&radar_pose, &reflections);
let obtained = radar.reflections();
println!("Obtained: {obtained:?}");
let expected = vec![(3.1415926f32, 0.0f32), (1.5707964, 0.0), (0.0, 1.5707964)];
let expected = vec![
(f32::consts::PI, 0.0f32),
(f32::consts::PI / 2, 0.0),
(0.0, f32::consts::PI / 2),
];
for (obtain, expect) in obtained.iter().zip(expected.iter()) {
approx_equal!(obtain.yaw, expect.0, 0.001);
approx_equal!(obtain.pitch, expect.1, 0.001);
Expand Down
6 changes: 3 additions & 3 deletions battleground_construct/src/display/arm_joint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Drawable for ArmJoint {
Element {
transform: Mat4::from_translation(Vec3::new(0.0, 0.0, 0.0)),
primitive: Primitive::Cylinder(Cylinder {
radius: radius,
radius,
height: length,
}),
material,
Expand All @@ -58,13 +58,13 @@ impl Drawable for ArmJoint {
Element {
transform: Mat4::from_angle_y(Deg(-90.0))
* Mat4::from_translation(Vec3::new(0.0, 0.0, 0.0)),
primitive: Primitive::Circle(Circle { radius: radius }),
primitive: Primitive::Circle(Circle { radius }),
material,
},
Element {
transform: Mat4::from_angle_y(Deg(-90.0))
* Mat4::from_translation(Vec3::new(0.0, 0.0, -length)),
primitive: Primitive::Circle(Circle { radius: radius }),
primitive: Primitive::Circle(Circle { radius }),
material,
},
Element {
Expand Down
20 changes: 20 additions & 0 deletions battleground_construct/src/units/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct UnitArm {
pub base_revolute_entity: EntityId,
pub arm_entity: EntityId,
pub lower_arm_entity: EntityId,
pub tip_entity: EntityId,
}
impl Component for UnitArm {}

Expand All @@ -36,6 +37,7 @@ impl Unit for UnitArm {
self.base_revolute_entity,
self.arm_entity,
self.lower_arm_entity,
self.tip_entity,
]
}
}
Expand Down Expand Up @@ -68,6 +70,7 @@ pub fn spawn_arm(world: &mut World, config: ArmSpawnConfig) -> EntityId {

let arm_entity = world.add_entity();
let lower_arm_entity = world.add_entity();
let tip_entity = world.add_entity();

let unit_arm = UnitArm {
unit_entity,
Expand All @@ -76,6 +79,7 @@ pub fn spawn_arm(world: &mut World, config: ArmSpawnConfig) -> EntityId {
base_revolute_entity,
arm_entity,
lower_arm_entity,
tip_entity,
};
// Unit must be first in the group!
let mut tank_group_entities: Vec<EntityId> = vec![unit_entity];
Expand Down Expand Up @@ -168,6 +172,22 @@ pub fn spawn_arm(world: &mut World, config: ArmSpawnConfig) -> EntityId {
);
world.add_component(lower_arm_entity, Parent::new(arm_entity));

// ----- tip
world.add_component(
tip_entity,
PreTransform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
);
world.add_component(tip_entity, Parent::new(lower_arm_entity));
let particle_effect_id = components::id_generator::generate_id(world);
world.add_component(
tip_entity,
display::particle_emitter::ParticleEmitter::bullet_trail(
particle_effect_id,
0.05,
display::Color::WHITE,
),
);

// ----- Control
world.add_component(control_entity, display::draw_module::DrawComponent::new());
register_interface.get_mut().add_module(
Expand Down
55 changes: 23 additions & 32 deletions battleground_construct/src/util/box_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,55 +241,46 @@ mod test {
let b = AxisAlignedBox::new(1.0f32, 1.0, 1.0);

// x direction.
assert_eq!(b.is_inside(Vector3::new(-1.0, 0.0, 0.0)), false);
assert_eq!(b.is_inside(Vector3::new(-0.4, 0.0, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(0.4, 0.0, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(1.0, 0.0, 0.0)), false);
assert!(!b.is_inside(Vector3::new(-1.0, 0.0, 0.0)));
assert!(b.is_inside(Vector3::new(-0.4, 0.0, 0.0)));
assert!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)));
assert!(b.is_inside(Vector3::new(0.4, 0.0, 0.0)));
assert!(!b.is_inside(Vector3::new(1.0, 0.0, 0.0)));

// y direction.
assert_eq!(b.is_inside(Vector3::new(0.0, -1.0, 0.0)), false);
assert_eq!(b.is_inside(Vector3::new(0.0, -0.4, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.4, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 1.0, 0.0)), false);
assert!(!b.is_inside(Vector3::new(0.0, -1.0, 0.0)));
assert!(b.is_inside(Vector3::new(0.0, -0.4, 0.0)));
assert!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)));
assert!(b.is_inside(Vector3::new(0.0, 0.4, 0.0)));
assert!(!b.is_inside(Vector3::new(0.0, 1.0, 0.0)));

// z direction.
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, -1.0)), false);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, -0.4)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, 0.4)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, 1.0)), false);
assert!(!b.is_inside(Vector3::new(0.0, 0.0, -1.0)));
assert!(b.is_inside(Vector3::new(0.0, 0.0, -0.4)));
assert!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)));
assert!(b.is_inside(Vector3::new(0.0, 0.0, 0.4)));
assert!(!b.is_inside(Vector3::new(0.0, 0.0, 1.0)));

// diagonal
assert_eq!(b.is_inside(Vector3::new(-1.0, -1.0, -1.0)), false);
assert_eq!(b.is_inside(Vector3::new(-0.4, -0.4, -0.4)), true);
assert_eq!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)), true);
assert_eq!(b.is_inside(Vector3::new(0.4, 0.4, 0.4)), true);
assert_eq!(b.is_inside(Vector3::new(1.0, 1.0, 1.0)), false);
assert!(!b.is_inside(Vector3::new(-1.0, -1.0, -1.0)));
assert!(b.is_inside(Vector3::new(-0.4, -0.4, -0.4)));
assert!(b.is_inside(Vector3::new(0.0, 0.0, 0.0)));
assert!(b.is_inside(Vector3::new(0.4, 0.4, 0.4)));
assert!(!b.is_inside(Vector3::new(1.0, 1.0, 1.0)));
}

#[test]
fn test_is_intersecting() {
use cgmath::vec3;
let b = AxisAlignedBox::new(1.0f32, 1.0, 1.0);
// assert_eq!(b.is_intersecting(vec3(1.0, 1.0, 1.0), vec3(0.0, 0.0, 0.0), ), true);
assert_eq!(
b.is_intersecting(vec3(1.0, 1.0, 1.0), vec3(0.0, 0.0, 0.0),),
true
);
assert!(b.is_intersecting(vec3(1.0, 1.0, 1.0), vec3(0.0, 0.0, 0.0),));
// assert_eq!(b.is_intersecting(vec3(1.0, 1.5, 1.0), vec3(2.0, 2.0, 2.0), ), false);
assert_eq!(
b.is_intersecting(vec3(1.0, 1.5, 1.0), vec3(2.0, 2.0, 2.0),),
false
);
assert!(!b.is_intersecting(vec3(1.0, 1.5, 1.0), vec3(2.0, 2.0, 2.0),));

// pointing at box, but not into it.
// assert_eq!(b.is_intersecting(vec3(3.0, 0.0, 0.0), vec3(5.0, 0.0, 0.0)), false);
assert_eq!(
b.is_intersecting(vec3(3.0, 0.0, 0.0), vec3(5.0, 0.0, 0.0)),
false
);
assert!(!b.is_intersecting(vec3(3.0, 0.0, 0.0), vec3(5.0, 0.0, 0.0)));

verify_points(&b, vec3(1.0, 1.0, 1.0), vec3(0.0, 0.0, 0.0), 0.001);
verify_points(&b, vec3(2.0, 0.0, 0.0), vec3(3.0, 0.0, 0.0), 0.001);
Expand Down
6 changes: 3 additions & 3 deletions battleground_construct/tests/rotating_arm_velocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn test_rotating_arm() {
world.add_component(arm_origin, Pose::new());

let arm_rotation = world.add_entity();
world.add_component(arm_rotation, Parent::new(arm_origin.clone()));
world.add_component(arm_rotation, Parent::new(arm_origin));

let mut arm_revolute = components::revolute::Revolute::new_with_axis(Vec3::new(0.0, 0.0, 1.0));
let rotation_vel = 1.0;
Expand All @@ -82,7 +82,7 @@ fn test_rotating_arm() {
world.add_component(arm_rotation, Velocity::new());

let arm_entity_pretransform = world.add_entity();
world.add_component(arm_entity_pretransform, Parent::new(arm_rotation.clone()));
world.add_component(arm_entity_pretransform, Parent::new(arm_rotation));
world.add_component(arm_entity_pretransform, Pose::new());
world.add_component(arm_entity_pretransform, Velocity::new());
world.add_component(
Expand All @@ -92,7 +92,7 @@ fn test_rotating_arm() {

let arm_tip = world.add_entity();
world.add_component(arm_tip, Pose::new());
world.add_component(arm_tip, Parent::new(arm_entity_pretransform.clone()));
world.add_component(arm_tip, Parent::new(arm_entity_pretransform));

let clock_id = world.add_entity();
world.add_component(clock_id, Clock::new());
Expand Down
20 changes: 10 additions & 10 deletions battleground_construct/tests/tank_body_velocities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn test_tank_body_velocities() {
);
world.add_component(turret_id, components::pose::Pose::new());
world.add_component(turret_id, turret_vel);
world.add_component(turret_id, Parent::new(base_id.clone()));
world.add_component(turret_id, Parent::new(base_id));

// Add the barrel linear offset, and joint.
let barrel_id = world.add_entity();
Expand All @@ -69,20 +69,20 @@ fn test_tank_body_velocities() {
);
world.add_component(barrel_id, components::pose::Pose::new());
world.add_component(barrel_id, barrel_vel);
world.add_component(barrel_id, Parent::new(turret_id.clone()));
world.add_component(barrel_id, Parent::new(turret_id));

// Then, the arm from the joint to the center of the barrel.
let barrel_cog_id = world.add_entity();
world.add_component(
barrel_cog_id,
PreTransform::from_translation(Vec3::new(1.0, 0.0, 0.0)),
);
world.add_component(barrel_cog_id, Parent::new(barrel_id.clone()));
world.add_component(barrel_cog_id, Parent::new(barrel_id));

// Finally, a meter further from the center of the barrel, add the nozzle.
let nozzle_id = world.add_entity();
println!("nozzle_id: {nozzle_id:?}");
world.add_component(nozzle_id, Parent::new(barrel_cog_id.clone()));
world.add_component(nozzle_id, Parent::new(barrel_cog_id));
world.add_component(
nozzle_id,
PreTransform::from_translation(Vec3::new(1.0, 0.0, 0.0)),
Expand All @@ -91,7 +91,7 @@ fn test_tank_body_velocities() {
// Shell at the exit, with rotational velocity of 5.5 radians
let shell_id = world.add_entity();
println!("shell_id: {shell_id:?}");
world.add_component(shell_id, Parent::new(nozzle_id.clone()));
world.add_component(shell_id, Parent::new(nozzle_id));
let mut shell_revolute =
components::revolute::Revolute::new_with_axis(Vec3::new(1.0, 0.0, 0.0));

Expand Down Expand Up @@ -151,7 +151,7 @@ fn test_tank_body_velocities() {
let rev = v.as_mut().unwrap();

rev.set_position(std::f32::consts::PI / 4.0);
rev.clone()
**rev
};
{
let mut p = world.component_mut::<Pose>(turret_id);
Expand All @@ -167,7 +167,7 @@ fn test_tank_body_velocities() {
let rev = v.as_mut().unwrap();

rev.set_position(-std::f32::consts::PI / 4.0);
rev.clone()
**rev
};
{
let mut p = world.component_mut::<Pose>(barrel_id);
Expand Down Expand Up @@ -238,14 +238,14 @@ fn test_tank_body_velocities() {
assert_eq!(vel_barrel_cog_in_global.w, vec3(-3.11127, 3.11127, 5.5));
assert_eq!(
vel_barrel_cog_in_global.v,
vec3(-1.3945432, 6.894543, -3.111270)
vec3(-1.3945432, 6.894543, -3.111_27)
);

let vel_shell_in_global = world_velocity(&world, shell_id);
println!("vel_shell_in_global: {vel_shell_in_global:?}");
assert_eq!(
vel_shell_in_global.v,
vec3(-1.9445434, 11.844543, -6.222540)
vec3(-1.9445434, 11.844543, -6.222_54)
);
assert_eq!(vel_shell_in_global.w, vec3(-0.36127007, 5.861270, 9.389088));
assert_eq!(vel_shell_in_global.w, vec3(-0.36127007, 5.861_27, 9.389088));
}

0 comments on commit f8fe3ed

Please sign in to comment.