Skip to content

Commit

Permalink
refactor example
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Apr 18, 2024
1 parent ca88b40 commit b8eb987
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 54 deletions.
66 changes: 66 additions & 0 deletions examples/projects/Youbot/KeyController.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

#ifndef THREEPP_KEYCONTROLLER_HPP
#define THREEPP_KEYCONTROLLER_HPP

#include "threepp/input/KeyListener.hpp"

#include "Youbot.hpp"

class KeyController: public threepp::KeyListener {

public:
KeyController(Youbot& youbot)
: youbot_(&youbot) {}

void onKeyPressed(KeyEvent evt) override {
if (evt.key == Key::W) {
keyState_.up = true;
} else if (evt.key == Key::S) {
keyState_.down = true;
} else if (evt.key == Key::D) {
keyState_.right = true;
} else if (evt.key == Key::A) {
keyState_.left = true;
}
}

void onKeyReleased(KeyEvent evt) override {
if (evt.key == Key::W) {
keyState_.up = false;
} else if (evt.key == Key::S) {
keyState_.down = false;
} else if (evt.key == Key::D) {
keyState_.right = false;
} else if (evt.key == Key::A) {
keyState_.left = false;
}
}

void update(float dt) {

if (keyState_.up) {
youbot_->driveForwards(dt);
}
if (keyState_.down) {
youbot_->driveBackwards(dt);
}
if (keyState_.right) {
youbot_->driveRight(dt);
}
if (keyState_.left) {
youbot_->driveLeft(dt);
}
}

private:
Youbot* youbot_;

struct KeyState {
bool left = false;
bool right = false;
bool up = false;
bool down = false;
} keyState_;
};

#endif//THREEPP_KEYCONTROLLER_HPP
52 changes: 2 additions & 50 deletions examples/projects/Youbot/Youbot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,9 @@
using namespace threepp;


struct Youbot: Object3D, KeyListener {

void onKeyPressed(KeyEvent evt) override {
if (evt.key == Key::W) {
keyState_.up = true;
} else if (evt.key == Key::S) {
keyState_.down = true;
} else if (evt.key == Key::D) {
keyState_.right = true;
} else if (evt.key == Key::A) {
keyState_.left = true;
}
}

void onKeyReleased(KeyEvent evt) override {
if (evt.key == Key::W) {
keyState_.up = false;
} else if (evt.key == Key::S) {
keyState_.down = false;
} else if (evt.key == Key::D) {
keyState_.right = false;
} else if (evt.key == Key::A) {
keyState_.left = false;
}
}
class Youbot: public Object3D {

public:
void driveForwards(float dt) {
float scale = 100;
translateX(translationSpeed * dt);
Expand Down Expand Up @@ -88,22 +65,6 @@ struct Youbot: Object3D, KeyListener {
};
}

void update(float dt) {

if (keyState_.up) {
driveForwards(dt);
}
if (keyState_.down) {
driveBackwards(dt);
}
if (keyState_.right) {
driveRight(dt);
}
if (keyState_.left) {
driveLeft(dt);
}
}

static std::unique_ptr<Youbot> create(const std::filesystem::path& path) {
AssimpLoader loader;
auto model = loader.load(path);
Expand All @@ -113,15 +74,6 @@ struct Youbot: Object3D, KeyListener {
}

private:
struct KeyState {
bool left = false;
bool right = false;
bool up = false;
bool down = false;
};


KeyState keyState_;
float rotationSpeed = 2;
float translationSpeed = 5;

Expand Down
9 changes: 7 additions & 2 deletions examples/projects/Youbot/youbot.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "threepp/threepp.hpp"

#include "KeyController.hpp"
#include "Youbot.hpp"

#include <future>
Expand Down Expand Up @@ -45,10 +46,12 @@ int main() {


std::shared_ptr<Youbot> youbot;
std::unique_ptr<KeyController> keyController;
auto future = std::async([&] {
youbot = Youbot::create("data/models/collada/youbot.dae");
keyController = std::make_unique<KeyController>(*youbot);
renderer.invokeLater([&] {
canvas.addKeyListener(*youbot);
canvas.addKeyListener(*keyController);
scene->add(youbot);
handle.setText("Use WASD keys to steer robot", opts);
});
Expand All @@ -70,6 +73,8 @@ int main() {
renderer.render(*scene, *camera);
hud.apply(renderer);

if (youbot) youbot->update(dt);
if (youbot) keyController->update(dt);
});

future.get();
}
7 changes: 5 additions & 2 deletions examples/projects/Youbot/youbot_kine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "kine/Kine.hpp"
#include "kine/ik/CCDSolver.hpp"

#include "KeyController.hpp"
#include "threepp/extras/imgui/ImguiContext.hpp"

#include <future>
Expand Down Expand Up @@ -107,13 +108,15 @@ int main() {


std::shared_ptr<Youbot> youbot;
std::unique_ptr<KeyController> keyController;
auto future = std::async([&] {
youbot = Youbot::create("data/models/collada/youbot.dae");
youbot->add(targetHelper);
youbot->add(endEffectorHelper);
endEffectorHelper->visible = true;
keyController = std::make_unique<KeyController>(*youbot);
renderer.invokeLater([&] {
canvas.addKeyListener(*youbot);
canvas.addKeyListener(*keyController);
scene->add(youbot);
hud.remove(handle);
});
Expand Down Expand Up @@ -180,7 +183,7 @@ int main() {
}

youbot->setJointValues(ui.values);
youbot->update(dt);
keyController->update(dt);
} else {

hud.apply(renderer);
Expand Down

0 comments on commit b8eb987

Please sign in to comment.