From 0b806ee0fc9097fa7bda7ac0109191c9c5e0a1ac Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 9 Feb 2014 22:10:30 -0300 Subject: GODOT IS OPEN SOURCE --- doc/examples/physics/script/test_base.sq | 295 +++++++++++++++++++++++++++++++ doc/examples/physics/script/test_fall.sq | 42 +++++ 2 files changed, 337 insertions(+) create mode 100644 doc/examples/physics/script/test_base.sq create mode 100644 doc/examples/physics/script/test_fall.sq (limited to 'doc/examples/physics/script') diff --git a/doc/examples/physics/script/test_base.sq b/doc/examples/physics/script/test_base.sq new file mode 100644 index 000000000..9d5451728 --- /dev/null +++ b/doc/examples/physics/script/test_base.sq @@ -0,0 +1,295 @@ + +class PhysicsTestBase extends MainLoopScripted { + + + bodies=[] + type_mesh_map={} + type_shape_map={} + cameratr=Transform() + + + function body_changed_transform(p_transform, p_velocity, p_angular_velocity,p_sleeping, p_visual_instance) { + + VisualServer.instance_set_transform(p_visual_instance,p_transform); + } + + + function create_body(p_shape, p_body_mode, p_location, p_active=true) { + + local mesh_instance = VisualServer.instance_create( type_mesh_map[p_shape] ) + local body = PhysicsServer.body_create(RID(),p_body_mode,!p_active) + PhysicsServer.body_add_shape(body,type_shape_map[p_shape]) + + local query = PhysicsServer.query_create(this,"body_changed_transform",mesh_instance) + PhysicsServer.query_body_state(query, body) + + PhysicsServer.body_set_state( body, PhysicsServer.BODY_STATE_TRANSFORM, p_location ) + bodies.append( body ) + return body + + } + + + function create_static_plane(p_plane) { + + local plane_shape = PhysicsServer.shape_create(PhysicsServer.SHAPE_PLANE) + PhysicsServer.shape_set_data( plane_shape, p_plane ); + + local b = PhysicsServer.body_create(RID(), PhysicsServer.BODY_MODE_STATIC ); + PhysicsServer.body_add_shape(b, plane_shape); + return b; + } + + function configure_body( p_body, p_mass, p_friction, p_bounce) { + + PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_MASS, p_mass ); + PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_FRICTION, p_friction ); + PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_BOUNCE, p_bounce ); + + } + + function init_shapes() { + + + /* SPHERE SHAPE */ + local sphere_mesh = VisualServer.make_sphere_mesh(10,20,1.0); + local sphere_material = VisualServer.fixed_material_create(); + //VisualServer.material_set_flag( sphere_material, VisualServer.MATERIAL_FLAG_WIREFRAME, true ); + VisualServer.fixed_material_set_parameter( sphere_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.7,0.8,3.0) ); + VisualServer.mesh_surface_set_material( sphere_mesh, 0, sphere_material ); + type_mesh_map[PhysicsServer.SHAPE_SPHERE] <- sphere_mesh; + + local sphere_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_SPHERE); + PhysicsServer.shape_set_data( sphere_shape, 1.0 ); + type_shape_map[PhysicsServer.SHAPE_SPHERE] <- sphere_shape; + + /* BOX SHAPE */ + + local box_planes = GeometryUtils.build_box_planes(Vector3(0.5,0.5,0.5)); + local box_material = VisualServer.fixed_material_create(); + VisualServer.fixed_material_set_parameter( box_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.2,0.2) ); + local box_mesh = VisualServer.mesh_create(); + + VisualServer.mesh_add_surface_from_planes(box_mesh,box_planes); + VisualServer.mesh_surface_set_material( box_mesh, 0, box_material ); + type_mesh_map[PhysicsServer.SHAPE_BOX]<- box_mesh; + + local box_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_BOX); + PhysicsServer.shape_set_data( box_shape, Vector3(0.5,0.5,0.5) ); + type_shape_map[PhysicsServer.SHAPE_BOX]<- box_shape; + + /* CYLINDER SHAPE */ + + local cylinder_planes = GeometryUtils.build_cylinder_planes(0.5,1,12 + ,Vector3.AXIS_Z); + local cylinder_material = VisualServer.fixed_material_create(); + VisualServer.fixed_material_set_parameter( cylinder_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) ); + local cylinder_mesh = VisualServer.mesh_create(); + + VisualServer.mesh_add_surface_from_planes(cylinder_mesh,cylinder_planes); + VisualServer.mesh_surface_set_material( cylinder_mesh, 0, cylinder_material ); + type_mesh_map[PhysicsServer.SHAPE_CYLINDER]<- cylinder_mesh; + + local cylinder_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CYLINDER); + local cylinder_params={} + cylinder_params["radius"]<- 0.5; + cylinder_params["height"]<- 2; + PhysicsServer.shape_set_data( cylinder_shape, cylinder_params ); + type_shape_map[PhysicsServer.SHAPE_CYLINDER]<- cylinder_shape; + + /* CAPSULE SHAPE */ + + local capsule_planes = GeometryUtils.build_capsule_planes(0.5,0.7,12,Vector3.AXIS_Z); + local capsule_material = VisualServer.fixed_material_create(); + VisualServer.fixed_material_set_parameter( capsule_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) ); + + local capsule_mesh = VisualServer.mesh_create(); + + VisualServer.mesh_add_surface_from_planes(capsule_mesh,capsule_planes); + VisualServer.mesh_surface_set_material( capsule_mesh, 0, capsule_material ); + type_mesh_map[PhysicsServer.SHAPE_CAPSULE]<-capsule_mesh; + + local capsule_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CAPSULE); + local capsule_params={} + capsule_params["radius"]<- 0.5; + capsule_params["height"]<- 1.4; + PhysicsServer.shape_set_data( capsule_shape, capsule_params ); + type_shape_map[PhysicsServer.SHAPE_CAPSULE]<- capsule_shape; + + /* CONVEX SHAPE */ + + local convex_planes = GeometryUtils.build_cylinder_planes(0.5,0.7,5,Vector3.AXIS_Z); + local convex_material = VisualServer.fixed_material_create(); + VisualServer.fixed_material_set_parameter( convex_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.8,0.2,0.9)); + + local convex_mesh = VisualServer.mesh_create(); + VisualServer.mesh_add_surface_from_planes(convex_mesh,convex_planes); + VisualServer.mesh_surface_set_material( convex_mesh, 0, convex_material ); + type_mesh_map[PhysicsServer.SHAPE_CONVEX_POLYGON]<- convex_mesh; + + local convex_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CONVEX_POLYGON); + PhysicsServer.shape_set_data( convex_shape, convex_planes ); + type_shape_map[PhysicsServer.SHAPE_CONVEX_POLYGON]<- convex_shape; + + } + + function make_trimesh(p_faces,p_xform=Transform()) { + + local trimesh_shape = PhysicsServer.shape_create(PhysicsServer.SHAPE_CONCAVE_POLYGON); + PhysicsServer.shape_set_data(trimesh_shape, p_faces); + p_faces=PhysicsServer.shape_get_data(trimesh_shape); // optimized one + normals=[] + + for (i=0;i