summaryrefslogtreecommitdiff
path: root/bin/tests/test_physics.cpp
blob: e3fd96ff9d7d0064789c9521831e06d2b52f1489 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*************************************************************************/
/*  test_physics.cpp                                                     */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "test_physics.h"


#include "servers/visual_server.h"
#include "servers/physics_server.h"
#include "os/main_loop.h"
#include "math_funcs.h"
#include "print_string.h"
#include "map.h"
#include "os/os.h"
#include "quick_hull.h"

class TestPhysicsMainLoop : public MainLoop {

	OBJ_TYPE( TestPhysicsMainLoop, MainLoop );

	enum {
		LINK_COUNT = 20,
	};

	RID test_cube;
	
	RID plane;
	RID sphere;
	RID light;
	RID camera;
	RID mover;
	RID scenario;
	RID space;

	RID character;

	float ofs_x,ofs_y;

	Point2 joy_direction;

	List<RID> bodies;
	Map<PhysicsServer::ShapeType,RID> type_shape_map;
	Map<PhysicsServer::ShapeType,RID> type_mesh_map;

	void body_changed_transform(Object *p_state, RID p_visual_instance) {

		PhysicsDirectBodyState *state = (PhysicsDirectBodyState*)p_state;
		VisualServer *vs=VisualServer::get_singleton();
		Transform t=state->get_transform();
		//t.basis.scale( Vector3(1.0,0.5,0.2) );
		vs->instance_set_transform(p_visual_instance,t);
	}
	
	bool quit;
	
protected:

