test_physics_2d.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*************************************************************************/
  2. /* test_physics_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_2d.h"
  31. #include "core/map.h"
  32. #include "core/os/main_loop.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include "scene/resources/texture.h"
  36. #include "servers/physics_2d_server.h"
  37. #include "servers/visual_server.h"
  38. static const unsigned char convex_png[] = {
  39. 0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa, 0x0, 0x0, 0x0, 0xd, 0x49, 0x48, 0x44, 0x52, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x40, 0x8, 0x6, 0x0, 0x0, 0x0, 0xaa, 0x69, 0x71, 0xde, 0x0, 0x0, 0x0, 0x1, 0x73, 0x52, 0x47, 0x42, 0x0, 0xae, 0xce, 0x1c, 0xe9, 0x0, 0x0, 0x0, 0x6, 0x62, 0x4b, 0x47, 0x44, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf9, 0x43, 0xbb, 0x7f, 0x0, 0x0, 0x0, 0x9, 0x70, 0x48, 0x59, 0x73, 0x0, 0x0, 0xb, 0x13, 0x0, 0x0, 0xb, 0x13, 0x1, 0x0, 0x9a, 0x9c, 0x18, 0x0, 0x0, 0x0, 0x7, 0x74, 0x49, 0x4d, 0x45, 0x7, 0xdb, 0x6, 0xa, 0x3, 0x13, 0x31, 0x66, 0xa7, 0xac, 0x79, 0x0, 0x0, 0x4, 0xef, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0xed, 0x9b, 0xdd, 0x4e, 0x2a, 0x57, 0x14, 0xc7, 0xf7, 0x1e, 0xc0, 0x19, 0x38, 0x32, 0x80, 0xa, 0x6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0x31, 0x36, 0x36, 0x96, 0x65, 0x59, 0x36, 0x29, 0x49, 0x92, 0xb7, 0xbd, 0xbd, 0xfd, 0x9f, 0x72, 0xda, 0xf9, 0xd1, 0x1, 0xa8, 0x1, 0x93, 0xcf, 0xe7, 0xa9, 0x93, 0x93, 0x13, 0x1b, 0x4d, 0xd3, 0x9f, 0xb, 0x82, 0x60, 0xf5, 0x7a, 0xbd, 0xd9, 0x54, 0x2a, 0xe5, 0xcc, 0x64, 0x32, 0xe, 0xb9, 0x6e, 0xb9, 0x16, 0x8c, 0x31, 0x2e, 0xda, 0x6c, 0xb6, 0xc8, 0xd0, 0xd0, 0x10, 0x65, 0xb3, 0xd9, 0x92, 0x95, 0xa8, 0x6e, 0xc5, 0x0, 0xa8, 0xe9, 0x96, 0x68, 0x34, 0x6a, 0xdd, 0xdf, 0xdf, 0x6f, 0x76, 0xb9, 0x5c, 0x9f, 0x89, 0xa2, 0x58, 0xbf, 0xb8, 0xb8, 0x8, 0x26, 0x93, 0x29, 0x3b, 0x3c, 0x3c, 0x8c, 0xed, 0x76, 0x7b, 0xd2, 0x68, 0x34, 0xfe, 0xd0, 0xd8, 0xd8, 0x98, 0xae, 0xb6, 0xe0, 0x8a, 0x1, 0x50, 0xb, 0xe6, 0xa9, 0x5, 0xbf, 0x9c, 0x97, 0xf3, 0xff, 0xf3, 0x2f, 0x6a, 0x82, 0x7f, 0xf6, 0x4e, 0xca, 0x1b, 0xf5, 0x0, 0x0, 0x0, 0x0, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
  40. };
  41. class TestPhysics2DMainLoop : public MainLoop {
  42. GDCLASS(TestPhysics2DMainLoop, MainLoop);
  43. RID circle_img;
  44. RID circle_shape;
  45. RID space;
  46. RID canvas;
  47. RID ray;
  48. RID ray_query;
  49. Transform2D view_xform;
  50. Vector2 ray_from, ray_to;
  51. struct BodyShapeData {
  52. RID image;
  53. RID shape;
  54. };
  55. BodyShapeData body_shape_data[8];
  56. void _create_body_shape_data() {
  57. VisualServer *vs = VisualServer::get_singleton();
  58. Physics2DServer *ps = Physics2DServer::get_singleton();
  59. // SEGMENT
  60. {
  61. PoolVector<uint8_t> pixels;
  62. pixels.resize(32 * 2 * 2);
  63. for (int i = 0; i < 2; i++) {
  64. for (int j = 0; j < 32; j++) {
  65. pixels.set(i * 32 * 2 + j * 2 + 0, (j == 0) ? 255 : 0);
  66. pixels.set(i * 32 * 2 + j * 2 + 1, 255);
  67. }
  68. }
  69. Ref<Image> image = memnew(Image(32, 2, 0, Image::FORMAT_LA8, pixels));
  70. body_shape_data[Physics2DServer::SHAPE_SEGMENT].image = vs->texture_create_from_image(image);
  71. RID segment_shape = ps->segment_shape_create();
  72. Rect2 sg(Point2(-16, 0), Point2(16, 0));
  73. ps->shape_set_data(segment_shape, sg);
  74. body_shape_data[Physics2DServer::SHAPE_SEGMENT].shape = segment_shape;
  75. }
  76. // CIRCLE
  77. {
  78. PoolVector<uint8_t> pixels;
  79. pixels.resize(32 * 32 * 2);
  80. for (int i = 0; i < 32; i++) {
  81. for (int j = 0; j < 32; j++) {
  82. bool black = Vector2(i - 16, j - 16).length_squared() < 16 * 16;
  83. pixels.set(i * 32 * 2 + j * 2 + 0, (i == 16 || j == 16) ? 255 : 0);
  84. pixels.set(i * 32 * 2 + j * 2 + 1, black ? 255 : 0);
  85. }
  86. }
  87. Ref<Image> image = memnew(Image(32, 32, 0, Image::FORMAT_LA8, pixels));
  88. body_shape_data[Physics2DServer::SHAPE_CIRCLE].image = vs->texture_create_from_image(image);
  89. RID circle_shape = ps->circle_shape_create();
  90. ps->shape_set_data(circle_shape, 16);
  91. body_shape_data[Physics2DServer::SHAPE_CIRCLE].shape = circle_shape;
  92. }
  93. // BOX
  94. {
  95. PoolVector<uint8_t> pixels;
  96. pixels.resize(32 * 32 * 2);
  97. for (int i = 0; i < 32; i++) {
  98. for (int j = 0; j < 32; j++) {
  99. bool black = i > 0 && i < 31 && j > 0 && j < 31;
  100. pixels.set(i * 32 * 2 + j * 2 + 0, black ? 0 : 255);
  101. pixels.set(i * 32 * 2 + j * 2 + 1, 255);
  102. }
  103. }
  104. Ref<Image> image = memnew(Image(32, 32, 0, Image::FORMAT_LA8, pixels));
  105. body_shape_data[Physics2DServer::SHAPE_RECTANGLE].image = vs->texture_create_from_image(image);
  106. RID rectangle_shape = ps->rectangle_shape_create();
  107. ps->shape_set_data(rectangle_shape, Vector2(16, 16));
  108. body_shape_data[Physics2DServer::SHAPE_RECTANGLE].shape = rectangle_shape;
  109. }
  110. // CAPSULE
  111. {
  112. PoolVector<uint8_t> pixels;
  113. pixels.resize(32 * 64 * 2);
  114. for (int i = 0; i < 64; i++) {
  115. for (int j = 0; j < 32; j++) {
  116. int si = i > 48 ? i - 32 : (i < 16 ? i : 16);
  117. bool black = Vector2(si - 16, j - 16).length_squared() < 16 * 16;
  118. pixels.set(i * 32 * 2 + j * 2 + 0, (i == 16 || j == 16 || i == 48) ? 255 : 0);
  119. pixels.set(i * 32 * 2 + j * 2 + 1, black ? 255 : 0);
  120. }
  121. }
  122. Ref<Image> image = memnew(Image(32, 64, 0, Image::FORMAT_LA8, pixels));
  123. body_shape_data[Physics2DServer::SHAPE_CAPSULE].image = vs->texture_create_from_image(image);
  124. RID capsule_shape = ps->capsule_shape_create();
  125. ps->shape_set_data(capsule_shape, Vector2(16, 32));
  126. body_shape_data[Physics2DServer::SHAPE_CAPSULE].shape = capsule_shape;
  127. }
  128. // CONVEX
  129. {
  130. Ref<Image> image = memnew(Image(convex_png));
  131. body_shape_data[Physics2DServer::SHAPE_CONVEX_POLYGON].image = vs->texture_create_from_image(image);
  132. RID convex_polygon_shape = ps->convex_polygon_shape_create();
  133. PoolVector<Vector2> arr;
  134. Point2 sb(32, 32);
  135. arr.push_back(Point2(20, 3) - sb);
  136. arr.push_back(Point2(58, 23) - sb);
  137. arr.push_back(Point2(55, 54) - sb);
  138. arr.push_back(Point2(27, 60) - sb);
  139. arr.push_back(Point2(5, 56) - sb);
  140. arr.push_back(Point2(4, 20) - sb);
  141. arr.push_back(Point2(11, 7) - sb);
  142. ps->shape_set_data(convex_polygon_shape, arr);
  143. body_shape_data[Physics2DServer::SHAPE_CONVEX_POLYGON].shape = convex_polygon_shape;
  144. }
  145. }
  146. void _do_ray_query() {
  147. /*
  148. Physics2DServer *ps = Physics2DServer::get_singleton();
  149. ps->query_intersection_segment(ray_query,ray_from,ray_to);
  150. */
  151. }
  152. protected:
  153. void input_event(const Ref<InputEvent> &p_event) {
  154. Ref<InputEventMouseButton> mb = p_event;
  155. if (mb.is_valid()) {
  156. if (mb->is_pressed()) {
  157. Point2 p(mb->get_position().x, mb->get_position().y);
  158. if (mb->get_button_index() == 1) {
  159. ray_to = p;
  160. _do_ray_query();
  161. } else if (mb->get_button_index() == 2) {
  162. ray_from = p;
  163. _do_ray_query();
  164. }
  165. }
  166. }
  167. Ref<InputEventMouseMotion> mm = p_event;
  168. if (mm.is_valid()) {
  169. Point2 p = mm->get_position();
  170. if (mm->get_button_mask() & BUTTON_MASK_LEFT) {
  171. ray_to = p;
  172. _do_ray_query();
  173. } else if (mm->get_button_mask() & BUTTON_MASK_RIGHT) {
  174. ray_from = p;
  175. _do_ray_query();
  176. }
  177. }
  178. }
  179. RID _add_body(Physics2DServer::ShapeType p_shape, const Transform2D &p_xform) {
  180. VisualServer *vs = VisualServer::get_singleton();
  181. Physics2DServer *ps = Physics2DServer::get_singleton();
  182. RID body = ps->body_create();
  183. ps->body_add_shape(body, body_shape_data[p_shape].shape);
  184. ps->body_set_space(body, space);
  185. ps->body_set_continuous_collision_detection_mode(body, Physics2DServer::CCD_MODE_CAST_SHAPE);
  186. ps->body_set_state(body, Physics2DServer::BODY_STATE_TRANSFORM, p_xform);
  187. //print_line("add body with xform: "+p_xform);
  188. RID sprite = vs->canvas_item_create();
  189. vs->canvas_item_set_parent(sprite, canvas);
  190. vs->canvas_item_set_transform(sprite, p_xform);
  191. Size2 imgsize(vs->texture_get_width(body_shape_data[p_shape].image), vs->texture_get_height(body_shape_data[p_shape].image));
  192. vs->canvas_item_add_texture_rect(sprite, Rect2(-imgsize / 2.0, imgsize), body_shape_data[p_shape].image);
  193. ps->body_set_force_integration_callback(body, this, "_body_moved", sprite);
  194. //RID q = ps->query_create(this,"_body_moved",sprite);
  195. //ps->query_body_state(q,body);
  196. return body;
  197. }
  198. void _add_plane(const Vector2 &p_normal, real_t p_d) {
  199. Physics2DServer *ps = Physics2DServer::get_singleton();
  200. Array arr;
  201. arr.push_back(p_normal);
  202. arr.push_back(p_d);
  203. RID plane = ps->line_shape_create();
  204. ps->shape_set_data(plane, arr);
  205. RID plane_body = ps->body_create();
  206. ps->body_set_mode(plane_body, Physics2DServer::BODY_MODE_STATIC);
  207. ps->body_set_space(plane_body, space);
  208. ps->body_add_shape(plane_body, plane);
  209. }
  210. void _add_concave(const Vector<Vector2> &p_points, const Transform2D &p_xform = Transform2D()) {
  211. Physics2DServer *ps = Physics2DServer::get_singleton();
  212. VisualServer *vs = VisualServer::get_singleton();
  213. RID concave = ps->concave_polygon_shape_create();
  214. ps->shape_set_data(concave, p_points);
  215. RID body = ps->body_create();
  216. ps->body_set_mode(body, Physics2DServer::BODY_MODE_STATIC);
  217. ps->body_set_space(body, space);
  218. ps->body_add_shape(body, concave);
  219. ps->body_set_state(body, Physics2DServer::BODY_STATE_TRANSFORM, p_xform);
  220. RID sprite = vs->canvas_item_create();
  221. vs->canvas_item_set_parent(sprite, canvas);
  222. vs->canvas_item_set_transform(sprite, p_xform);
  223. for (int i = 0; i < p_points.size(); i += 2) {
  224. vs->canvas_item_add_line(sprite, p_points[i], p_points[i + 1], Color(0, 0, 0), 2);
  225. }
  226. }
  227. void _body_moved(Object *p_state, RID p_sprite) {
  228. Physics2DDirectBodyState *state = (Physics2DDirectBodyState *)p_state;
  229. VisualServer::get_singleton()->canvas_item_set_transform(p_sprite, state->get_transform());
  230. }
  231. void _ray_query_callback(const RID &p_rid, ObjectID p_id, int p_shape, const Vector2 &p_point, const Vector2 &p_normal) {
  232. Vector2 ray_end;
  233. if (p_rid.is_valid()) {
  234. ray_end = p_point;
  235. } else {
  236. ray_end = ray_to;
  237. }
  238. VisualServer *vs = VisualServer::get_singleton();
  239. vs->canvas_item_clear(ray);
  240. vs->canvas_item_add_line(ray, ray_from, ray_end, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2);
  241. if (p_rid.is_valid())
  242. vs->canvas_item_add_line(ray, ray_end, ray_end + p_normal * 20, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2);
  243. }
  244. static void _bind_methods() {
  245. ClassDB::bind_method(D_METHOD("_body_moved"), &TestPhysics2DMainLoop::_body_moved);
  246. ClassDB::bind_method(D_METHOD("_ray_query_callback"), &TestPhysics2DMainLoop::_ray_query_callback);
  247. }
  248. public:
  249. virtual void init() {
  250. VisualServer *vs = VisualServer::get_singleton();
  251. Physics2DServer *ps = Physics2DServer::get_singleton();
  252. space = ps->space_create();
  253. ps->space_set_active(space, true);
  254. ps->set_active(true);
  255. ps->area_set_param(space, Physics2DServer::AREA_PARAM_GRAVITY_VECTOR, Vector2(0, 1));
  256. ps->area_set_param(space, Physics2DServer::AREA_PARAM_GRAVITY, 98);
  257. {
  258. RID vp = vs->viewport_create();
  259. canvas = vs->canvas_create();
  260. Size2i screen_size = OS::get_singleton()->get_window_size();
  261. vs->viewport_attach_canvas(vp, canvas);
  262. vs->viewport_set_size(vp, screen_size.x, screen_size.y);
  263. vs->viewport_attach_to_screen(vp, Rect2(Vector2(), screen_size));
  264. vs->viewport_set_active(vp, true);
  265. Transform2D smaller;
  266. //smaller.scale(Vector2(0.6,0.6));
  267. //smaller.elements[2]=Vector2(100,0);
  268. //view_xform = smaller;
  269. vs->viewport_set_canvas_transform(vp, canvas, view_xform);
  270. }
  271. ray = vs->canvas_item_create();
  272. vs->canvas_item_set_parent(ray, canvas);
  273. //ray_query = ps->query_create(this,"_ray_query_callback",Variant());
  274. //ps->query_intersection(ray_query,space);
  275. _create_body_shape_data();
  276. for (int i = 0; i < 32; i++) {
  277. Physics2DServer::ShapeType types[4] = {
  278. Physics2DServer::SHAPE_CIRCLE,
  279. Physics2DServer::SHAPE_CAPSULE,
  280. Physics2DServer::SHAPE_RECTANGLE,
  281. Physics2DServer::SHAPE_CONVEX_POLYGON,
  282. };
  283. Physics2DServer::ShapeType type = types[i % 4];
  284. //type=Physics2DServer::SHAPE_SEGMENT;
  285. _add_body(type, Transform2D(i * 0.8, Point2(152 + i * 40, 100 - 40 * i)));
  286. /*
  287. if (i==0)
  288. ps->body_set_mode(b,Physics2DServer::BODY_MODE_STATIC);
  289. */
  290. }
  291. //RID b= _add_body(Physics2DServer::SHAPE_CIRCLE,Transform2D(0,Point2(101,140)));
  292. //ps->body_set_mode(b,Physics2DServer::BODY_MODE_STATIC);
  293. Point2 prev;
  294. Vector<Point2> parr;
  295. for (int i = 0; i < 30; i++) {
  296. Point2 p(i * 60, Math::randf() * 70 + 340);
  297. if (i > 0) {
  298. parr.push_back(prev);
  299. parr.push_back(p);
  300. }
  301. prev = p;
  302. }
  303. _add_concave(parr);
  304. //_add_plane(Vector2(0.0,-1).normalized(),-300);
  305. //_add_plane(Vector2(1,0).normalized(),50);
  306. //_add_plane(Vector2(-1,0).normalized(),-600);
  307. }
  308. virtual bool idle(float p_time) {
  309. return false;
  310. }
  311. virtual void finish() {
  312. }
  313. TestPhysics2DMainLoop() {}
  314. };
  315. namespace TestPhysics2D {
  316. MainLoop *test() {
  317. return memnew(TestPhysics2DMainLoop);
  318. }
  319. } // namespace TestPhysics2D