test_physics.cpp 14 KB

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