Skip to content

Commit

Permalink
feat: robot arm ik
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Oct 17, 2024
1 parent 78a0d12 commit 87f8e98
Show file tree
Hide file tree
Showing 14 changed files with 468 additions and 186 deletions.
Binary file added assets.tar.gz
Binary file not shown.
41 changes: 41 additions & 0 deletions crates/beet_examples/src/components/keyboard_controller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use bevy::prelude::*;

#[derive(Component, Reflect)]
#[reflect(Default, Component)]
pub struct KeyboardController {
pub speed: f32,
}

impl Default for KeyboardController {
fn default() -> Self { Self { speed: 3. } }
}

pub fn keyboard_controller(
time: Res<Time>,
keys: Res<ButtonInput<KeyCode>>,
mut query: Populated<(&mut Transform, &KeyboardController)>,
) {
for (mut transform, controller) in query.iter_mut() {
let mut direction = Vec3::ZERO;
if keys.pressed(KeyCode::KeyW) {
direction -= Vec3::Z;
}
if keys.pressed(KeyCode::KeyS) {
direction += Vec3::Z;
}
if keys.pressed(KeyCode::KeyA) {
direction -= Vec3::X;
}
if keys.pressed(KeyCode::KeyD) {
direction += Vec3::X;
}
if keys.pressed(KeyCode::KeyR) {
direction += Vec3::Y;
}
if keys.pressed(KeyCode::KeyF) {
direction -= Vec3::Y;
}
transform.translation +=
direction * controller.speed * time.delta_seconds();
}
}
3 changes: 3 additions & 0 deletions crates/beet_examples/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use self::collectable::*;
pub mod follow_cursor;
#[allow(unused_imports)]
pub use self::follow_cursor::*;
pub mod keyboard_controller;
#[allow(unused_imports)]
pub use self::keyboard_controller::*;
pub mod randomize_position;
#[allow(unused_imports)]
pub use self::randomize_position::*;
Expand Down
5 changes: 3 additions & 2 deletions crates/beet_examples/src/plugins/beet_example_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use crate::beet::prelude::*;
use crate::prelude::*;
use beetmash::prelude::*;
use bevy::prelude::*;

