Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
cylindrical_billboard.h
1 #pragma once
2 
3 #include "../behaviour.h"
4 #include "../stage_node_behaviour.h"
5 #include "../../generic/managed.h"
6 
7 namespace smlt {
8 namespace behaviours {
9 
11  public StageNodeBehaviour,
12  public RefCounted<CylindricalBillboard> {
13 
14 public:
15  CylindricalBillboard(StageNode* target, const Vec3& forward=Vec3::NEGATIVE_Z):
16  target_(target),
17  forward_(forward) {}
18 
19  void set_target(StageNode* target) {
20  target_ = target;
21  }
22 
23  void late_update(float dt) override {
24  _S_UNUSED(dt);
25 
26  if(target_) {
27  auto dir = (
28  target_->absolute_position() - stage_node->absolute_position()
29  ).normalized();
30 
31  smlt::Plane up_plane(smlt::Vec3::POSITIVE_Y, 0);
32 
33  /* If we're right above/below the Y axis then default to
34  * looking down negative Z */
35  auto d = std::abs(dir.dot(Vec3::POSITIVE_Y));
36  if(almost_equal(d, 1.0f)) {
37  dir = forward_;
38  }
39 
40  dir = up_plane.project(dir).normalized();
41 
42  auto rot = forward_.rotation_to(dir);
43  stage_node->rotate_to_absolute(rot);
44  }
45  }
46 
47  const char* name() const override {
48  return "cylindrical_billboard";
49  }
50 private:
51  StageNode* target_ = nullptr;
52  Vec3 forward_;
53 };
54 
55 }
56 }
smlt::Vec3
Definition: vec3.h:23
smlt::behaviours::StageNodeBehaviour
Definition: stage_node_behaviour.h:12
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::StageNode
Definition: stage_node.h:62
smlt::behaviours::CylindricalBillboard
Definition: cylindrical_billboard.h:12
smlt::Plane
Definition: plane.h:18