summaryrefslogtreecommitdiff
path: root/doc/examples/physics/script
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2014-02-09 22:10:30 -0300
committerJuan Linietsky <reduzio@gmail.com>2014-02-09 22:10:30 -0300
commit0b806ee0fc9097fa7bda7ac0109191c9c5e0a1ac (patch)
tree276c4d099e178eb67fbd14f61d77b05e3808e9e3 /doc/examples/physics/script
parent0e49da1687bc8192ed210947da52c9e5c5f301bb (diff)
GODOT IS OPEN SOURCE
Diffstat (limited to 'doc/examples/physics/script')
-rw-r--r--doc/examples/physics/script/test_base.sq295
-rw-r--r--doc/examples/physics/script/test_fall.sq42
2 files changed, 337 insertions, 0 deletions
diff --git a/doc/examples/physics/script/test_base.sq b/doc/examples/physics/script/test_base.sq
new file mode 100644
index 0000000000..9d5451728f
--- /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<p_faces.length()/3;i++) {
+
+ p=Plane( p_faces[i*3+0],p_faces[i*3+1], p_faces[i*3+2] );
+ normals.append(p.normal);
+ normals.append(p.normal);
+ normals.append(p.normal);
+ }
+
+ local trimesh_mesh = VisualServer.mesh_create();
+ VisualServer.mesh_add_surface(trimesh_mesh, VS::PRIMITIVE_TRIANGLES, VS::ARRAY_FORMAT_VERTEX|VS::ARRAY_FORMAT_NORMAL, p_faces.length() );
+ VisualServer.mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_VERTEX, p_faces );
+ VisualServer.mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_NORMAL, normals );
+ local trimesh_mat = VisualServer.fixed_material_create();
+ VisualServer.fixed_material_set_parameter( trimesh_mat, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.5,0.8));
+ //VisualServer.material_set_flag( trimesh_mat, VisualServer.MATERIAL_FLAG_UNSHADED,true);
+ VisualServer.mesh_surface_set_material( trimesh_mesh, 0, trimesh_mat );
+
+ local triins = VisualServer.instance_create(trimesh_mesh);
+
+
+ local tribody = PhysicsServer.body_create(RID(), PhysicsServer.BODY_MODE_STATIC);
+ PhysicsServer.body_add_shape(tribody, trimesh_shape);
+ tritrans = p_xform;
+ PhysicsServer.body_set_state( tribody, PhysicsServer.BODY_STATE_TRANSFORM, tritrans );
+ VisualServer.instance_set_transform( triins, tritrans );
+ //RID trimesh_material = VisualServer.fixed_material_create();
+ //VisualServer.material_generate( trimesh_material, Color(0.2,0.4,0.6) );
+ //VisualServer.mesh_surface_set_material( trimesh_mesh, 0, trimesh_material );
+
+ }
+
+ function make_grid(p_width,p_height,p_cellsize,p_cellheight,p_xform=Transform()) {
+
+ local grid = []
+
+ for (local i =0;i<p_width;i++) {
+
+ local row = []
+
+ for (local j=0;j<p_height;j++) {
+
+ local val = 1.0+Math.random(-p_cellheight, p_cellheight );
+ row.append(val)
+ }
+ grid.append(row)
+ }
+
+ local faces=[]
+
+ for (local i =1;i<p_width;i++) {
+
+ for (local j=1;j<p_height;j++) {
+
+ function MAKE_VERTEX(m_x,m_z) {
+ local v= Vector3( (m_x-p_width/2)*p_cellsize, grid[m_x][m_z], (m_z-p_height/2)*p_cellsize )
+ faces.push_back(v)
+ }
+
+
+ MAKE_VERTEX(i,j-1)
+ MAKE_VERTEX(i,j)
+ MAKE_VERTEX(i-1,j)
+
+ MAKE_VERTEX(i-1,j-1)
+ MAKE_VERTEX(i,j-1)
+ MAKE_VERTEX(i-1,j)
+
+ }
+ }
+
+ make_trimesh(faces,p_xform);
+ }
+
+ quit=false
+
+ transform = Transform()
+ camera=RID()
+
+ function init_internal() {}
+ function iteration_internal(time) {}
+//public
+
+
+ function input_event(p_event) {
+
+
+ }
+
+ function request_quit() {
+
+ quit=true;
+ }
+
+ function init() {
+
+ init_shapes();
+
+
+ /* LIGHT */
+ local lightaux = VisualServer.light_create( VisualServer.LIGHT_DIRECTIONAL );
+ //VisualServer.light_set_color( lightaux, VisualServer.LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
+ VisualServer.light_set_shadow(lightaux,true);
+ local light = VisualServer.instance_create( lightaux );
+ local t=Transform()
+ t.rotate(Vector3(1.0,0,0),0.6);
+ VisualServer.instance_set_transform(light,t);
+
+
+ /* CAMERA */
+
+ camera = VisualServer.camera_create();
+ local viewport = VisualServer.viewport_create();
+ VisualServer.viewport_attach_camera( viewport, camera );
+ VisualServer.viewport_set_parent(viewport, RID() );
+
+ VisualServer.camera_set_perspective(camera,60,0.1,20.0);
+ cameratr=Transform( Matrix3(), Vector3(0,2,8))
+ VisualServer.camera_set_transform(camera,cameratr);
+
+ quit=false;
+
+ init_internal()
+
+ }
+
+
+ function iteration(p_time) {
+
+ // VisualServer.camera_set_transform(camera,cameratr);
+ iteration_internal(p_time)
+
+ return quit;
+ }
+
+ function idle(p_time) {
+
+ return quit;
+ }
+
+
+ function finish() {
+
+ }
+
+
+ constructor() {
+ MainLoopScripted.constructor();
+ }
+
+}
+
+
+return PhysicsTestBase;
diff --git a/doc/examples/physics/script/test_fall.sq b/doc/examples/physics/script/test_fall.sq
new file mode 100644
index 0000000000..79526da8b4
--- /dev/null
+++ b/doc/examples/physics/script/test_fall.sq
@@ -0,0 +1,42 @@
+
+include("test_base.sq")
+
+class TestFall extends PhysicsTestBase {
+
+
+ fall_elements=10
+
+ function init_internal() {
+
+ for (local i=0;i<10;i++) {
+
+ local shape_idx=[
+ PhysicsServer.SHAPE_SPHERE,
+ PhysicsServer.SHAPE_BOX,
+ PhysicsServer.SHAPE_CAPSULE,
+ PhysicsServer.SHAPE_CYLINDER,
+ PhysicsServer.SHAPE_CONVEX_POLYGON
+ ];
+
+ local stype=shape_idx[i%5];
+// stype=PhysicsServer.SHAPE_SPHERE;
+
+ local t=Transform()
+ t.set_origin(Vector3(-0.7+0.0*i,3.5+4.1*i,0))
+ t.rotate_basis(Vector3(1,0,0),Math.PI/4*i)
+
+ local b = create_body(stype,PhysicsServer.BODY_MODE_RIGID,t);
+
+ }
+
+ create_static_plane( Plane( Vector3(0,1,0), -1) );
+
+ }
+
+ constructor() {
+ PhysicsTestBase.constructor()
+ }
+}
+
+
+return TestFall