载入中...
搜索中...
未找到
SceneLinks.cpp
浏览该文件的文档.
1// Registers the scene link kinds backed by physics: 2D and 3D rigid bodies.
2//
3// These are the only kinds that can drive a node instead of following it
4// (syncMode 1), so they are also the only ones supplying pullWorld.
5
7#include "physics/Body.h"
8#include "physics/Body3D.h"
9#include "scene/SceneHost.h"
10#include "scene/SceneLink.h"
11
12#include <glm/gtc/quaternion.hpp>
13
14namespace eve::physics {
15namespace {
16
19
20void pushBody2D(const SceneNode &n, void *target) {
21 auto *b = static_cast<Body *>(target);
22 b->setPosition(n.x, n.y);
23 b->setAngle(n.roll);
24}
25
26void pullBody2D(SceneNode &n, void *target) {
27 auto *b = static_cast<Body *>(target);
28 n.x = b->getX();
29 n.y = b->getY();
30 n.roll = b->getAngle();
31}
32
33bool aliveBody2D(const void *target) {
34 return static_cast<const Body *>(target)->raw() != nullptr;
35}
36
37void pushBody3D(const SceneNode &n, void *target) {
38 auto *b = static_cast<Body3D *>(target);
39 b->setPosition(n.x, n.y, n.z);
40 glm::quat q = glm::angleAxis(n.yaw, glm::vec3(0.f, 1.f, 0.f)) *
41 glm::angleAxis(n.pitch, glm::vec3(1.f, 0.f, 0.f)) *
42 glm::angleAxis(n.roll, glm::vec3(0.f, 0.f, 1.f));
43 b->setRotation(q.x, q.y, q.z, q.w);
44}
45
46void pullBody3D(SceneNode &n, void *target) {
47 auto *b = static_cast<Body3D *>(target);
48 n.x = b->getX();
49 n.y = b->getY();
50 n.z = b->getZ();
51 glm::quat q(b->getRotW(), b->getRotX(), b->getRotY(), b->getRotZ());
52 // Exact inverse of the composition in pushBody3D() (see TransformEuler.h):
53 // glm::eulerAngles() returns a different Euler family, so a body rotated about
54 // more than one axis came back to the node at the wrong orientation.
55 eve::eulerYxzFromQuat(q, n.yaw, n.pitch, n.roll);
56}
57
58bool aliveBody3D(const void *target) {
59 return static_cast<const Body3D *>(target)->isValid();
60}
61
62struct Register {
63 Register() {
64 eve::scene::registerLinkKind({"physics2d", &pushBody2D, &pullBody2D, &aliveBody2D});
65 eve::scene::registerLinkKind({"physics3d", &pushBody3D, &pullBody3D, &aliveBody3D});
66 }
67} g_register;
68
69} // namespace
70} // namespace eve::physics
LogicalId target
double raw
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
MeleePoint3 b
Definition MeleeHit.cpp:41
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
int registerLinkKind(const LinkOps &ops)
Registers link kind.
void eulerYxzFromQuat(const glm::qua< T, glm::defaultp > &q, T &yaw, T &pitch, T &roll)
Quaternion overload of eulerYxzFromMatrix(); float and double both work.
LinkOps public API.
Definition SceneLink.h:28
Retained scene node (arena). Conceptual GameObject; isomorphic to eve::ui::UINode.
Definition SceneHost.h:46