test_base.sq 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. class PhysicsTestBase extends MainLoopScripted {
  2. bodies=[]
  3. type_mesh_map={}
  4. type_shape_map={}
  5. cameratr=Transform()
  6. function body_changed_transform(p_transform, p_velocity, p_angular_velocity,p_sleeping, p_visual_instance) {
  7. VisualServer.instance_set_transform(p_visual_instance,p_transform);
  8. }
  9. function create_body(p_shape, p_body_mode, p_location, p_active=true) {
  10. local mesh_instance = VisualServer.instance_create( type_mesh_map[p_shape] )
  11. local body = PhysicsServer.body_create(RID(),p_body_mode,!p_active)
  12. PhysicsServer.body_add_shape(body,type_shape_map[p_shape])
  13. local query = PhysicsServer.query_create(this,"body_changed_transform",mesh_instance)
  14. PhysicsServer.query_body_state(query, body)
  15. PhysicsServer.body_set_state( body, PhysicsServer.BODY_STATE_TRANSFORM, p_location )
  16. bodies.append( body )
  17. return body
  18. }
  19. function create_static_plane(p_plane) {
  20. local plane_shape = PhysicsServer.shape_create(PhysicsServer.SHAPE_PLANE)
  21. PhysicsServer.shape_set_data( plane_shape, p_plane );
  22. local b = PhysicsServer.body_create(RID(), PhysicsServer.BODY_MODE_STATIC );
  23. PhysicsServer.body_add_shape(b, plane_shape);
  24. return b;
  25. }
  26. function configure_body( p_body, p_mass, p_friction, p_bounce) {
  27. PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_MASS, p_mass );
  28. PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_FRICTION, p_friction );
  29. PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_BOUNCE, p_bounce );
  30. }
  31. function init_shapes() {
  32. /* SPHERE SHAPE */
  33. local sphere_mesh = VisualServer.make_sphere_mesh(10,20,1.0);
  34. local sphere_material = VisualServer.fixed_material_create();
  35. //VisualServer.material_set_flag( sphere_material, VisualServer.MATERIAL_FLAG_WIREFRAME, true );
  36. VisualServer.fixed_material_set_parameter( sphere_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.7,0.8,3.0) );
  37. VisualServer.mesh_surface_set_material( sphere_mesh, 0, sphere_material );
  38. type_mesh_map[PhysicsServer.SHAPE_SPHERE] <- sphere_mesh;
  39. local sphere_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_SPHERE);
  40. PhysicsServer.shape_set_data( sphere_shape, 1.0 );
  41. type_shape_map[PhysicsServer.SHAPE_SPHERE] <- sphere_shape;
  42. /* BOX SHAPE */
  43. local box_planes = GeometryUtils.build_box_planes(Vector3(0.5,0.5,0.5));
  44. local box_material = VisualServer.fixed_material_create();
  45. VisualServer.fixed_material_set_parameter( box_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.2,0.2) );
  46. local box_mesh = VisualServer.mesh_create();
  47. VisualServer.mesh_add_surface_from_planes(box_mesh,box_planes);
  48. VisualServer.mesh_surface_set_material( box_mesh, 0, box_material );
  49. type_mesh_map[PhysicsServer.SHAPE_BOX]<- box_mesh;
  50. local box_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_BOX);
  51. PhysicsServer.shape_set_data( box_shape, Vector3(0.5,0.5,0.5) );
  52. type_shape_map[PhysicsServer.SHAPE_BOX]<- box_shape;
  53. /* CYLINDER SHAPE */
  54. local cylinder_planes = GeometryUtils.build_cylinder_planes(0.5,1,12
  55. ,Vector3.AXIS_Z);
  56. local cylinder_material = VisualServer.fixed_material_create();
  57. VisualServer.fixed_material_set_parameter( cylinder_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) );
  58. local cylinder_mesh = VisualServer.mesh_create();
  59. VisualServer.mesh_add_surface_from_planes(cylinder_mesh,cylinder_planes);
  60. VisualServer.mesh_surface_set_material( cylinder_mesh, 0, cylinder_material );
  61. type_mesh_map[PhysicsServer.SHAPE_CYLINDER]<- cylinder_mesh;
  62. local cylinder_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CYLINDER);
  63. local cylinder_params={}
  64. cylinder_params["radius"]<- 0.5;
  65. cylinder_params["height"]<- 2;
  66. PhysicsServer.shape_set_data( cylinder_shape, cylinder_params );
  67. type_shape_map[PhysicsServer.SHAPE_CYLINDER]<- cylinder_shape;
  68. /* CAPSULE SHAPE */
  69. local capsule_planes = GeometryUtils.build_capsule_planes(0.5,0.7,12,Vector3.AXIS_Z);
  70. local capsule_material = VisualServer.fixed_material_create();
  71. VisualServer.fixed_material_set_parameter( capsule_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) );
  72. local capsule_mesh = VisualServer.mesh_create();
  73. VisualServer.mesh_add_surface_from_planes(capsule_mesh,capsule_planes);
  74. VisualServer.mesh_surface_set_material( capsule_mesh, 0, capsule_material );
  75. type_mesh_map[PhysicsServer.SHAPE_CAPSULE]<-capsule_mesh;
  76. local capsule_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CAPSULE);
  77. local capsule_params={}
  78. capsule_params["radius"]<- 0.5;
  79. capsule_params["height"]<- 1.4;
  80. PhysicsServer.shape_set_data( capsule_shape, capsule_params );
  81. type_shape_map[PhysicsServer.SHAPE_CAPSULE]<- capsule_shape;
  82. /* CONVEX SHAPE */
  83. local convex_planes = GeometryUtils.build_cylinder_planes(0.5,0.7,5,Vector3.AXIS_Z);
  84. local convex_material = VisualServer.fixed_material_create();
  85. VisualServer.fixed_material_set_parameter( convex_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.8,0.2,0.9));
  86. local convex_mesh = VisualServer.mesh_create();
  87. VisualServer.mesh_add_surface_from_planes(convex_mesh,convex_planes);
  88. VisualServer.mesh_surface_set_material( convex_mesh, 0, convex_material );
  89. type_mesh_map[PhysicsServer.SHAPE_CONVEX_POLYGON]<- convex_mesh;
  90. local convex_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CONVEX_POLYGON);
  91. PhysicsServer.shape_set_data( convex_shape, convex_planes );
  92. type_shape_map[PhysicsServer.SHAPE_CONVEX_POLYGON]<- convex_shape;
  93. }
  94. function make_trimesh(p_faces,p_xform=Transform()) {
  95. local trimesh_shape = PhysicsServer.shape_create(PhysicsServer.SHAPE_CONCAVE_POLYGON);
  96. PhysicsServer.shape_set_data(trimesh_shape, p_faces);
  97. p_faces=PhysicsServer.shape_get_data(trimesh_shape); // optimized one
  98. normals=[]
  99. for (i=0;i<p_faces.length()/3;i++) {
  100. p=Plane( p_faces[i*3+0],p_faces[i*3+1], p_faces[i*3+2] );
  101. normals.append(p.normal);
  102. normals.append(p.normal);
  103. normals.append(p.normal);
  104. }
  105. local trimesh_mesh = VisualServer.mesh_create();
  106. VisualServer.mesh_add_surface(trimesh_mesh, VS::PRIMITIVE_TRIANGLES, VS::ARRAY_FORMAT_VERTEX|VS::ARRAY_FORMAT_NORMAL, p_faces.length() );
  107. VisualServer.mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_VERTEX, p_faces );
  108. VisualServer.mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_NORMAL, normals );
  109. local trimesh_mat = VisualServer.fixed_material_create();
  110. VisualServer.fixed_material_set_parameter( trimesh_mat, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.5,0.8));
  111. //VisualServer.material_set_flag( trimesh_mat, VisualServer.MATERIAL_FLAG_UNSHADED,true);
  112. VisualServer.mesh_surface_set_material( trimesh_mesh, 0, trimesh_mat );
  113. local triins = VisualServer.instance_create(trimesh_mesh);
  114. local tribody = PhysicsServer.body_create(RID(), PhysicsServer.BODY_MODE_STATIC);
  115. PhysicsServer.body_add_shape(tribody, trimesh_shape);
  116. tritrans = p_xform;
  117. PhysicsServer.body_set_state( tribody, PhysicsServer.BODY_STATE_TRANSFORM, tritrans );
  118. VisualServer.instance_set_transform( triins, tritrans );
  119. //RID trimesh_material = VisualServer.fixed_material_create();
  120. //VisualServer.material_generate( trimesh_material, Color(0.2,0.4,0.6) );
  121. //VisualServer.mesh_surface_set_material( trimesh_mesh, 0, trimesh_material );
  122. }
  123. function make_grid(p_width,p_height,p_cellsize,p_cellheight,p_xform=Transform()) {
  124. local grid = []
  125. for (local i =0;i<p_width;i++) {
  126. local row = []
  127. for (local j=0;j<p_height;j++) {
  128. local val = 1.0+Math.random(-p_cellheight, p_cellheight );
  129. row.append(val)
  130. }
  131. grid.append(row)
  132. }
  133. local faces=[]
  134. for (local i =1;i<p_width;i++) {
  135. for (local j=1;j<p_height;j++) {
  136. function MAKE_VERTEX(m_x,m_z) {
  137. local v= Vector3( (m_x-p_width/2)*p_cellsize, grid[m_x][m_z], (m_z-p_height/2)*p_cellsize )
  138. faces.push_back(v)
  139. }
  140. MAKE_VERTEX(i,j-1)
  141. MAKE_VERTEX(i,j)
  142. MAKE_VERTEX(i-1,j)
  143. MAKE_VERTEX(i-1,j-1)
  144. MAKE_VERTEX(i,j-1)
  145. MAKE_VERTEX(i-1,j)
  146. }
  147. }
  148. make_trimesh(faces,p_xform);
  149. }
  150. quit=false
  151. transform = Transform()
  152. camera=RID()
  153. function init_internal() {}
  154. function iteration_internal(time) {}
  155. //public
  156. function input_event(p_event) {
  157. }
  158. function request_quit() {
  159. quit=true;
  160. }
  161. function init() {
  162. init_shapes();
  163. /* LIGHT */
  164. local lightaux = VisualServer.light_create( VisualServer.LIGHT_DIRECTIONAL );
  165. //VisualServer.light_set_color( lightaux, VisualServer.LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
  166. VisualServer.light_set_shadow(lightaux,true);
  167. local light = VisualServer.instance_create( lightaux );
  168. local t=Transform()
  169. t.rotate(Vector3(1.0,0,0),0.6);
  170. VisualServer.instance_set_transform(light,t);
  171. /* CAMERA */
  172. camera = VisualServer.camera_create();
  173. local viewport = VisualServer.viewport_create();
  174. VisualServer.viewport_attach_camera( viewport, camera );
  175. VisualServer.viewport_set_parent(viewport, RID() );
  176. VisualServer.camera_set_perspective(camera,60,0.1,20.0);
  177. cameratr=Transform( Matrix3(), Vector3(0,2,8))
  178. VisualServer.camera_set_transform(camera,cameratr);
  179. quit=false;
  180. init_internal()
  181. }
  182. function iteration(p_time) {
  183. // VisualServer.camera_set_transform(camera,cameratr);
  184. iteration_internal(p_time)
  185. return quit;
  186. }
  187. function idle(p_time) {
  188. return quit;
  189. }
  190. function finish() {
  191. }
  192. constructor() {
  193. MainLoopScripted.constructor();
  194. }
  195. }
  196. return PhysicsTestBase;