	static void _bind_methods() {
	
		ObjectTypeDB::bind_method("body_changed_transform",&TestPhysicsMainLoop::body_changed_transform);
	}

	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()) {

		VisualServer *vs=VisualServer::get_singleton();
		PhysicsServer * ps = PhysicsServer::get_singleton();

		RID mesh_instance = vs->instance_create2(type_mesh_map[p_shape],scenario);
		RID body = ps->body_create(p_body,!p_active_default);		
		ps->body_set_space(body,space);
		ps->body_set_param(body,PhysicsServer::BODY_PARAM_BOUNCE,0.0);
		//todo set space
		ps->body_add_shape(body,type_shape_map[p_shape]);		
		ps->body_set_force_integration_callback(body,this,"body_changed_transform",mesh_instance);

		ps->body_set_state( body, PhysicsServer::BODY_STATE_TRANSFORM,p_location);
		bodies.push_back(body);

		if (p_body==PhysicsServer::BODY_MODE_STATIC) {

			vs->instance_set_transform(mesh_instance,p_location);
		}
		return body;
	}

	RID create_static_plane(const Plane& p_plane) {

		PhysicsServer * ps = PhysicsServer::get_singleton();

		RID plane_shape = ps->shape_create(PhysicsServer::SHAPE_PLANE);;
		ps->shape_set_data( plane_shape, p_plane );

		RID b = ps->body_create( PhysicsServer::BODY_MODE_STATIC );
		ps->body_set_space(b,space);
		//todo set space
		ps->body_add_shape(b, plane_shape);
		return b;

	}

	void configure_body(RID p_body,float p_mass, float p_friction, float p_bounce) {

		PhysicsServer * ps = PhysicsServer::get_singleton();
		ps->body_set_param( p_body, PhysicsServer::BODY_PARAM_MASS, p_mass );
		ps->body_set_param( p_body, PhysicsServer::BODY_PARAM_FRICTION, p_friction );
		ps->body_set_param( p_body, PhysicsServer::BODY_PARAM_BOUNCE, p_bounce );

	}

	void init_shapes() {

		VisualServer *vs=VisualServer::get_singleton();
		PhysicsServer * ps = PhysicsServer::get_singleton();

		/* SPHERE SHAPE */
		RID sphere_mesh = vs->make_sphere_mesh(10,20,0.5);
		RID sphere_material = vs->fixed_material_create();
		//vs->material_set_flag( sphere_material, VisualServer::MATERIAL_FLAG_WIREFRAME, true );
		vs->fixed_material_set_param( sphere_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.7,0.8,3.0) );
		vs->mesh_surface_set_material( sphere_mesh, 0, sphere_material );
		type_mesh_map[PhysicsServer::SHAPE_SPHERE]=sphere_mesh;

		RID sphere_shape=ps->shape_create(PhysicsServer::SHAPE_SPHERE);
		ps->shape_set_data( sphere_shape, 0.5 );
		type_shape_map[PhysicsServer::SHAPE_SPHERE]=sphere_shape;

		/* BOX SHAPE */

		DVector<Plane> box_planes = Geometry::build_box_planes(Vector3(0.5,0.5,0.5));
		RID box_material = vs->fixed_material_create();
		vs->fixed_material_set_param( box_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.2,0.2) );
		RID box_mesh = vs->mesh_create();
		Geometry::MeshData box_data = Geometry::build_convex_mesh(box_planes);
		vs->mesh_add_surface_from_mesh_data(box_mesh,box_data);
		vs->mesh_surface_set_material( box_mesh, 0, box_material );
		type_mesh_map[PhysicsServer::SHAPE_BOX]=box_mesh;

		RID box_shape=ps->shape_create(PhysicsServer::SHAPE_BOX);
		ps->shape_set_data( box_shape, Vector3(0.5,0.5,0.5) );
		type_shape_map[PhysicsServer::SHAPE_BOX]=box_shape;


		/* CAPSULE SHAPE */

		DVector<Plane> capsule_planes = Geometry::build_capsule_planes(0.5,0.7,12,Vector3::AXIS_Z);
		RID capsule_material = vs->fixed_material_create();
		vs->fixed_material_set_param( capsule_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) );

		RID capsule_mesh = vs->mesh_create();
		Geometry::MeshData capsule_data = Geometry::build_convex_mesh(capsule_planes);
		vs->mesh_add_surface_from_mesh_data(capsule_mesh,capsule_data);
		vs->mesh_surface_set_material( capsule_mesh, 0, capsule_material );
		type_mesh_map[PhysicsServer::SHAPE_CAPSULE]=capsule_mesh;

		RID capsule_shape=ps->shape_create(PhysicsServer::SHAPE_CAPSULE);
		Dictionary capsule_params;
		capsule_params["radius"]=0.5;
		capsule_params["height"]=1.4;
		ps->shape_set_data( capsule_shape, capsule_params );
		type_shape_map[PhysicsServer::SHAPE_CAPSULE]=capsule_shape;

		/* CONVEX SHAPE */

		DVector<Plane> convex_planes = Geometry::build_cylinder_planes(0.5,0.7,5,Vector3::AXIS_Z);
		RID convex_material = vs->fixed_material_create();
		vs->fixed_material_set_param( convex_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.8,0.2,0.9));

		RID convex_mesh = vs->mesh_create();
		Geometry::MeshData convex_data = Geometry::build_convex_mesh(convex_planes);
		QuickHull::build(convex_data.vertices,convex_data);
		vs->mesh_add_surface_from_mesh_data(convex_mesh,convex_data);
		vs->mesh_surface_set_material( convex_mesh, 0, convex_material );
		type_mesh_map[PhysicsServer::SHAPE_CONVEX_POLYGON]=convex_mesh;

		RID convex_shape=ps->shape_create(PhysicsServer::SHAPE_CONVEX_POLYGON);
		ps->shape_set_data( convex_shape, convex_data.vertices );
		type_shape_map[PhysicsServer::SHAPE_CONVEX_POLYGON]=convex_shape;

	}

	void make_trimesh(Vector<Vector3> p_faces,const Transform& p_xform=Transform()) {

		VisualServer *vs=VisualServer::get_singleton();
		PhysicsServer * ps = PhysicsServer::get_singleton();
		RID trimesh_shape = ps->shape_create(PhysicsServer::SHAPE_CONCAVE_POLYGON);
		ps->shape_set_data(trimesh_shape, p_faces);
		p_faces=ps->shape_get_data(trimesh_shape); // optimized one
		Vector<Vector3> normals; // for drawing
		for (int i=0;i<p_faces.size()/3;i++) {

			Plane p( p_faces[i*3+0],p_faces[i*3+1], p_faces[i*3+2] );
			normals.push_back(p.normal);
			normals.push_back(p.normal);
			normals.push_back(p.normal);
		}

		RID trimesh_mesh = vs->mesh_create();
		Array d;
		d.resize(VS::ARRAY_MAX);
		d[VS::ARRAY_VERTEX]=p_faces;
		d[VS::ARRAY_NORMAL]=normals;
		vs->mesh_add_surface(trimesh_mesh, VS::PRIMITIVE_TRIANGLES, d );
		RID trimesh_mat = vs->fixed_material_create();
		vs->fixed_material_set_param( trimesh_mat, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.5,0.8));
		//vs->material_set_flag( trimesh_mat, VisualServer::MATERIAL_FLAG_UNSHADED,true);
		vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_mat );

		RID triins = vs->instance_create2(trimesh_mesh,scenario);


		RID tribody = ps->body_create( PhysicsServer::BODY_MODE_STATIC);
		ps->body_set_space(tribody,space);
		//todo set space
		ps->body_add_shape(tribody, trimesh_shape);
		Transform tritrans = p_xform;
		ps->body_set_state( tribody, PhysicsServer::BODY_STATE_TRANSFORM, tritrans );
		vs->instance_set_transform( triins, tritrans );
		//RID trimesh_material = vs->fixed_material_create();
		//vs->material_generate( trimesh_material, Color(0.2,0.4,0.6) );
		//vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_material );

	}

	void make_grid(int p_width,int p_height,float p_cellsize,float p_cellheight,const Transform& p_xform=Transform()) {

		Vector< Vector< float > > grid;

		grid.resize(p_width);

		for (int i=0;i<p_width;i++) {

			grid[i].resize(p_height);

			for (int j=0;j<p_height;j++) {

				grid[i][j]=1.0+Math::random(-p_cellheight, p_cellheight );
			}
		}

		Vector<Vector3> faces;

		for (int i=1;i<p_width;i++) {

			for (int j=1;j<p_height;j++) {


#define MAKE_VERTEX(m_x,m_z)\
	faces.push_back( Vector3( (m_x-p_width/2)*p_cellsize, grid[m_x][m_z], (m_z-p_height/2)*p_cellsize ) )

				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);

	}


public:
	virtual void input_event(const InputEvent& p_event) {
	
		if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&4) {

			ofs_y-=p_event.mouse_motion.relative_y/200.0;
			ofs_x+=p_event.mouse_motion.relative_x/200.0;

		}

		if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&1) {

			float y=-p_event.mouse_motion.relative_y/20.0;
			float x=p_event.mouse_motion.relative_x/20.0;

			if (mover.is_valid()) {


				PhysicsServer * ps = PhysicsServer::get_singleton();
				Transform t = ps->body_get_state(mover,PhysicsServer::BODY_STATE_TRANSFORM);
				t.origin+=Vector3(x,y,0);

				ps->body_set_state(mover,PhysicsServer::BODY_STATE_TRANSFORM,t);
			}

		}

		if (p_event.type == InputEvent::JOYSTICK_MOTION) {

			if (p_event.joy_motion.axis == 0) {

				joy_direction.x = p_event.joy_motion.axis_value;
			};

			if (p_event.joy_motion.axis == 1) {

				joy_direction.y = p_event.joy_motion.axis_value;
			};
		};
	}

	virtual void request_quit() {
	
		quit=true;
	}
	virtual void init() {

		ofs_x=ofs_y=0;
		init_shapes();

		PhysicsServer *ps = PhysicsServer::get_singleton();
		space=ps->space_create();
		ps->space_set_active(space,true);

		VisualServer *vs=VisualServer::get_singleton();

		/* LIGHT */
		RID lightaux = vs->light_create( VisualServer::LIGHT_DIRECTIONAL );
		//vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
		scenario = vs->scenario_create();
		vs->light_set_shadow(lightaux,true);
		light = vs->instance_create2( lightaux,scenario );
		Transform t;
		t.rotate(Vector3(1.0,0,0),0.6);
		vs->instance_set_transform(light,t);



		/* CAMERA */

		camera = vs->camera_create();
		RID viewport = vs->viewport_create();
		vs->viewport_attach_camera( viewport, camera );
		vs->viewport_attach_to_screen(viewport);
		vs->viewport_set_scenario( viewport, scenario );

		vs->camera_set_perspective(camera,60,0.1,40.0);
		vs->camera_set_transform(camera,Transform( Matrix3(), Vector3(0,9,12)));
		//vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);

		Transform gxf;
		gxf.basis.scale(Vector3(1.4,0.4,1.4));
		gxf.origin=Vector3(-2,1,-2);
		make_grid(5,5,2.5,1,gxf);
	//	create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_STATIC,gxf);
		//create_static_plane( Plane( Vector3(0,1,0), -2) );
//		test_joint();
		test_fall();
		//test_joint();


/*
		Vector<Vector3> faces;
		faces.push_back( Vector3(10,0,-5) );
		faces.push_back( Vector3(0,0,10) );
		faces.push_back( Vector3(-10,-0.2,-5) );
		make_trimesh(faces);
*/
		/* Make Trimesh */
		quit=false;
		return;
		
#if 0
#define GRID_SIZE 5
		
		float grid[GRID_SIZE][GRID_SIZE];
		
		for (int i=0;i<GRID_SIZE;i++) {
		
			for (int j=0;j<GRID_SIZE;j++) {
		
				grid[j][i]=Math::random(0.0, 1.0 );
			}
		}
		
		Vector<Vector3> faces;
		
		for (int i=1;i<GRID_SIZE;i++) {
		
			for (int j=1;j<GRID_SIZE;j++) {
		
		
#define MAKE_VERTEX(m_x,m_z)\
	faces.push_back( Vector3( m_x-GRID_SIZE/2.0, grid[m_x][m_z], m_z-GRID_SIZE/2.0 )*3.0 )
									
				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);
								
			}
		}
		/*
		faces.clear();		
		faces.push_back( Vector3(0,0,-5) );
		faces.push_back( Vector3(1,0,-1) );
		faces.push_back( Vector3(-1,-0,-1) );
		*/
		
		RID trimesh_shape = ps->shape_create();
		ps->shape_set_data(trimesh_shape, PhysicsServer::SHAPE_CONCAVE_POLYGON,faces);
		faces=ps->shape_get_shape(trimesh_shape, 0);
		Vector<Vector3> normals; // for drawing
		for (int i=0;i<faces.size()/3;i++) {
		
			Plane p( faces[i*3+0],faces[i*3+1], faces[i*3+2] );
			normals.push_back(p.normal);
			normals.push_back(p.normal);
			normals.push_back(p.normal);
		}

		RID trimesh_mesh = vs->mesh_create();
		vs->mesh_add_surface(trimesh_mesh, VS::PRIMITIVE_TRIANGLES, VS::ARRAY_FORMAT_VERTEX|VS::ARRAY_FORMAT_NORMAL, faces.size() );
		vs->mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_VERTEX, faces );
		vs->mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_NORMAL, normals );
		RID trimesh_mat = vs->fixed_material_create();
		vs->material_generate( trimesh_mat, Color(1.0,0.5,0.3) );
		vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_mat );
		
		RID triins = vs->instance_create2(trimesh_mesh);
		
		
		
		RID tribody = ps->body_create( PhysicsServer::BODY_MODE_STATIC, trimesh_shape);
		Transform tritrans = Transform( Matrix3(), Vector3(0,0,-2) );
		ps->body_set_state( tribody, PhysicsServer::BODY_STATE_TRANSFORM, tritrans );
		vs->instance_set_transform( triins, tritrans );
		RID trimesh_material = vs->fixed_material_create();
		vs->material_generate( trimesh_material, Color(0.2,0.4,0.6) );
		vs->mesh_surface_set_material( trimesh_mesh, 0, trimesh_material );