Expand Down Expand Up @@ -81,9 +81,10 @@ fn plugin_2d(app: &mut App) {
fn plugin_3d(app: &mut App) {
app.add_systems(
Update,
(follow_cursor_3d, camera_distance, rotate_collectables),
(follow_cursor_3d, camera_distance, rotate_collectables,keyboard_controller),
)
.register_type::<FollowCursor3d>()
.register_type::<KeyboardController>()
.register_type::<CameraDistance>()
/*-*/;
}
106 changes: 77 additions & 29 deletions crates/beet_examples/src/scenes/spatial/inverse_kinematics.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,117 @@
use crate::prelude::FollowCursor3d;
use beet_spatial::prelude::Ik2Dof;
use beet_spatial::prelude::Ik2DofTransforms;
use beet_spatial::prelude::IkSegment;
// use crate::beet::prelude::*;
// use beetmash::core::scenes::Foxie;
use crate::beet::prelude::*;
use crate::prelude::*;
use beetmash::prelude::*;
// use bevy::animation::RepeatAnimation;
use bevy::{
color::palettes::tailwind,
prelude::*,
};
// use std::time::Duration;
use bevy::color::palettes::tailwind;
use bevy::prelude::*;


pub fn inverse_kinematics(mut commands: Commands) {
let ik_solver = Ik2Dof::new(IkSegment::DEG_360, IkSegment::DEG_360);
let arm_width = 0.1;

commands.spawn((
Name::new("Camera"),
BundlePlaceholder::Camera3d,
Transform::from_xyz(0., 0., 5.0).looking_at(Vec3::ZERO, Vec3::Y),
Transform::from_xyz(0., 7., 7.).looking_at(Vec3::ZERO, Vec3::Y),
));
spawn_arm(commands);
}
pub fn spawn_arm(mut commands: Commands) {
let target = spawn_target(&mut commands);

commands.spawn((
Name::new("scene"),
BundlePlaceholder::Gltf("robot-arm/robot-arm.glb".into()),
Transform::from_scale(Vec3::splat(10.)),
TargetAgent(target),
IkSpawner::default(),
));
let target = commands
}

fn spawn_target(commands: &mut Commands) -> Entity {
commands
.spawn((
Name::new("Mouse"),
FollowCursor3d::ORIGIN_Z,
Transform::default().looking_to(-Vec3::Z, Vec3::Y),
Name::new("Target"),
KeyboardController::default(),
// FollowCursor3d::ORIGIN_Z,
Transform::from_xyz(2.5, 1., 0.).looking_to(-Vec3::Z, Vec3::Y),
BundlePlaceholder::Pbr {
mesh: Circle::new(0.2).into(),
mesh: Sphere::new(0.2).into(),
material: MaterialPlaceholder::unlit(tailwind::BLUE_500),
},
))
.id();
.id()
}


pub fn spawn_test_arm(mut commands: Commands) {
let target = spawn_target(&mut commands);

let ik_solver = IkArm4Dof::new(
0.,
IkSegment::DEG_360,
IkSegment::DEG_360,
IkSegment::DEG_360.with_len(0.2),
);
let arm_width = 0.1;

let root = commands
.spawn((
Name::new("IK Root"),
Transform::default().looking_to(Vec3::X, Vec3::Y),
))
.id();
let mut entity1 = Entity::PLACEHOLDER;
let mut entity2 = Entity::PLACEHOLDER;
let mut entity_base = Entity::PLACEHOLDER;
let mut entity_segment1 = Entity::PLACEHOLDER;
let mut entity_segment2 = Entity::PLACEHOLDER;
let mut entity_segment3 = Entity::PLACEHOLDER;

commands.entity(root).with_children(|parent| {
entity1 = ik_segment(
entity_base = parent
.spawn((Transform::default(), BundlePlaceholder::Pbr {
mesh: Cylinder::new(0.2, 0.1).into(),
material: MaterialPlaceholder::unlit(tailwind::AMBER_100),
}))
.id();
});

commands.entity(entity_base).with_children(|parent| {
entity_segment1 = ik_segment(
&mut parent.spawn_empty(),
&ik_solver.segment1,
Transform::default(),
arm_width,
tailwind::AMBER_300,
);
});

commands.entity(entity_segment1).with_children(|parent| {
entity_segment2 = ik_segment(
&mut parent.spawn_empty(),
&ik_solver.segment2,
Transform::from_xyz(0., 0., -ik_solver.segment1.len),
arm_width,
tailwind::AMBER_500,
);
});

commands.entity(entity1).with_children(|parent| {
entity2 = ik_segment(
commands.entity(entity_segment2).with_children(|parent| {
entity_segment3 = ik_segment(
&mut parent.spawn_empty(),
&ik_solver.segment2,
Transform::from_xyz(0., 0., -ik_solver.segment1.len),
arm_width,
tailwind::AMBER_700,
);
});

commands.spawn((
Name::new("IK Solver"),
Ik2DofTransforms::new(ik_solver, target, entity1, entity2),
IkArm4DofTransforms::new(
ik_solver,
target,
entity_base,
entity_segment1,
entity_segment2,
entity_segment3,
),
));
}

Expand All @@ -74,6 +121,7 @@ pub fn ik_segment(
seg: &IkSegment,
transform: Transform,
arm_width: f32,
color: Srgba,
) -> Entity {
commands
.insert((Name::new("Segment"), transform, Visibility::Visible))
Expand All @@ -83,7 +131,7 @@ pub fn ik_segment(
Transform::from_xyz(0., 0., -seg.len * 0.5),
BundlePlaceholder::Pbr {
mesh: Cuboid::new(arm_width, arm_width, seg.len).into(),
material: MaterialPlaceholder::unlit(tailwind::AMBER_500),
material: MaterialPlaceholder::unlit(color),
},
));
})
Expand Down
83 changes: 0 additions & 83 deletions crates/beet_spatial/src/inverse_kinematics/ik_2dof_transforms.rs

This file was deleted.

Loading

0 comments on commit 87f8e98

Please sign in to comment.