test_physics.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*************************************************************************/
  2. /* test_physics.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "test_physics.h"
  30. #include "servers/visual_server.h"
  31. #include "servers/physics_server.h"
  32. #include "os/main_loop.h"
  33. #include "math_funcs.h"
  34. #include "print_string.h"
  35. #include "map.h"
  36. #include "os/os.h"
  37. #include "quick_hull.h"
  38. class TestPhysicsMainLoop : public MainLoop {
  39. OBJ_TYPE( TestPhysicsMainLoop, MainLoop );
  40. enum {
  41. LINK_COUNT = 20,
  42. };
  43. RID test_cube;
  44. RID plane;
  45. RID sphere;
  46. RID light;
  47. RID camera;
  48. RID mover;
  49. RID scenario;
  50. RID space;
  51. RID character;
  52. float ofs_x,ofs_y;
  53. Point2 joy_direction;
  54. List<RID> bodies;
  55. Map<PhysicsServer::ShapeType,RID> type_shape_map;
  56. Map<PhysicsServer::ShapeType,RID> type_mesh_map;
  57. void body_changed_transform(Object *p_state, RID p_visual_instance) {
  58. PhysicsDirectBodyState *state = (PhysicsDirectBodyState*)p_state;
  59. VisualServer *vs=VisualServer::get_singleton();
  60. Transform t=state->get_transform();
  61. //t.basis.scale( Vector3(1.0,0.5,0.2) );
  62. vs->instance_set_transform(p_visual_instance,t);
  63. }
  64. bool quit;
  65. protected:
  66. static void _bind_methods() {
  67. ObjectTypeDB::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. RID sphere_material = vs->fixed_material_create();
  108. //vs->material_set_flag( sphere_material, VisualServer::MATERIAL_FLAG_WIREFRAME, true );
  109. vs->fixed_material_set_param( sphere_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.7,0.8,3.0) );
  110. vs->mesh_surface_set_material( sphere_mesh, 0, sphere_material );
  111. type_mesh_map[PhysicsServer::SHAPE_SPHERE]=sphere_mesh;
  112. RID sphere_shape=ps->shape_create(PhysicsServer::SHAPE_SPHERE);
  113. ps->shape_set_data( sphere_shape, 0.5 );
  114. type_shape_map[PhysicsServer::SHAPE_SPHERE]=sphere_shape;
  115. /* BOX SHAPE */
  116. DVector<Plane> box_planes = Geometry::build_box_planes(Vector3(0.5,0.5,0.5));
  117. RID box_material = vs->fixed_material_create();
  118. vs->fixed_material_set_param( box_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.2,0.2) );
  119. RID box_mesh = vs->mesh_create();
  120. Geometry::MeshData box_data = Geometry::build_convex_mesh(box_planes);
  121. vs->mesh_add_surface_from_mesh_data(box_mesh,box_data);
  122. vs->mesh_surface_set_material( box_mesh, 0, box_material );
  123. type_mesh_map[PhysicsServer::SHAPE_BOX]=box_mesh;
  124. RID box_shape=ps->shape_create(PhysicsServer::SHAPE_BOX);
  125. ps->shape_set_data( box_shape, Vector3(0.5,0.5,0.5) );
  126. type_shape_map[PhysicsServer::SHAPE_BOX]=box_shape;
  127. /* CAPSULE SHAPE */
  128. DVector<Plane> capsule_planes = Geometry::build_capsule_planes(0.5,0.7,12,Vector3::AXIS_Z);
  129. RID capsule_material = vs->fixed_material_create();
  130. vs->fixed_material_set_param( capsule_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) );
  131. RID capsule_mesh = vs->mesh_create();
  132. Geometry::MeshData capsule_data = Geometry::build_convex_mesh(capsule_planes);
  133. vs->mesh_add_surface_from_mesh_data(capsule_mesh,capsule_data);
  134. vs->mesh_surface_set_material( capsule_mesh, 0, capsule_material );
  135. type_mesh_map[PhysicsServer::SHAPE_CAPSULE]=capsule_mesh;
  136. RID capsule_shape=ps->shape_create(PhysicsServer::SHAPE_CAPSULE);
  137. Dictionary capsule_params;
  138. capsule_params["radius"]=0.5;
  139. capsule_params["height"]=1.4;
  140. ps->shape_set_data( capsule_shape, capsule_params );
  141. type_shape_map[PhysicsServer::SHAPE_CAPSULE]=capsule_shape;
  142. /* CONVEX SHAPE */
  143. DVector<Plane> convex_planes = Geometry::build_cylinder_planes(0.5,0.7,5,Vector3::AXIS_Z);
  144. RID convex_material = vs->fixed_material_create();
  145. vs->fixed_material_set_param( convex_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.8,0.2,0.9));
  146. RID convex_mesh = vs->mesh_create();
  147. Geometry::MeshData convex_data = Geometry::build_convex_mesh(convex_planes);
  148. QuickHull::build(convex_data.vertices,convex_data);
  149. vs->mesh_add_surface_from_mesh_data(convex_mesh,convex_data);
  150. vs->mesh_surface_set_material( convex_mesh, 0, convex_material );
  151. type_mesh_map[PhysicsServer::SHAPE_CONVEX_POLYGON]=convex_mesh;
  152. RID convex_shape=ps->shape_create(PhysicsServer::SHAPE_CONVEX_POLYGON);
  153. ps->shape_set_data( convex_shape, convex_data.vertices );
  154. type_shape_map[PhysicsServer::SHAPE_CONVEX_POLYGON]=convex_shape;
  155. }
  156. void make_trimesh(Vector<Vector3> p_faces,const Transform& p_xform=Transform()) {
  157. VisualServer *vs=VisualServer::get_singleton();
  158. PhysicsServer * ps = PhysicsServer::get_singleton();
  159. RID trimesh_shape = ps->shape_create(PhysicsServer::SHAPE_CONCAVE_POLYGON);
  160. ps->shape_set_data(trimesh_shape, p_faces);
  161. p_faces=ps->shape_get_data(trimesh_shape); // optimized one
  162. Vector<Vector3> normals; // for drawing
  163. for (int i=0;i<p_faces.size()/3;i++) {
  164. Plane p( p_faces[i*3+0],p_faces[i*3+1], p_faces[i*3+2] );
  165. normals.push_back(p.normal);
  166. normals.push_back(p.normal);
  167. normals.push_back(p.normal);
  168. }
  169. RID trimesh_mesh = vs->mesh_create();
  170. Array d;
  171. d.resize(VS::ARRAY_MAX);
  172. d[VS::ARRAY_VERTEX]=p_faces;
  173. d[VS::ARRAY_NORMAL]=normals;
  174. vs->mesh_add_surface(trimesh_mesh, VS::PRIMITIVE_TRIANGLES, d );
  175. RID trimesh_mat = vs->fixed_material_create();
  176. vs->fixed_material_set_param( trimesh_mat, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.5,0.8));
  177. //vs->material_set_flag( trimesh_mat, VisualServer::MATERIAL_FLAG_UNSHADED,true);
  178. vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_mat );
  179. RID triins = vs->instance_create2(trimesh_mesh,scenario);
  180. RID tribody = ps->body_create( PhysicsServer::BODY_MODE_STATIC);
  181. ps->body_set_space(tribody,space);
  182. //todo set space
  183. ps->body_add_shape(tribody, trimesh_shape);
  184. Transform tritrans = p_xform;
  185. ps->body_set_state( tribody, PhysicsServer::BODY_STATE_TRANSFORM, tritrans );
  186. vs->instance_set_transform( triins, tritrans );
  187. //RID trimesh_material = vs->fixed_material_create();
  188. //vs->material_generate( trimesh_material, Color(0.2,0.4,0.6) );
  189. //vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_material );
  190. }
  191. void make_grid(int p_width,int p_height,float p_cellsize,float p_cellheight,const Transform& p_xform=Transform()) {
  192. Vector< Vector< float > > grid;
  193. grid.resize(p_width);
  194. for (int i=0;i<p_width;i++) {
  195. grid[i].resize(p_height);
  196. for (int j=0;j<p_height;j++) {
  197. grid[i][j]=1.0+Math::random(-p_cellheight, p_cellheight );
  198. }
  199. }
  200. Vector<Vector3> faces;
  201. for (int i=1;i<p_width;i++) {
  202. for (int j=1;j<p_height;j++) {
  203. #define MAKE_VERTEX(m_x,m_z)\
  204. faces.push_back( Vector3( (m_x-p_width/2)*p_cellsize, grid[m_x][m_z], (m_z-p_height/2)*p_cellsize ) )
  205. MAKE_VERTEX(i,j-1);
  206. MAKE_VERTEX(i,j);
  207. MAKE_VERTEX(i-1,j);
  208. MAKE_VERTEX(i-1,j-1);
  209. MAKE_VERTEX(i,j-1);
  210. MAKE_VERTEX(i-1,j);
  211. }
  212. }
  213. make_trimesh(faces,p_xform);
  214. }
  215. public:
  216. virtual void input_event(const InputEvent& p_event) {
  217. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&4) {
  218. ofs_y-=p_event.mouse_motion.relative_y/200.0;
  219. ofs_x+=p_event.mouse_motion.relative_x/200.0;
  220. }
  221. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&1) {
  222. float y=-p_event.mouse_motion.relative_y/20.0;
  223. float x=p_event.mouse_motion.relative_x/20.0;
  224. if (mover.is_valid()) {
  225. PhysicsServer * ps = PhysicsServer::get_singleton();
  226. Transform t = ps->body_get_state(mover,PhysicsServer::BODY_STATE_TRANSFORM);
  227. t.origin+=Vector3(x,y,0);
  228. ps->body_set_state(mover,PhysicsServer::BODY_STATE_TRANSFORM,t);
  229. }
  230. }
  231. if (p_event.type == InputEvent::JOYSTICK_MOTION) {
  232. if (p_event.joy_motion.axis == 0) {
  233. joy_direction.x = p_event.joy_motion.axis_value;
  234. };
  235. if (p_event.joy_motion.axis == 1) {
  236. joy_direction.y = p_event.joy_motion.axis_value;
  237. };
  238. };
  239. }
  240. virtual void request_quit() {
  241. quit=true;
  242. }
  243. virtual void init() {
  244. ofs_x=ofs_y=0;
  245. init_shapes();
  246. PhysicsServer *ps = PhysicsServer::get_singleton();
  247. space=ps->space_create();
  248. ps->space_set_active(space,true);
  249. VisualServer *vs=VisualServer::get_singleton();
  250. /* LIGHT */
  251. RID lightaux = vs->light_create( VisualServer::LIGHT_DIRECTIONAL );
  252. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
  253. scenario = vs->scenario_create();
  254. vs->light_set_shadow(lightaux,true);
  255. light = vs->instance_create2( lightaux,scenario );
  256. Transform t;
  257. t.rotate(Vector3(1.0,0,0),0.6);
  258. vs->instance_set_transform(light,t);
  259. /* CAMERA */
  260. camera = vs->camera_create();
  261. RID viewport = vs->viewport_create();
  262. vs->viewport_attach_camera( viewport, camera );
  263. vs->viewport_attach_to_screen(viewport);
  264. vs->viewport_set_scenario( viewport, scenario );
  265. vs->camera_set_perspective(camera,60,0.1,40.0);
  266. vs->camera_set_transform(camera,Transform( Matrix3(), Vector3(0,9,12)));
  267. //vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);
  268. Transform gxf;
  269. gxf.basis.scale(Vector3(1.4,0.4,1.4));
  270. gxf.origin=Vector3(-2,1,-2);
  271. make_grid(5,5,2.5,1,gxf);
  272. // create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_STATIC,gxf);
  273. //create_static_plane( Plane( Vector3(0,1,0), -2) );
  274. // test_joint();
  275. test_fall();
  276. //test_joint();
  277. /*
  278. Vector<Vector3> faces;
  279. faces.push_back( Vector3(10,0,-5) );
  280. faces.push_back( Vector3(0,0,10) );
  281. faces.push_back( Vector3(-10,-0.2,-5) );
  282. make_trimesh(faces);
  283. */
  284. /* Make Trimesh */
  285. quit=false;
  286. return;
  287. #if 0
  288. #define GRID_SIZE 5
  289. float grid[GRID_SIZE][GRID_SIZE];
  290. for (int i=0;i<GRID_SIZE;i++) {
  291. for (int j=0;j<GRID_SIZE;j++) {
  292. grid[j][i]=Math::random(0.0, 1.0 );
  293. }
  294. }
  295. Vector<Vector3> faces;
  296. for (int i=1;i<GRID_SIZE;i++) {
  297. for (int j=1;j<GRID_SIZE;j++) {
  298. #define MAKE_VERTEX(m_x,m_z)\
  299. faces.push_back( Vector3( m_x-GRID_SIZE/2.0, grid[m_x][m_z], m_z-GRID_SIZE/2.0 )*3.0 )
  300. MAKE_VERTEX(i,j-1);
  301. MAKE_VERTEX(i,j);
  302. MAKE_VERTEX(i-1,j);
  303. MAKE_VERTEX(i-1,j-1);
  304. MAKE_VERTEX(i,j-1);
  305. MAKE_VERTEX(i-1,j);
  306. }
  307. }
  308. /*
  309. faces.clear();
  310. faces.push_back( Vector3(0,0,-5) );
  311. faces.push_back( Vector3(1,0,-1) );
  312. faces.push_back( Vector3(-1,-0,-1) );
  313. */
  314. RID trimesh_shape = ps->shape_create();
  315. ps->shape_set_data(trimesh_shape, PhysicsServer::SHAPE_CONCAVE_POLYGON,faces);
  316. faces=ps->shape_get_shape(trimesh_shape, 0);
  317. Vector<Vector3> normals; // for drawing
  318. for (int i=0;i<faces.size()/3;i++) {
  319. Plane p( faces[i*3+0],faces[i*3+1], faces[i*3+2] );
  320. normals.push_back(p.normal);
  321. normals.push_back(p.normal);
  322. normals.push_back(p.normal);
  323. }
  324. RID trimesh_mesh = vs->mesh_create();
  325. vs->mesh_add_surface(trimesh_mesh, VS::PRIMITIVE_TRIANGLES, VS::ARRAY_FORMAT_VERTEX|VS::ARRAY_FORMAT_NORMAL, faces.size() );
  326. vs->mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_VERTEX, faces );
  327. vs->mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_NORMAL, normals );
  328. RID trimesh_mat = vs->fixed_material_create();
  329. vs->material_generate( trimesh_mat, Color(1.0,0.5,0.3) );
  330. vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_mat );
  331. RID triins = vs->instance_create2(trimesh_mesh);
  332. RID tribody = ps->body_create( PhysicsServer::BODY_MODE_STATIC, trimesh_shape);
  333. Transform tritrans = Transform( Matrix3(), Vector3(0,0,-2) );
  334. ps->body_set_state( tribody, PhysicsServer::BODY_STATE_TRANSFORM, tritrans );
  335. vs->instance_set_transform( triins, tritrans );
  336. RID trimesh_material = vs->fixed_material_create();
  337. vs->material_generate( trimesh_material, Color(0.2,0.4,0.6) );
  338. vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_material );
  339. #endif
  340. }
  341. virtual bool iteration(float p_time) {
  342. if (mover) {
  343. static float joy_speed = 10;
  344. PhysicsServer * ps = PhysicsServer::get_singleton();
  345. Transform t = ps->body_get_state(mover,PhysicsServer::BODY_STATE_TRANSFORM);
  346. t.origin+=Vector3(joy_speed * joy_direction.x * p_time, -joy_speed * joy_direction.y * p_time,0);
  347. ps->body_set_state(mover,PhysicsServer::BODY_STATE_TRANSFORM,t);
  348. };
  349. Transform cameratr;
  350. cameratr.rotate(Vector3(0,1,0),ofs_x);
  351. cameratr.rotate(Vector3(1,0,0),-ofs_y);
  352. cameratr.translate(Vector3(0,2,8));
  353. VisualServer *vs=VisualServer::get_singleton();
  354. vs->camera_set_transform(camera,cameratr);
  355. return quit;
  356. }
  357. virtual void finish() {
  358. }
  359. void test_joint() {
  360. #if 0
  361. PhysicsServer * ps = PhysicsServer::get_singleton();
  362. mover = create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_STATIC,Transform(Matrix3(),Vector3(0,0,-24)));
  363. RID b = create_body(PhysicsServer::SHAPE_CAPSULE,PhysicsServer::BODY_MODE_RIGID,Transform());
  364. ps->joint_create_double_pin(b,Vector3(0,0,1.0),mover,Vector3(0,0,0));
  365. ps->body_add_collision_exception(mover,b);
  366. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  367. int link_count = LINK_COUNT;
  368. if (cmdline.size() > 0 && cmdline[cmdline.size()-1].to_int()) {
  369. link_count = cmdline[cmdline.size()-1].to_int();
  370. };
  371. for(int i=0;i<link_count;i++) {
  372. RID c = create_body(PhysicsServer::SHAPE_CAPSULE,PhysicsServer::BODY_MODE_RIGID,Transform());
  373. ps->joint_create_double_pin(b,Vector3(0,0,-0.7),c,Vector3(0,0,0.7));
  374. ps->body_add_collision_exception(c,b);
  375. b=c;
  376. }
  377. create_static_plane(Plane(Vector3(0,1,0),-8));
  378. #endif
  379. }
  380. void test_hinge() {
  381. #if 0
  382. PhysicsServer * ps = PhysicsServer::get_singleton();
  383. mover = create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_STATIC,Transform(Matrix3(),Vector3(0,0,-24)));
  384. RID b = create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_RIGID,Transform());
  385. ps->joint_create_double_hinge(b,Transform(Matrix3(),Vector3(1,1,1.0)),mover,Transform(Matrix3(),Vector3(0,0,0)));
  386. ps->body_add_collision_exception(mover,b);
  387. /*
  388. for(int i=0;i<20;i++) {
  389. RID c = create_body(PhysicsServer::SHAPE_CAPSULE,PhysicsServer::BODY_MODE_RIGID,Transform());
  390. ps->joint_create_double_hinge(b,Transform(Matrix3(),Vector3(0,0,-0.7)),c,Transform(Matrix3(),Vector3(0,0,0.7)));
  391. ps->body_add_collision_exception(c,b);
  392. b=c;
  393. }
  394. */
  395. //create_static_plane(Plane(Vector3(0,1,0),-8));
  396. #endif
  397. }
  398. void test_character() {
  399. VisualServer *vs=VisualServer::get_singleton();
  400. PhysicsServer * ps = PhysicsServer::get_singleton();
  401. DVector<Plane> capsule_planes = Geometry::build_capsule_planes(0.5,1,12,5,Vector3::AXIS_Y);
  402. RID capsule_material = vs->fixed_material_create();
  403. vs->fixed_material_set_param( capsule_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1,1,1) );
  404. RID capsule_mesh = vs->mesh_create();
  405. Geometry::MeshData capsule_data = Geometry::build_convex_mesh(capsule_planes);
  406. vs->mesh_add_surface_from_mesh_data(capsule_mesh,capsule_data);
  407. vs->mesh_surface_set_material( capsule_mesh, 0, capsule_material );
  408. type_mesh_map[PhysicsServer::SHAPE_CAPSULE]=capsule_mesh;
  409. RID capsule_shape=ps->shape_create(PhysicsServer::SHAPE_CAPSULE);
  410. Dictionary capsule_params;
  411. capsule_params["radius"]=0.5;
  412. capsule_params["height"]=1;
  413. Transform shape_xform;
  414. shape_xform.rotate(Vector3(1,0,0),Math_PI/2.0);
  415. //shape_xform.origin=Vector3(1,1,1);
  416. ps->shape_set_data( capsule_shape, capsule_params);
  417. RID mesh_instance = vs->instance_create2(capsule_mesh,scenario);
  418. character = ps->body_create(PhysicsServer::BODY_MODE_CHARACTER);
  419. ps->body_set_space(character,space);
  420. //todo add space
  421. ps->body_add_shape(character,capsule_shape);
  422. ps->body_set_force_integration_callback(character,this,"body_changed_transform",mesh_instance);
  423. ps->body_set_state( character, PhysicsServer::BODY_STATE_TRANSFORM,Transform(Matrix3(),Vector3(-2,5,-2)));
  424. bodies.push_back(character);
  425. }
  426. void test_fall() {
  427. for (int i=0;i<35;i++) {
  428. static const PhysicsServer::ShapeType shape_idx[]={
  429. PhysicsServer::SHAPE_CAPSULE,
  430. PhysicsServer::SHAPE_BOX,
  431. PhysicsServer::SHAPE_SPHERE,
  432. PhysicsServer::SHAPE_CONVEX_POLYGON
  433. };
  434. PhysicsServer::ShapeType type=shape_idx[i%4];
  435. //type=PhysicsServer::SHAPE_CONVEX_POLYGON;
  436. Transform t;
  437. t.origin=Vector3(0.0*i,3.5+1.1*i,0.7+0.0*i);
  438. //t.origin=Vector3(-0.7+0.0*i,0.5+4.1*i,0);
  439. t.basis.rotate(Vector3(0.2,-1,0),Math_PI/2*0.6);
  440. //t.basis.rotate(Vector3(0,-1,0),Math_PI/4*i);
  441. //t.basis.rotate(Vector3(0,-1,0),Math_PI/4*i);
  442. //t.basis.rotate(Vector3(-1,0,0),Math_PI/4*i);
  443. RID b = create_body(type,PhysicsServer::BODY_MODE_RIGID,t);
  444. //RID b = create_body(type,i==0?PhysicsServer::BODY_MODE_STATIC:PhysicsServer::BODY_MODE_RIGID,t);
  445. }
  446. create_static_plane( Plane( Vector3(0,1,0), -1) );
  447. /*
  448. create_static_plane( Plane( Vector3(1,0,0), -2) );
  449. create_static_plane( Plane( Vector3(-1,0,0), -2) );
  450. create_static_plane( Plane( Vector3(0,0,1), -2) );
  451. create_static_plane( Plane( Vector3(0,0,-1), -2) );
  452. */
  453. }
  454. void test_activate() {
  455. create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_RIGID,Transform(Matrix3(),Vector3(0,2,0)),true);
  456. //create_body(PhysicsServer::SHAPE_SPHERE,PhysicsServer::BODY_MODE_RIGID,Transform(Matrix3(),Vector3(0,6,0)),true);
  457. create_static_plane( Plane( Vector3(0,1,0), -1) );
  458. }
  459. virtual bool idle(float p_time) {
  460. return false;
  461. }
  462. TestPhysicsMainLoop() {
  463. }
  464. };
  465. namespace TestPhysics {
  466. MainLoop* test() {
  467. return memnew( TestPhysicsMainLoop );
  468. }
  469. }