#endif
	}
	virtual bool iteration(float p_time) {

		if (mover) {
			static float joy_speed = 10;
			PhysicsServer * ps = PhysicsServer::get_singleton();
			Transform t = ps->body_get_state(mover,PhysicsServer::BODY_STATE_TRANSFORM);
			t.origin+=Vector3(joy_speed * joy_direction.x * p_time, -joy_speed * joy_direction.y * p_time,0);
			ps->body_set_state(mover,PhysicsServer::BODY_STATE_TRANSFORM,t);
		};


		Transform cameratr;
		cameratr.rotate(Vector3(0,1,0),ofs_x);
		cameratr.rotate(Vector3(1,0,0),-ofs_y);
		cameratr.translate(Vector3(0,2,8));
		VisualServer *vs=VisualServer::get_singleton();
		vs->camera_set_transform(camera,cameratr);

		return quit;
	}
	virtual void finish() {
	
	}

	void test_joint() {
#if 0
		PhysicsServer * ps = PhysicsServer::get_singleton();

		mover = create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_STATIC,Transform(Matrix3(),Vector3(0,0,-24)));
		RID b = create_body(PhysicsServer::SHAPE_CAPSULE,PhysicsServer::BODY_MODE_RIGID,Transform());

		ps->joint_create_double_pin(b,Vector3(0,0,1.0),mover,Vector3(0,0,0));
		ps->body_add_collision_exception(mover,b);


		List<String> cmdline = OS::get_singleton()->get_cmdline_args();
		int link_count = LINK_COUNT;
		if (cmdline.size() > 0 && cmdline[cmdline.size()-1].to_int()) {
			link_count = cmdline[cmdline.size()-1].to_int();
		};

		for(int i=0;i<link_count;i++) {

			RID c = create_body(PhysicsServer::SHAPE_CAPSULE,PhysicsServer::BODY_MODE_RIGID,Transform());
			ps->joint_create_double_pin(b,Vector3(0,0,-0.7),c,Vector3(0,0,0.7));
			ps->body_add_collision_exception(c,b);
			b=c;
		}


		create_static_plane(Plane(Vector3(0,1,0),-8));
