aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ode-0.9/contrib/Ode.NET/Tests/BoxStack.cs
blob: 10771246e727a30c943fca90d4348f801bd8251f (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
using System;
using System.Collections.Generic;
using Drawstuff.NET;

namespace Ode.NET
{
#if dDOUBLE
	using dReal = System.Double;
#else
	using dReal = System.Single;
#endif

	public class TestBoxStack
	{
		#region Description of convex shape

		static dReal[] planes = 
			{
				1.0f, 0.0f, 0.0f, 0.25f,
				0.0f, 1.0f, 0.0f, 0.25f,
				0.0f, 0.0f, 1.0f, 0.25f,
				0.0f, 0.0f, -1.0f, 0.25f,
				0.0f, -1.0f, 0.0f, 0.25f,
				-1.0f, 0.0f , 0.0f, 0.25f
			};

		static dReal[] points =
			{
				0.25f, 0.25f, 0.25f,
				-0.25f, 0.25f, 0.25f,
				0.25f, -0.25f, 0.25f,
				-0.25f, -0.25f, 0.25f,
				0.25f, 0.25f, -0.25f,
				-0.25f,0.25f,-0.25f,
				0.25f,-0.25f,-0.25f,
				-0.25f,-0.25f,-0.25f,
			};

		static int[] polygons =
		{
			4, 0, 2, 6, 4,
			4, 1, 0, 4, 5,
			4, 0, 1, 3, 2,
			4, 3, 1, 5, 7,
			4, 2, 3, 7, 6,
			4, 5, 4, 6, 7,
		};

		#endregion

		const int NUM = 100;
		const float DENSITY = 5.0f;
		const int MAX_CONTACTS = 8;
		
		static IntPtr world;
		static IntPtr space;
		static IntPtr contactgroup;

		static Queue<IntPtr> obj = new Queue<IntPtr>();

		static d.Vector3 xyz = new d.Vector3(2.1640f, -1.3079f, 1.7600f);
		static d.Vector3 hpr = new d.Vector3(125.5000f, -17.0000f, 0.0000f);

		static d.NearCallback nearCallback = near;
		static d.ContactGeom[] contacts = new d.ContactGeom[MAX_CONTACTS];
		static d.Contact contact;


		// Called when window is opened - sets up viewpoint and prints usage
		static void start(int unused)
		{
			ds.SetViewpoint(ref xyz, ref hpr);
			Console.WriteLine("To drop another object, press:");
			Console.WriteLine("   b for box.");
			Console.WriteLine("   s for sphere.");
			Console.WriteLine("   c for capsule.");
			Console.WriteLine("   y for cylinder.");
			Console.WriteLine("   v for a convex object.");
			Console.WriteLine("   x for a composite object.");
			Console.WriteLine("To select an object, press space.");
			Console.WriteLine("To disable the selected object, press d.");
			Console.WriteLine("To enable the selected object, press e.");
			Console.WriteLine("To toggle showing the geom AABBs, press a.");
			Console.WriteLine("To toggle showing the contact points, press t.");
			Console.WriteLine("To toggle dropping from random position/orientation, press r.");
			Console.WriteLine("To save the current state to 'state.dif', press 1.");
		}


		// Near callback - creates contact joints
		static void near(IntPtr space, IntPtr g1, IntPtr g2)
		{
			IntPtr b1 = d.GeomGetBody(g1);
			IntPtr b2 = d.GeomGetBody(g2);
			if (b1 != IntPtr.Zero && b2 != IntPtr.Zero && d.AreConnectedExcluding(b1, b2, d.JointType.Contact))
				return;

			int count = d.Collide(g1, g2, MAX_CONTACTS, contacts, d.ContactGeom.SizeOf);
			for (int i = 0; i < count; ++i)
			{
				contact.geom = contacts[i];
				IntPtr joint = d.JointCreateContact(world, contactgroup, ref contact);
				d.JointAttach(joint, b1, b2);
			}
		}


		// Adds a new object to the scene - attaches a body to the geom and
		// sets the initial position and orientation
		static void addObject(IntPtr geom, d.Mass mass)
		{
			// Create a body for this object
			IntPtr body = d.BodyCreate(world);
			d.GeomSetBody(geom, body);
			d.BodySetMass(body, ref mass);
			obj.Enqueue(geom);

			// Set the position of the new object
			d.Matrix3 R;
			d.BodySetPosition(body, d.RandReal() * 2 - 1, d.RandReal() * 2 - 1, d.RandReal() + 2);
			d.RFromAxisAndAngle(out R, d.RandReal() * 2 - 1, d.RandReal() * 2 - 1, d.RandReal() * 2 - 1, d.RandReal() * 10 - 5);
			d.BodySetRotation(body, ref R);

			// Cap the total number of objects
			if (obj.Count > NUM)
			{
				geom = obj.Dequeue();
				body = d.GeomGetBody(geom);
				d.BodyDestroy(body);
				d.GeomDestroy(geom);
			}
		}


		// Keyboard callback
		static void command(int cmd)
		{
			IntPtr geom;
			d.Mass mass;
			d.Vector3 sides = new d.Vector3(d.RandReal() * 0.5f + 0.1f, d.RandReal() * 0.5f + 0.1f, d.RandReal() * 0.5f + 0.1f);

			Char ch = Char.ToLower((Char)cmd);
			switch ((Char)ch)
			{
			case 'b':
				d.MassSetBox(out mass, DENSITY, sides.X, sides.Y, sides.Z);
				geom = d.CreateBox(space, sides.X, sides.Y, sides.Z);
				addObject(geom, mass);
				break;

			case 'c':
				sides.X *= 0.5f;
				d.MassSetCapsule(out mass, DENSITY, 3, sides.X, sides.Y);
				geom = d.CreateCapsule(space, sides.X, sides.Y);
				addObject(geom, mass);
				break;

			case 'v':
				d.MassSetBox(out mass, DENSITY, 0.25f, 0.25f, 0.25f);
				geom = d.CreateConvex(space, planes, planes.Length / 4, points, points.Length / 3, polygons);
				addObject(geom, mass);
				break;
			}
		}


		// Draw an object in the scene
		static void drawGeom(IntPtr geom)
		{
			IntPtr body = d.GeomGetBody(geom);

			d.Vector3 pos;
			d.BodyCopyPosition(body, out pos);

			d.Matrix3 R;
			d.BodyCopyRotation(body, out R);

			d.GeomClassID type = d.GeomGetClass(geom);
			switch (type)
			{
			case d.GeomClassID.BoxClass:
				d.Vector3 sides;
				d.GeomBoxGetLengths(geom, out sides);
				ds.DrawBox(ref pos, ref R, ref sides);
				break;
			case d.GeomClassID.CapsuleClass:
				dReal radius, length;
				d.GeomCapsuleGetParams(geom, out radius, out length);
				ds.DrawCapsule(ref pos, ref R, length, radius);
				break;
			case d.GeomClassID.ConvexClass:
				ds.DrawConvex(ref pos, ref R, planes, planes.Length / 4, points, points.Length / 3, polygons);
				break;
			}
		}


		// Called once per frame; updates the scene
		static void step(int pause)
		{
			d.SpaceCollide(space, IntPtr.Zero, nearCallback);
			if (pause == 0)
				d.WorldQuickStep(world, 0.02f);
			d.JointGroupEmpty(contactgroup);

			ds.SetColor(1.0f, 1.0f, 0.0f);
			ds.SetTexture(ds.Texture.Wood);

			foreach (IntPtr geom in obj)
			{
				drawGeom(geom);
			}
		}


		static void Main(string[] args)
		{
			// Setup pointers to drawstuff callback functions
			ds.Functions fn;
			fn.version = ds.VERSION;
			fn.start = new ds.CallbackFunction(start);
			fn.step = new ds.CallbackFunction(step);
			fn.command = new ds.CallbackFunction(command);
			fn.stop = null;
			fn.path_to_textures = "../../../../drawstuff/textures";
			if (args.Length > 0)
			{
				fn.path_to_textures = args[0];
			}

			// Set up contact response parameters
			contact.surface.mode = d.ContactFlags.Bounce | d.ContactFlags.SoftCFM;
			contact.surface.mu = d.Infinity;
			contact.surface.mu2 = 0.0f;
			contact.surface.bounce = 0.1f;
			contact.surface.bounce_vel = 0.1f;
			contact.surface.soft_cfm = 0.01f;

			// Initialize the scene
			world = d.WorldCreate();
			space = d.HashSpaceCreate(IntPtr.Zero);
			contactgroup = d.JointGroupCreate(0);
			d.WorldSetGravity(world, 0.0f, 0.0f, -0.5f);
			d.WorldSetCFM(world, 1e-5f);
			d.WorldSetAutoDisableFlag(world, true);
			d.WorldSetContactMaxCorrectingVel(world, 0.1f);
			d.WorldSetContactSurfaceLayer(world, 0.001f);
			d.CreatePlane(space, 0, 0, 1, 0);

			// Run the scene
			ds.SimulationLoop(args.Length, args, 352, 288, ref fn);

			// Clean up
			d.JointGroupDestroy(contactgroup);
			d.SpaceDestroy(space);
			d.WorldDestroy(world);
			d.CloseODE();
		}
	}
}