gltf_physics_body.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**************************************************************************/
  2. /* gltf_physics_body.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gltf_physics_body.h"
  31. #include "scene/3d/physics/animatable_body_3d.h"
  32. #include "scene/3d/physics/area_3d.h"
  33. #include "scene/3d/physics/character_body_3d.h"
  34. #include "scene/3d/physics/static_body_3d.h"
  35. #include "scene/3d/physics/vehicle_body_3d.h"
  36. void GLTFPhysicsBody::_bind_methods() {
  37. ClassDB::bind_static_method("GLTFPhysicsBody", D_METHOD("from_node", "body_node"), &GLTFPhysicsBody::from_node);
  38. ClassDB::bind_method(D_METHOD("to_node"), &GLTFPhysicsBody::to_node);
  39. ClassDB::bind_static_method("GLTFPhysicsBody", D_METHOD("from_dictionary", "dictionary"), &GLTFPhysicsBody::from_dictionary);
  40. ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFPhysicsBody::to_dictionary);
  41. ClassDB::bind_method(D_METHOD("get_body_type"), &GLTFPhysicsBody::get_body_type);
  42. ClassDB::bind_method(D_METHOD("set_body_type", "body_type"), &GLTFPhysicsBody::set_body_type);
  43. ClassDB::bind_method(D_METHOD("get_mass"), &GLTFPhysicsBody::get_mass);
  44. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &GLTFPhysicsBody::set_mass);
  45. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &GLTFPhysicsBody::get_linear_velocity);
  46. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &GLTFPhysicsBody::set_linear_velocity);
  47. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &GLTFPhysicsBody::get_angular_velocity);
  48. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &GLTFPhysicsBody::set_angular_velocity);
  49. ClassDB::bind_method(D_METHOD("get_center_of_mass"), &GLTFPhysicsBody::get_center_of_mass);
  50. ClassDB::bind_method(D_METHOD("set_center_of_mass", "center_of_mass"), &GLTFPhysicsBody::set_center_of_mass);
  51. ClassDB::bind_method(D_METHOD("get_inertia_diagonal"), &GLTFPhysicsBody::get_inertia_diagonal);
  52. ClassDB::bind_method(D_METHOD("set_inertia_diagonal", "inertia_diagonal"), &GLTFPhysicsBody::set_inertia_diagonal);
  53. ClassDB::bind_method(D_METHOD("get_inertia_orientation"), &GLTFPhysicsBody::get_inertia_orientation);
  54. ClassDB::bind_method(D_METHOD("set_inertia_orientation", "inertia_orientation"), &GLTFPhysicsBody::set_inertia_orientation);
  55. #ifndef DISABLE_DEPRECATED
  56. ClassDB::bind_method(D_METHOD("get_inertia_tensor"), &GLTFPhysicsBody::get_inertia_tensor);
  57. ClassDB::bind_method(D_METHOD("set_inertia_tensor", "inertia_tensor"), &GLTFPhysicsBody::set_inertia_tensor);
  58. #endif // DISABLE_DEPRECATED
  59. ADD_PROPERTY(PropertyInfo(Variant::STRING, "body_type"), "set_body_type", "get_body_type");
  60. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass"), "set_mass", "get_mass");
  61. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  62. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  63. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass"), "set_center_of_mass", "get_center_of_mass");
  64. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inertia_diagonal"), "set_inertia_diagonal", "get_inertia_diagonal");
  65. ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "inertia_orientation"), "set_inertia_orientation", "get_inertia_orientation");
  66. #ifndef DISABLE_DEPRECATED
  67. ADD_PROPERTY(PropertyInfo(Variant::BASIS, "inertia_tensor"), "set_inertia_tensor", "get_inertia_tensor");
  68. #endif // DISABLE_DEPRECATED
  69. }
  70. String GLTFPhysicsBody::get_body_type() const {
  71. switch (body_type) {
  72. case PhysicsBodyType::STATIC:
  73. return "static";
  74. case PhysicsBodyType::ANIMATABLE:
  75. return "animatable";
  76. case PhysicsBodyType::CHARACTER:
  77. return "character";
  78. case PhysicsBodyType::RIGID:
  79. return "rigid";
  80. case PhysicsBodyType::VEHICLE:
  81. return "vehicle";
  82. case PhysicsBodyType::TRIGGER:
  83. return "trigger";
  84. }
  85. // Unreachable, the switch cases handle all values the enum can take.
  86. // Omitting this works on Clang but not GCC or MSVC. If reached, it's UB.
  87. return "rigid";
  88. }
  89. void GLTFPhysicsBody::set_body_type(String p_body_type) {
  90. if (p_body_type == "static") {
  91. body_type = PhysicsBodyType::STATIC;
  92. } else if (p_body_type == "animatable") {
  93. body_type = PhysicsBodyType::ANIMATABLE;
  94. } else if (p_body_type == "character") {
  95. body_type = PhysicsBodyType::CHARACTER;
  96. } else if (p_body_type == "rigid") {
  97. body_type = PhysicsBodyType::RIGID;
  98. } else if (p_body_type == "vehicle") {
  99. body_type = PhysicsBodyType::VEHICLE;
  100. } else if (p_body_type == "trigger") {
  101. body_type = PhysicsBodyType::TRIGGER;
  102. } else {
  103. ERR_PRINT("Error setting glTF physics body type: The body type must be one of \"static\", \"animatable\", \"character\", \"rigid\", \"vehicle\", or \"trigger\".");
  104. }
  105. }
  106. GLTFPhysicsBody::PhysicsBodyType GLTFPhysicsBody::get_physics_body_type() const {
  107. return body_type;
  108. }
  109. void GLTFPhysicsBody::set_physics_body_type(PhysicsBodyType p_body_type) {
  110. body_type = p_body_type;
  111. }
  112. real_t GLTFPhysicsBody::get_mass() const {
  113. return mass;
  114. }
  115. void GLTFPhysicsBody::set_mass(real_t p_mass) {
  116. mass = p_mass;
  117. }
  118. Vector3 GLTFPhysicsBody::get_linear_velocity() const {
  119. return linear_velocity;
  120. }
  121. void GLTFPhysicsBody::set_linear_velocity(Vector3 p_linear_velocity) {
  122. linear_velocity = p_linear_velocity;
  123. }
  124. Vector3 GLTFPhysicsBody::get_angular_velocity() const {
  125. return angular_velocity;
  126. }
  127. void GLTFPhysicsBody::set_angular_velocity(Vector3 p_angular_velocity) {
  128. angular_velocity = p_angular_velocity;
  129. }
  130. Vector3 GLTFPhysicsBody::get_center_of_mass() const {
  131. return center_of_mass;
  132. }
  133. void GLTFPhysicsBody::set_center_of_mass(const Vector3 &p_center_of_mass) {
  134. center_of_mass = p_center_of_mass;
  135. }
  136. Vector3 GLTFPhysicsBody::get_inertia_diagonal() const {
  137. return inertia_diagonal;
  138. }
  139. void GLTFPhysicsBody::set_inertia_diagonal(const Vector3 &p_inertia_diagonal) {
  140. inertia_diagonal = p_inertia_diagonal;
  141. }
  142. Quaternion GLTFPhysicsBody::get_inertia_orientation() const {
  143. return inertia_orientation;
  144. }
  145. void GLTFPhysicsBody::set_inertia_orientation(const Quaternion &p_inertia_orientation) {
  146. inertia_orientation = p_inertia_orientation;
  147. }
  148. #ifndef DISABLE_DEPRECATED
  149. Basis GLTFPhysicsBody::get_inertia_tensor() const {
  150. return Basis::from_scale(inertia_diagonal);
  151. }
  152. void GLTFPhysicsBody::set_inertia_tensor(Basis p_inertia_tensor) {
  153. inertia_diagonal = p_inertia_tensor.get_main_diagonal();
  154. }
  155. #endif // DISABLE_DEPRECATED
  156. Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_node(const CollisionObject3D *p_body_node) {
  157. Ref<GLTFPhysicsBody> physics_body;
  158. physics_body.instantiate();
  159. ERR_FAIL_NULL_V_MSG(p_body_node, physics_body, "Tried to create a GLTFPhysicsBody from a CollisionObject3D node, but the given node was null.");
  160. if (cast_to<CharacterBody3D>(p_body_node)) {
  161. physics_body->body_type = PhysicsBodyType::CHARACTER;
  162. } else if (cast_to<AnimatableBody3D>(p_body_node)) {
  163. physics_body->body_type = PhysicsBodyType::ANIMATABLE;
  164. } else if (cast_to<RigidBody3D>(p_body_node)) {
  165. const RigidBody3D *body = cast_to<const RigidBody3D>(p_body_node);
  166. physics_body->mass = body->get_mass();
  167. physics_body->linear_velocity = body->get_linear_velocity();
  168. physics_body->angular_velocity = body->get_angular_velocity();
  169. physics_body->center_of_mass = body->get_center_of_mass();
  170. physics_body->inertia_diagonal = body->get_inertia();
  171. if (cast_to<VehicleBody3D>(p_body_node)) {
  172. physics_body->body_type = PhysicsBodyType::VEHICLE;
  173. } else {
  174. physics_body->body_type = PhysicsBodyType::RIGID;
  175. }
  176. } else if (cast_to<StaticBody3D>(p_body_node)) {
  177. physics_body->body_type = PhysicsBodyType::STATIC;
  178. } else if (cast_to<Area3D>(p_body_node)) {
  179. physics_body->body_type = PhysicsBodyType::TRIGGER;
  180. }
  181. return physics_body;
  182. }
  183. CollisionObject3D *GLTFPhysicsBody::to_node() const {
  184. switch (body_type) {
  185. case PhysicsBodyType::CHARACTER: {
  186. CharacterBody3D *body = memnew(CharacterBody3D);
  187. return body;
  188. }
  189. case PhysicsBodyType::ANIMATABLE: {
  190. AnimatableBody3D *body = memnew(AnimatableBody3D);
  191. return body;
  192. }
  193. case PhysicsBodyType::VEHICLE: {
  194. VehicleBody3D *body = memnew(VehicleBody3D);
  195. body->set_mass(mass);
  196. body->set_linear_velocity(linear_velocity);
  197. body->set_angular_velocity(angular_velocity);
  198. body->set_inertia(inertia_diagonal);
  199. body->set_center_of_mass_mode(RigidBody3D::CENTER_OF_MASS_MODE_CUSTOM);
  200. body->set_center_of_mass(center_of_mass);
  201. return body;
  202. }
  203. case PhysicsBodyType::RIGID: {
  204. RigidBody3D *body = memnew(RigidBody3D);
  205. body->set_mass(mass);
  206. body->set_linear_velocity(linear_velocity);
  207. body->set_angular_velocity(angular_velocity);
  208. body->set_inertia(inertia_diagonal);
  209. body->set_center_of_mass_mode(RigidBody3D::CENTER_OF_MASS_MODE_CUSTOM);
  210. body->set_center_of_mass(center_of_mass);
  211. return body;
  212. }
  213. case PhysicsBodyType::STATIC: {
  214. StaticBody3D *body = memnew(StaticBody3D);
  215. return body;
  216. }
  217. case PhysicsBodyType::TRIGGER: {
  218. Area3D *body = memnew(Area3D);
  219. return body;
  220. }
  221. }
  222. // Unreachable, the switch cases handle all values the enum can take.
  223. // Omitting this works on Clang but not GCC or MSVC. If reached, it's UB.
  224. return nullptr;
  225. }
  226. Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_dictionary) {
  227. Ref<GLTFPhysicsBody> physics_body;
  228. physics_body.instantiate();
  229. Dictionary motion;
  230. if (p_dictionary.has("motion")) {
  231. motion = p_dictionary["motion"];
  232. #ifndef DISABLE_DEPRECATED
  233. } else {
  234. motion = p_dictionary;
  235. #endif // DISABLE_DEPRECATED
  236. }
  237. if (motion.has("type")) {
  238. // Read the body type. This representation sits between glTF's and Godot's physics nodes.
  239. // While we may only read "static", "kinematic", or "dynamic" from a valid glTF file, we
  240. // want to allow another extension to override this to another Godot node type mid-import.
  241. // For example, a vehicle extension may want to override the body type to "vehicle"
  242. // so Godot generates a VehicleBody3D node. Therefore we distinguish by importing
  243. // "dynamic" as "rigid", and "kinematic" as "animatable", in the GLTFPhysicsBody code.
  244. String body_type_string = motion["type"];
  245. if (body_type_string == "static") {
  246. physics_body->body_type = PhysicsBodyType::STATIC;
  247. } else if (body_type_string == "kinematic") {
  248. physics_body->body_type = PhysicsBodyType::ANIMATABLE;
  249. } else if (body_type_string == "dynamic") {
  250. physics_body->body_type = PhysicsBodyType::RIGID;
  251. #ifndef DISABLE_DEPRECATED
  252. } else if (body_type_string == "character") {
  253. physics_body->body_type = PhysicsBodyType::CHARACTER;
  254. } else if (body_type_string == "rigid") {
  255. physics_body->body_type = PhysicsBodyType::RIGID;
  256. } else if (body_type_string == "vehicle") {
  257. physics_body->body_type = PhysicsBodyType::VEHICLE;
  258. } else if (body_type_string == "trigger") {
  259. physics_body->body_type = PhysicsBodyType::TRIGGER;
  260. #endif // DISABLE_DEPRECATED
  261. } else {
  262. ERR_PRINT("Error parsing glTF physics body: The body type in the glTF file \"" + body_type_string + "\" was not recognized.");
  263. }
  264. }
  265. if (motion.has("mass")) {
  266. physics_body->mass = motion["mass"];
  267. }
  268. if (motion.has("linearVelocity")) {
  269. const Array &arr = motion["linearVelocity"];
  270. if (arr.size() == 3) {
  271. physics_body->set_linear_velocity(Vector3(arr[0], arr[1], arr[2]));
  272. } else {
  273. ERR_PRINT("Error parsing glTF physics body: The linear velocity vector must have exactly 3 numbers.");
  274. }
  275. }
  276. if (motion.has("angularVelocity")) {
  277. const Array &arr = motion["angularVelocity"];
  278. if (arr.size() == 3) {
  279. physics_body->set_angular_velocity(Vector3(arr[0], arr[1], arr[2]));
  280. } else {
  281. ERR_PRINT("Error parsing glTF physics body: The angular velocity vector must have exactly 3 numbers.");
  282. }
  283. }
  284. if (motion.has("centerOfMass")) {
  285. const Array &arr = motion["centerOfMass"];
  286. if (arr.size() == 3) {
  287. physics_body->set_center_of_mass(Vector3(arr[0], arr[1], arr[2]));
  288. } else {
  289. ERR_PRINT("Error parsing glTF physics body: The center of mass vector must have exactly 3 numbers.");
  290. }
  291. }
  292. if (motion.has("inertiaDiagonal")) {
  293. const Array &arr = motion["inertiaDiagonal"];
  294. if (arr.size() == 3) {
  295. physics_body->set_inertia_diagonal(Vector3(arr[0], arr[1], arr[2]));
  296. } else {
  297. ERR_PRINT("Error parsing glTF physics body: The inertia diagonal vector must have exactly 3 numbers.");
  298. }
  299. }
  300. if (motion.has("inertiaOrientation")) {
  301. const Array &arr = motion["inertiaOrientation"];
  302. if (arr.size() == 4) {
  303. physics_body->set_inertia_orientation(Quaternion(arr[0], arr[1], arr[2], arr[3]));
  304. } else {
  305. ERR_PRINT("Error parsing glTF physics body: The inertia orientation quaternion must have exactly 4 numbers.");
  306. }
  307. }
  308. return physics_body;
  309. }
  310. Dictionary GLTFPhysicsBody::to_dictionary() const {
  311. Dictionary ret;
  312. if (body_type == PhysicsBodyType::TRIGGER) {
  313. // The equivalent of a Godot Area3D node in glTF is a node that
  314. // defines that it is a trigger, but does not have a shape.
  315. Dictionary trigger;
  316. ret["trigger"] = trigger;
  317. return ret;
  318. }
  319. // All non-trigger body types are defined using the motion property.
  320. Dictionary motion;
  321. // When stored in memory, the body type can correspond to a Godot
  322. // node type. However, when exporting to glTF, we need to squash
  323. // this down to one of "static", "kinematic", or "dynamic".
  324. if (body_type == PhysicsBodyType::STATIC) {
  325. motion["type"] = "static";
  326. } else if (body_type == PhysicsBodyType::ANIMATABLE || body_type == PhysicsBodyType::CHARACTER) {
  327. motion["type"] = "kinematic";
  328. } else {
  329. motion["type"] = "dynamic";
  330. }
  331. if (mass != 1.0) {
  332. motion["mass"] = mass;
  333. }
  334. if (linear_velocity != Vector3()) {
  335. Array velocity_array;
  336. velocity_array.resize(3);
  337. velocity_array[0] = linear_velocity.x;
  338. velocity_array[1] = linear_velocity.y;
  339. velocity_array[2] = linear_velocity.z;
  340. motion["linearVelocity"] = velocity_array;
  341. }
  342. if (angular_velocity != Vector3()) {
  343. Array velocity_array;
  344. velocity_array.resize(3);
  345. velocity_array[0] = angular_velocity.x;
  346. velocity_array[1] = angular_velocity.y;
  347. velocity_array[2] = angular_velocity.z;
  348. motion["angularVelocity"] = velocity_array;
  349. }
  350. if (center_of_mass != Vector3()) {
  351. Array center_of_mass_array;
  352. center_of_mass_array.resize(3);
  353. center_of_mass_array[0] = center_of_mass.x;
  354. center_of_mass_array[1] = center_of_mass.y;
  355. center_of_mass_array[2] = center_of_mass.z;
  356. motion["centerOfMass"] = center_of_mass_array;
  357. }
  358. if (inertia_diagonal != Vector3()) {
  359. Array inertia_array;
  360. inertia_array.resize(3);
  361. inertia_array[0] = inertia_diagonal[0];
  362. inertia_array[1] = inertia_diagonal[1];
  363. inertia_array[2] = inertia_diagonal[2];
  364. motion["inertiaDiagonal"] = inertia_array;
  365. }
  366. if (inertia_orientation != Quaternion()) {
  367. Array inertia_array;
  368. inertia_array.resize(4);
  369. inertia_array[0] = inertia_orientation[0];
  370. inertia_array[1] = inertia_orientation[1];
  371. inertia_array[2] = inertia_orientation[2];
  372. inertia_array[3] = inertia_orientation[3];
  373. motion["inertiaDiagonal"] = inertia_array;
  374. }
  375. ret["motion"] = motion;
  376. return ret;
  377. }