#endif
	}

	void test_hinge() {
#if 0
		PhysicsServer * ps = PhysicsServer::get_singleton();


		mover = create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_STATIC,Transform(Matrix3(),Vector3(0,0,-24)));
		RID b = create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_RIGID,Transform());

		ps->joint_create_double_hinge(b,Transform(Matrix3(),Vector3(1,1,1.0)),mover,Transform(Matrix3(),Vector3(0,0,0)));
		ps->body_add_collision_exception(mover,b);

/*
		for(int i=0;i<20;i++) {

			RID c = create_body(PhysicsServer::SHAPE_CAPSULE,PhysicsServer::BODY_MODE_RIGID,Transform());
			ps->joint_create_double_hinge(b,Transform(Matrix3(),Vector3(0,0,-0.7)),c,Transform(Matrix3(),Vector3(0,0,0.7)));
			ps->body_add_collision_exception(c,b);
			b=c;
		}

*/
		//create_static_plane(Plane(Vector3(0,1,0),-8));
#endif
	}

	void test_character() {

		VisualServer *vs=VisualServer::get_singleton();
		PhysicsServer * ps = PhysicsServer::get_singleton();


		DVector<Plane> capsule_planes = Geometry::build_capsule_planes(0.5,1,12,5,Vector3::AXIS_Y);
		RID capsule_material = vs->fixed_material_create();

		vs->fixed_material_set_param( capsule_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1,1,1) );


		RID capsule_mesh = vs->mesh_create();
		Geometry::MeshData capsule_data = Geometry::build_convex_mesh(capsule_planes);
		vs->mesh_add_surface_from_mesh_data(capsule_mesh,capsule_data);
		vs->mesh_surface_set_material( capsule_mesh, 0, capsule_material );
		type_mesh_map[PhysicsServer::SHAPE_CAPSULE]=capsule_mesh;

		RID capsule_shape=ps->shape_create(PhysicsServer::SHAPE_CAPSULE);
		Dictionary capsule_params;
		capsule_params["radius"]=0.5;
		capsule_params["height"]=1;
		Transform shape_xform;
		shape_xform.rotate(Vector3(1,0,0),Math_PI/2.0);
		//shape_xform.origin=Vector3(1,1,1);
		ps->shape_set_data( capsule_shape,  capsule_params);


		RID mesh_instance = vs->instance_create2(capsule_mesh,scenario);
		character = ps->body_create(PhysicsServer::BODY_MODE_CHARACTER);
		ps->body_set_space(character,space);
		//todo add space
		ps->body_add_shape(character,capsule_shape);

		ps->body_set_force_integration_callback(character,this,"body_changed_transform",mesh_instance);


		ps->body_set_state( character, PhysicsServer::BODY_STATE_TRANSFORM,Transform(Matrix3(),Vector3(-2,5,-2)));
		bodies.push_back(character);


	}

	void test_fall() {


		for (int i=0;i<35;i++) {

			static const PhysicsServer::ShapeType shape_idx[]={
				PhysicsServer::SHAPE_CAPSULE,
				PhysicsServer::SHAPE_BOX,
				PhysicsServer::SHAPE_SPHERE,
				PhysicsServer::SHAPE_CONVEX_POLYGON
			};

			PhysicsServer::ShapeType type=shape_idx[i%4];
			//type=PhysicsServer::SHAPE_CONVEX_POLYGON;

			Transform t;			

			t.origin=Vector3(0.0*i,3.5+1.1*i,0.7+0.0*i);
			//t.origin=Vector3(-0.7+0.0*i,0.5+4.1*i,0);
			t.basis.rotate(Vector3(0.2,-1,0),Math_PI/2*0.6);
			//t.basis.rotate(Vector3(0,-1,0),Math_PI/4*i);
			//t.basis.rotate(Vector3(0,-1,0),Math_PI/4*i);
			//t.basis.rotate(Vector3(-1,0,0),Math_PI/4*i);


			RID b = create_body(type,PhysicsServer::BODY_MODE_RIGID,t);
			//RID b = create_body(type,i==0?PhysicsServer::BODY_MODE_STATIC:PhysicsServer::BODY_MODE_RIGID,t);

		}

		create_static_plane( Plane( Vector3(0,1,0), -1) );


/*
		create_static_plane( Plane( Vector3(1,0,0), -2) );
		create_static_plane( Plane( Vector3(-1,0,0), -2) );
		create_static_plane( Plane( Vector3(0,0,1), -2) );
		create_static_plane( Plane( Vector3(0,0,-1), -2) );
*/


	}

	void test_activate() {

		create_body(PhysicsServer::SHAPE_BOX,PhysicsServer::BODY_MODE_RIGID,Transform(Matrix3(),Vector3(0,2,0)),true);
		//create_body(PhysicsServer::SHAPE_SPHERE,PhysicsServer::BODY_MODE_RIGID,Transform(Matrix3(),Vector3(0,6,0)),true);
		create_static_plane( Plane( Vector3(0,1,0), -1) );

	}


	virtual bool idle(float p_time) {
		return false;
	}







	TestPhysicsMainLoop() {

	}
};

namespace TestPhysics {

MainLoop* test() {

	return memnew( TestPhysicsMainLoop );

}

}