test_physics_3d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*************************************************************************/
  2. /* test_physics_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "test_physics_3d.h"
  31. #include "core/math/convex_hull.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/main_loop.h"
  34. #include "core/os/os.h"
  35. #include "core/string/print_string.h"
  36. #include "core/templates/map.h"
  37. #include "servers/display_server.h"
  38. #include "servers/physics_server_3d.h"
  39. #include "servers/rendering_server.h"
  40. class TestPhysics3DMainLoop : public MainLoop {
  41. GDCLASS(TestPhysics3DMainLoop, MainLoop);
  42. enum {
  43. LINK_COUNT = 20,
  44. };
  45. RID test_cube;
  46. RID plane;
  47. RID sphere;
  48. RID light;
  49. RID camera;
  50. RID mover;
  51. RID scenario;
  52. RID space;
  53. RID character;
  54. real_t ofs_x, ofs_y;
  55. Point2 joy_direction;
  56. List<RID> bodies;
  57. Map<PhysicsServer3D::ShapeType, RID> type_shape_map;
  58. Map<PhysicsServer3D::ShapeType, RID> type_mesh_map;
  59. void body_changed_transform(Object *p_state, RID p_visual_instance) {
  60. PhysicsDirectBodyState3D *state = (PhysicsDirectBodyState3D *)p_state;
  61. RenderingServer *vs = RenderingServer::get_singleton();
  62. Transform3D t = state->get_transform();
  63. vs->instance_set_transform(p_visual_instance, t);
  64. }
  65. bool quit;
  66. protected:
  67. RID create_body(PhysicsServer3D::ShapeType p_shape, PhysicsServer3D::BodyMode p_body, const Transform3D p_location, bool p_active_default = true, const Transform3D &p_shape_xform = Transform3D()) {
  68. RenderingServer *vs = RenderingServer::get_singleton();
  69. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  70. RID mesh_instance = vs->instance_create2(type_mesh_map[p_shape], scenario);
  71. RID body = ps->body_create();
  72. ps->body_set_mode(body, p_body);
  73. ps->body_set_state(body, PhysicsServer3D::BODY_STATE_SLEEPING, !p_active_default);
  74. ps->body_set_space(body, space);
  75. ps->body_set_param(body, PhysicsServer3D::BODY_PARAM_BOUNCE, 0.0);
  76. //todo set space
  77. ps->body_add_shape(body, type_shape_map[p_shape]);
  78. ps->body_set_force_integration_callback(body, callable_mp(this, &TestPhysics3DMainLoop::body_changed_transform), mesh_instance);
  79. ps->body_set_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM, p_location);
  80. bodies.push_back(body);
  81. if (p_body == PhysicsServer3D::BODY_MODE_STATIC) {
  82. vs->instance_set_transform(mesh_instance, p_location);
  83. }
  84. return body;
  85. }
  86. RID create_world_boundary(const Plane &p_plane) {
  87. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  88. RID world_boundary_shape = ps->shape_create(PhysicsServer3D::SHAPE_WORLD_BOUNDARY);
  89. ps->shape_set_data(world_boundary_shape, p_plane);
  90. RID b = ps->body_create();
  91. ps->body_set_mode(b, PhysicsServer3D::BODY_MODE_STATIC);
  92. ps->body_set_space(b, space);
  93. ps->body_add_shape(b, world_boundary_shape);
  94. return b;
  95. }
  96. void configure_body(RID p_body, real_t p_mass, real_t p_friction, real_t p_bounce) {
  97. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  98. ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_MASS, p_mass);
  99. ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_FRICTION, p_friction);
  100. ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_BOUNCE, p_bounce);
  101. }
  102. void initialize_shapes() {
  103. RenderingServer *vs = RenderingServer::get_singleton();
  104. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  105. /* SPHERE SHAPE */
  106. RID sphere_mesh = vs->make_sphere_mesh(10, 20, 0.5);
  107. type_mesh_map[PhysicsServer3D::SHAPE_SPHERE] = sphere_mesh;
  108. RID sphere_shape = ps->shape_create(PhysicsServer3D::SHAPE_SPHERE);
  109. ps->shape_set_data(sphere_shape, 0.5);
  110. type_shape_map[PhysicsServer3D::SHAPE_SPHERE] = sphere_shape;
  111. /* BOX SHAPE */
  112. Vector<Plane> box_planes = Geometry3D::build_box_planes(Vector3(0.5, 0.5, 0.5));
  113. RID box_mesh = vs->mesh_create();
  114. Geometry3D::MeshData box_data = Geometry3D::build_convex_mesh(box_planes);
  115. vs->mesh_add_surface_from_mesh_data(box_mesh, box_data);
  116. type_mesh_map[PhysicsServer3D::SHAPE_BOX] = box_mesh;
  117. RID box_shape = ps->shape_create(PhysicsServer3D::SHAPE_BOX);
  118. ps->shape_set_data(box_shape, Vector3(0.5, 0.5, 0.5));
  119. type_shape_map[PhysicsServer3D::SHAPE_BOX] = box_shape;
  120. /* CAPSULE SHAPE */
  121. Vector<Plane> capsule_planes = Geometry3D::build_capsule_planes(0.5, 0.7, 12, Vector3::AXIS_Z);
  122. RID capsule_mesh = vs->mesh_create();
  123. Geometry3D::MeshData capsule_data = Geometry3D::build_convex_mesh(capsule_planes);
  124. vs->mesh_add_surface_from_mesh_data(capsule_mesh, capsule_data);
  125. type_mesh_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_mesh;
  126. RID capsule_shape = ps->shape_create(PhysicsServer3D::SHAPE_CAPSULE);
  127. Dictionary capsule_params;
  128. capsule_params["radius"] = 0.5;
  129. capsule_params["height"] = 1.4;
  130. ps->shape_set_data(capsule_shape, capsule_params);
  131. type_shape_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_shape;
  132. /* CONVEX SHAPE */
  133. Vector<Plane> convex_planes = Geometry3D::build_cylinder_planes(0.5, 0.7, 5, Vector3::AXIS_Z);
  134. RID convex_mesh = vs->mesh_create();
  135. Geometry3D::MeshData convex_data = Geometry3D::build_convex_mesh(convex_planes);
  136. ConvexHullComputer::convex_hull(convex_data.vertices, convex_data);
  137. vs->mesh_add_surface_from_mesh_data(convex_mesh, convex_data);
  138. type_mesh_map[PhysicsServer3D::SHAPE_CONVEX_POLYGON] = convex_mesh;
  139. RID convex_shape = ps->shape_create(PhysicsServer3D::SHAPE_CONVEX_POLYGON);
  140. ps->shape_set_data(convex_shape, convex_data.vertices);
  141. type_shape_map[PhysicsServer3D::SHAPE_CONVEX_POLYGON] = convex_shape;
  142. }
  143. void make_trimesh(Vector<Vector3> p_faces, const Transform3D &p_xform = Transform3D()) {
  144. RenderingServer *vs = RenderingServer::get_singleton();
  145. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  146. RID trimesh_shape = ps->shape_create(PhysicsServer3D::SHAPE_CONCAVE_POLYGON);
  147. Dictionary trimesh_params;
  148. trimesh_params["faces"] = p_faces;
  149. trimesh_params["backface_collision"] = false;
  150. ps->shape_set_data(trimesh_shape, trimesh_params);
  151. Vector<Vector3> normals; // for drawing
  152. for (int i = 0; i < p_faces.size() / 3; i++) {
  153. Plane p(p_faces[i * 3 + 0], p_faces[i * 3 + 1], p_faces[i * 3 + 2]);
  154. normals.push_back(p.normal);
  155. normals.push_back(p.normal);
  156. normals.push_back(p.normal);
  157. }
  158. RID trimesh_mesh = vs->mesh_create();
  159. Array d;
  160. d.resize(RS::ARRAY_MAX);
  161. d[RS::ARRAY_VERTEX] = p_faces;
  162. d[RS::ARRAY_NORMAL] = normals;
  163. vs->mesh_add_surface_from_arrays(trimesh_mesh, RS::PRIMITIVE_TRIANGLES, d);
  164. RID triins = vs->instance_create2(trimesh_mesh, scenario);
  165. RID tribody = ps->body_create();
  166. ps->body_set_mode(tribody, PhysicsServer3D::BODY_MODE_STATIC);
  167. ps->body_set_space(tribody, space);
  168. //todo set space
  169. ps->body_add_shape(tribody, trimesh_shape);
  170. Transform3D tritrans = p_xform;
  171. ps->body_set_state(tribody, PhysicsServer3D::BODY_STATE_TRANSFORM, tritrans);
  172. vs->instance_set_transform(triins, tritrans);
  173. }
  174. void make_grid(int p_width, int p_height, real_t p_cellsize, real_t p_cellheight, const Transform3D &p_xform = Transform3D()) {
  175. Vector<Vector<real_t>> grid;
  176. grid.resize(p_width);
  177. for (int i = 0; i < p_width; i++) {
  178. grid.write[i].resize(p_height);
  179. for (int j = 0; j < p_height; j++) {
  180. grid.write[i].write[j] = 1.0 + Math::random(-p_cellheight, p_cellheight);
  181. }
  182. }
  183. Vector<Vector3> faces;
  184. for (int i = 1; i < p_width; i++) {
  185. for (int j = 1; j < p_height; j++) {
  186. #define MAKE_VERTEX(m_x, m_z) \
  187. faces.push_back(Vector3((m_x - p_width / 2) * p_cellsize, grid[m_x][m_z], (m_z - p_height / 2) * p_cellsize))
  188. MAKE_VERTEX(i, j - 1);
  189. MAKE_VERTEX(i, j);
  190. MAKE_VERTEX(i - 1, j);
  191. MAKE_VERTEX(i - 1, j - 1);
  192. MAKE_VERTEX(i, j - 1);
  193. MAKE_VERTEX(i - 1, j);
  194. }
  195. }
  196. make_trimesh(faces, p_xform);
  197. }
  198. public:
  199. virtual void input_event(const Ref<InputEvent> &p_event) {
  200. Ref<InputEventMouseMotion> mm = p_event;
  201. if (mm.is_valid() && mm->get_button_mask() & 4) {
  202. ofs_y -= mm->get_relative().y / 200.0;
  203. ofs_x += mm->get_relative().x / 200.0;
  204. }
  205. if (mm.is_valid() && mm->get_button_mask() & 1) {
  206. real_t y = -mm->get_relative().y / 20.0;
  207. real_t x = mm->get_relative().x / 20.0;
  208. if (mover.is_valid()) {
  209. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  210. Transform3D t = ps->body_get_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM);
  211. t.origin += Vector3(x, y, 0);
  212. ps->body_set_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM, t);
  213. }
  214. }
  215. }
  216. virtual void request_quit() {
  217. quit = true;
  218. }
  219. virtual void initialize() override {
  220. ofs_x = ofs_y = 0;
  221. initialize_shapes();
  222. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  223. space = ps->space_create();
  224. ps->space_set_active(space, true);
  225. RenderingServer *vs = RenderingServer::get_singleton();
  226. /* LIGHT */
  227. RID lightaux = vs->directional_light_create();
  228. scenario = vs->scenario_create();
  229. vs->light_set_shadow(lightaux, true);
  230. light = vs->instance_create2(lightaux, scenario);
  231. Transform3D t;
  232. t.rotate(Vector3(1.0, 0, 0), 0.6);
  233. vs->instance_set_transform(light, t);
  234. /* CAMERA */
  235. camera = vs->camera_create();
  236. RID viewport = vs->viewport_create();
  237. Size2i screen_size = DisplayServer::get_singleton()->window_get_size();
  238. vs->viewport_set_size(viewport, screen_size.x, screen_size.y);
  239. vs->viewport_attach_to_screen(viewport, Rect2(Vector2(), screen_size));
  240. vs->viewport_set_active(viewport, true);
  241. vs->viewport_attach_camera(viewport, camera);
  242. vs->viewport_set_scenario(viewport, scenario);
  243. vs->camera_set_perspective(camera, 60, 0.1, 40.0);
  244. vs->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 9, 12)));
  245. Transform3D gxf;
  246. gxf.basis.scale(Vector3(1.4, 0.4, 1.4));
  247. gxf.origin = Vector3(-2, 1, -2);
  248. make_grid(5, 5, 2.5, 1, gxf);
  249. test_fall();
  250. quit = false;
  251. }
  252. virtual bool physics_process(double p_time) override {
  253. if (mover.is_valid()) {
  254. static real_t joy_speed = 10;
  255. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  256. Transform3D t = ps->body_get_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM);
  257. t.origin += Vector3(joy_speed * joy_direction.x * p_time, -joy_speed * joy_direction.y * p_time, 0);
  258. ps->body_set_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM, t);
  259. };
  260. Transform3D cameratr;
  261. cameratr.rotate(Vector3(0, 1, 0), ofs_x);
  262. cameratr.rotate(Vector3(1, 0, 0), -ofs_y);
  263. cameratr.translate(Vector3(0, 2, 8));
  264. RenderingServer *vs = RenderingServer::get_singleton();
  265. vs->camera_set_transform(camera, cameratr);
  266. return quit;
  267. }
  268. virtual void finalize() override {
  269. }
  270. void test_joint() {
  271. }
  272. void test_hinge() {
  273. }
  274. void test_character() {
  275. RenderingServer *vs = RenderingServer::get_singleton();
  276. PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
  277. Vector<Plane> capsule_planes = Geometry3D::build_capsule_planes(0.5, 1, 12, 5, Vector3::AXIS_Y);
  278. RID capsule_mesh = vs->mesh_create();
  279. Geometry3D::MeshData capsule_data = Geometry3D::build_convex_mesh(capsule_planes);
  280. vs->mesh_add_surface_from_mesh_data(capsule_mesh, capsule_data);
  281. type_mesh_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_mesh;
  282. RID capsule_shape = ps->shape_create(PhysicsServer3D::SHAPE_CAPSULE);
  283. Dictionary capsule_params;
  284. capsule_params["radius"] = 0.5;
  285. capsule_params["height"] = 1;
  286. Transform3D shape_xform;
  287. shape_xform.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  288. //shape_xform.origin=Vector3(1,1,1);
  289. ps->shape_set_data(capsule_shape, capsule_params);
  290. RID mesh_instance = vs->instance_create2(capsule_mesh, scenario);
  291. character = ps->body_create();
  292. ps->body_set_mode(character, PhysicsServer3D::BODY_MODE_DYNAMIC_LINEAR);
  293. ps->body_set_space(character, space);
  294. //todo add space
  295. ps->body_add_shape(character, capsule_shape);
  296. ps->body_set_force_integration_callback(character, callable_mp(this, &TestPhysics3DMainLoop::body_changed_transform), mesh_instance);
  297. ps->body_set_state(character, PhysicsServer3D::BODY_STATE_TRANSFORM, Transform3D(Basis(), Vector3(-2, 5, -2)));
  298. bodies.push_back(character);
  299. }
  300. void test_fall() {
  301. for (int i = 0; i < 35; i++) {
  302. static const PhysicsServer3D::ShapeType shape_idx[] = {
  303. PhysicsServer3D::SHAPE_CAPSULE,
  304. PhysicsServer3D::SHAPE_BOX,
  305. PhysicsServer3D::SHAPE_SPHERE,
  306. PhysicsServer3D::SHAPE_CONVEX_POLYGON
  307. };
  308. PhysicsServer3D::ShapeType type = shape_idx[i % 4];
  309. Transform3D t;
  310. t.origin = Vector3(0.0 * i, 3.5 + 1.1 * i, 0.7 + 0.0 * i);
  311. t.basis.rotate(Vector3(0.2, -1, 0), Math_PI / 2 * 0.6);
  312. create_body(type, PhysicsServer3D::BODY_MODE_DYNAMIC, t);
  313. }
  314. create_world_boundary(Plane(Vector3(0, 1, 0), -1));
  315. }
  316. void test_activate() {
  317. create_body(PhysicsServer3D::SHAPE_BOX, PhysicsServer3D::BODY_MODE_DYNAMIC, Transform3D(Basis(), Vector3(0, 2, 0)), true);
  318. create_world_boundary(Plane(Vector3(0, 1, 0), -1));
  319. }
  320. virtual bool process(double p_time) override {
  321. return false;
  322. }
  323. TestPhysics3DMainLoop() {
  324. }
  325. };
  326. namespace TestPhysics3D {
  327. MainLoop *test() {
  328. return memnew(TestPhysics3DMainLoop);
  329. }
  330. } // namespace TestPhysics3D