summaryrefslogtreecommitdiff
path: root/doc/examples/physics/script/test_base.sq
blob: 9d5451728fd1a970b16fed8ed910664a5140e00e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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;