summaryrefslogtreecommitdiff
path: root/src/ode/ode_bindings.c
blob: 116878a8157617d2c1df525b46e8a7711ede8d8e (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
#include <lua.h>
#include <honeysuckle.h>
#include <ode/ode.h>


/* setup/teardown */
int init_ode(lua_State *L);
int close_ode(lua_State *L);

/* world functions */
int world_create(lua_State *L);
int world_destroy(lua_State *L);
int world_set_gravity(lua_State *L);
int world_get_gravity(lua_State *L);
int world_set_cfm(lua_State *L);
int world_quickstep(lua_State *L);

/* space functions */
int hash_space_create(lua_State *L);
int space_destroy(lua_State *L);
int space_collide(lua_State *L);

/* joint group functions */
int joint_group_create(lua_State *L);
int joint_group_destroy(lua_State *L);
int joint_group_empty(lua_State *L);

/* joint functions */
int joint_create_contact(lua_State *L);
int joint_attach(lua_State *L);

/* body functions */
int body_create(lua_State *L);
int body_set_mass(lua_State *L);
int body_set_position(lua_State *L);
int body_get_position(lua_State *L);
int body_add_force(lua_State *L);
int body_get_force(lua_State *L);
int body_set_linear_vel(lua_State *L);

/* geom functions */
int create_sphere(lua_State *L);
int create_plane(lua_State *L);
int geom_get_class(lua_State *L);
int geom_set_body(lua_State *L);
int geom_get_body(lua_State *L);
int collide(lua_State *L);

/* mass functions */
int mass_create(lua_State *L);
int mass_set_sphere(lua_State *L);


/* contact functions */
int contact_create(lua_State *L);
int contact_surface_set_mode(lua_State *L);
int contact_surface_set_mu(lua_State *L);
int contact_surface_set_bounce(lua_State *L);
int contact_surface_set_bounce_vel(lua_State *L);
int contact_surface_set_soft_cfm(lua_State *L);


#define NEW_METATABLE(name) luaL_newmetatable(L, name); lua_pop(L, 1);
static const char *world_tname = "ode.World";
static const char *space_tname = "ode.Space";
static const char *body_tname  = "ode.Body";
static const char *geom_tname  = "ode.Geom";
static const char *mass_tname  = "ode.Mass";
static const char *joint_group_tname = "ode.JointGroup";
static const char *joint_tname = "ode.Joint";
static const char *contact_tname = "ode.Contact";

void setup_ode(lua_State *L, int honey_tbl)
{
	/* setup metatables */
	NEW_METATABLE(world_tname);
	NEW_METATABLE(space_tname);
	NEW_METATABLE(body_tname);
	NEW_METATABLE(geom_tname);
	NEW_METATABLE(mass_tname);
	NEW_METATABLE(joint_group_tname);
	NEW_METATABLE(joint_tname);
	NEW_METATABLE(contact_tname);


	/* create main table */
	hs_create_table(L,
		hs_str_cfunc("InitODE", init_ode),
		hs_str_cfunc("CloseODE", close_ode),

		hs_str_cfunc("WorldCreate", world_create),
		hs_str_cfunc("WorldDestroy", world_destroy),
		hs_str_cfunc("WorldSetGravity", world_set_gravity),
		hs_str_cfunc("WorldGetGravity", world_get_gravity),
		hs_str_cfunc("WorldSetCFM", world_set_cfm),
		hs_str_cfunc("WorldQuickStep", world_quickstep),

		hs_str_cfunc("HashSpaceCreate", hash_space_create),
		hs_str_cfunc("SpaceDestroy", space_destroy),
		hs_str_cfunc("SpaceCollide", space_collide),

		hs_str_cfunc("JointGroupCreate", joint_group_create),
		hs_str_cfunc("JointGroupDestroy", joint_group_destroy),
		hs_str_cfunc("JointGroupEmpty", joint_group_empty),

		hs_str_cfunc("JointCreateContact", joint_create_contact),
		hs_str_cfunc("JointAttach", joint_attach),

		hs_str_cfunc("BodyCreate", body_create),
		hs_str_cfunc("BodySetMass", body_set_mass),
		hs_str_cfunc("BodySetPosition", body_set_position),
		hs_str_cfunc("BodyGetPosition", body_get_position),
		hs_str_cfunc("BodyAddForce", body_add_force),
		hs_str_cfunc("BodyGetForce", body_get_force),
		hs_str_cfunc("BodySetLinearVel", body_set_linear_vel),

		hs_str_cfunc("CreateSphere", create_sphere),
		hs_str_cfunc("CreatePlane", create_plane),
		hs_str_cfunc("GeomGetClass", geom_get_class),
		hs_str_cfunc("GeomSetBody", geom_set_body),
		hs_str_cfunc("GeomGetBody", geom_get_body),
		hs_str_cfunc("Collide", collide),

		hs_str_cfunc("MassCreate", mass_create),
		hs_str_cfunc("MassSetSphere", mass_set_sphere),

		hs_str_cfunc("ContactCreate", contact_create),
		hs_str_cfunc("ContactSurfaceSetMode", contact_surface_set_mode),
		hs_str_cfunc("ContactSurfaceSetMu", contact_surface_set_mu),
		hs_str_cfunc("ContactSurfaceSetBounce", contact_surface_set_bounce),
		hs_str_cfunc("ContactSurfaceSetBounceVel", contact_surface_set_bounce_vel),
		hs_str_cfunc("ContactSurfaceSetSoftCFM", contact_surface_set_soft_cfm),

		/* contact surface mode enum */
		hs_str_int("ContactBounce", dContactBounce),
		hs_str_int("ContactSoftCFM", dContactSoftCFM),

		/* helpful */
		hs_str_num("Infinity", dInfinity),
	);

	lua_setfield(L, honey_tbl, "ode");
}


/* --===== setup/teardown =====-- */

int init_ode(lua_State *L)
{
	dInitODE();
	return 0;
}


int close_ode(lua_State *L)
{
	dCloseODE();
	return 0;
}


/* --===== world functions =====-- */

int world_create(lua_State *L)
{
	dWorldID *world = lua_newuserdata(L, sizeof(dWorldID *));
	*world = dWorldCreate();
	luaL_getmetatable(L, world_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int world_destroy(lua_State *L)
{
	dWorldID *world = luaL_checkudata(L, 1, world_tname);
	dWorldDestroy(*world);
	return 0;
}


int world_set_gravity(lua_State *L)
{
	dWorldID *world = luaL_checkudata(L, 1, world_tname);
	double gx = luaL_checknumber(L, 2);
	double gy = luaL_checknumber(L, 3);
	double gz = luaL_checknumber(L, 4);
	dWorldSetGravity(*world, gx, gy, gz);
	return 0;
}


int world_get_gravity(lua_State *L)
{
	dWorldID *world = luaL_checkudata(L, 1, world_tname);
	dVector3 result;
	dWorldGetGravity(*world, result);
	lua_pushnumber(L, result[0]);
	lua_pushnumber(L, result[1]);
	lua_pushnumber(L, result[2]);
	return 3;
}


int world_set_cfm(lua_State *L)
{
	dWorldID *world = luaL_checkudata(L, 1, world_tname);
	double cfm = luaL_checknumber(L, 2);
	dWorldSetCFM(*world, cfm);
	return 0;
}


int world_quickstep(lua_State *L)
{
	dWorldID *world = luaL_checkudata(L, 1, world_tname);
	double stepsize = luaL_checknumber(L, 2);
	dWorldQuickStep(*world, stepsize);
	return 0;
}


/* --===== space functions =====-- */

int hash_space_create(lua_State *L)
{
	dSpaceID *space = lua_newuserdata(L, sizeof(dSpaceID *));
	*space = dHashSpaceCreate(NULL);
	luaL_getmetatable(L, space_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int space_destroy(lua_State *L)
{
	dSpaceID *space = luaL_checkudata(L, 1, space_tname);
	dSpaceDestroy(*space);
	return 0;
}


static void near_callback(void *data, dGeomID o1, dGeomID o2)
{
	lua_State *L = data;
	lua_pushvalue(L, 2); /* user callback should be at stack index 2 */

	dGeomID *ptr1 = lua_newuserdata(L, sizeof(dGeomID *));
	*ptr1 = o1;
	luaL_getmetatable(L, geom_tname);
	lua_setmetatable(L, -2);

	dGeomID *ptr2 = lua_newuserdata(L, sizeof(dGeomID *));
	*ptr2 = o2;
	luaL_getmetatable(L, geom_tname);
	lua_setmetatable(L, -2);

	lua_call(L, 2, 0);
}

int space_collide(lua_State *L)
{
	dSpaceID *space = luaL_checkudata(L, 1, space_tname);
	luaL_argcheck(L, lua_type(L, 2) == LUA_TFUNCTION, 2, "");
	dSpaceCollide(*space, L, near_callback);
	return 0;
}


/* --===== joint group functions =====-- */

int joint_group_create(lua_State *L)
{
	dJointGroupID *group = lua_newuserdata(L, sizeof(dJointGroupID *));
	*group = dJointGroupCreate(0);
	luaL_getmetatable(L, joint_group_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int joint_group_destroy(lua_State *L)
{
	dJointGroupID *group = luaL_checkudata(L, 1, joint_group_tname);
	dJointGroupDestroy(*group);
	return 0;
}


int joint_group_empty(lua_State *L)
{
	dJointGroupID *group = luaL_checkudata(L, 1, joint_group_tname);
	dJointGroupEmpty(*group);
	return 0;
}


/* --===== joint functions =====-- */

int joint_create_contact(lua_State *L)
{
	dWorldID *world = luaL_checkudata(L, 1, world_tname);
	dJointGroupID group = NULL;
	if (lua_type(L, 2) != LUA_TNIL) {
		dJointGroupID *ptr = luaL_checkudata(L, 2, joint_group_tname);
		group = *ptr;
	}
	dContact *contact = luaL_checkudata(L, 3, contact_tname);

	dJointID *joint = lua_newuserdata(L, sizeof(dJointID *));
	*joint = dJointCreateContact(*world, group, contact);
	luaL_getmetatable(L, joint_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int joint_attach(lua_State *L)
{
	dJointID *joint = luaL_checkudata(L, 1, joint_tname);
	dBodyID *body1 = luaL_checkudata(L, 2, body_tname);
	dBodyID *body2 = luaL_checkudata(L, 3, body_tname);

	dJointAttach(*joint, *body1, *body2);
	return 0;
}




/* --===== body functions =====-- */

int body_create(lua_State *L)
{
	dWorldID *world = luaL_checkudata(L, 1, world_tname);

	dBodyID *body = lua_newuserdata(L, sizeof(dBodyID *));
	*body = dBodyCreate(*world);
	luaL_getmetatable(L, body_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int body_set_mass(lua_State *L)
{
	dBodyID *body = luaL_checkudata(L, 1, body_tname);
	dMass *mass = luaL_checkudata(L, 2, mass_tname);

	dBodySetMass(*body, mass);
	return 0;
}


int body_set_position(lua_State *L)
{
	dBodyID *body = luaL_checkudata(L, 1, body_tname);
	double x = luaL_checknumber(L, 2);
	double y = luaL_checknumber(L, 3);
	double z = luaL_checknumber(L, 4);

	dBodySetPosition(*body, x, y, z);
	return 0;
}


int body_get_position(lua_State *L)
{
	dBodyID *body = luaL_checkudata(L, 1, body_tname);
	const dReal *pos = dBodyGetPosition(*body);

	lua_pushnumber(L, pos[0]);
	lua_pushnumber(L, pos[1]);
	lua_pushnumber(L, pos[2]);
	return 3;
}


int body_add_force(lua_State *L)
{
	dBodyID *body = luaL_checkudata(L, 1, body_tname);
	double fx = luaL_checknumber(L, 2);
	double fy = luaL_checknumber(L, 3);
	double fz = luaL_checknumber(L, 4);

	dBodyAddForce(*body, fx, fy, fz);
	return 0;
}


int body_get_force(lua_State *L)
{
	dBodyID *body = luaL_checkudata(L, 1, body_tname);
	
	const dReal *f = dBodyGetForce(*body);
	lua_pushnumber(L, f[0]);
	lua_pushnumber(L, f[1]);
	lua_pushnumber(L, f[2]);
	return 3;
}


int body_set_linear_vel(lua_State *L)
{
	dBodyID *body = luaL_checkudata(L, 1, body_tname);
	double vx = luaL_checknumber(L, 2);
	double vy = luaL_checknumber(L, 3);
	double vz = luaL_checknumber(L, 4);

	dBodySetLinearVel(*body, vx, vy, vz);
	return 0;
}


/* --===== geom functions =====-- */

int create_sphere(lua_State *L)
{
	dSpaceID *space = luaL_checkudata(L, 1, space_tname);
	double radius = luaL_checknumber(L, 2);

	dGeomID *geom = lua_newuserdata(L, sizeof(dGeomID *));
	*geom = dCreateSphere(*space, radius);
	luaL_getmetatable(L, geom_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int create_plane(lua_State *L)
{
	dSpaceID *space = luaL_checkudata(L, 1, space_tname);
	double a = luaL_checknumber(L, 2);
	double b = luaL_checknumber(L, 3);
	double c = luaL_checknumber(L, 4);
	double d = luaL_checknumber(L, 5);

	dGeomID *geom = lua_newuserdata(L, sizeof(dGeomID *));
	*geom = dCreatePlane(*space, a, b, c, d);
	luaL_getmetatable(L, geom_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int geom_get_class(lua_State *L)
{
	dGeomID *geom = luaL_checkudata(L, 1, geom_tname);
	int class = dGeomGetClass(*geom);
	lua_pushinteger(L, class);
	return 1;
}


int geom_set_body(lua_State *L)
{
	dGeomID *geom = luaL_checkudata(L, 1, geom_tname);
	dBodyID *body = luaL_checkudata(L, 2, body_tname);
	dGeomSetBody(*geom, *body);
	return 0;
}


int geom_get_body(lua_State *L)
{
	dGeomID *geom = luaL_checkudata(L, 1, geom_tname);

	dBodyID *body = lua_newuserdata(L, sizeof(dBodyID *));
	*body = dGeomGetBody(*geom);
	luaL_getmetatable(L, body_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int collide(lua_State *L)
{
	dGeomID *o1 = luaL_checkudata(L, 1, geom_tname);
	dGeomID *o2 = luaL_checkudata(L, 2, geom_tname);
	dContact *contact = luaL_checkudata(L, 3, contact_tname);

	int count = dCollide(*o1, *o2, 1, &(contact->geom), sizeof(dContactGeom));
	lua_pushinteger(L, count);
	return 1;
}


/* --===== mass functions =====-- */

int mass_create(lua_State *L)
{
	lua_newuserdata(L, sizeof(dMass));
	luaL_getmetatable(L, mass_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int mass_set_sphere(lua_State *L)
{
	dMass *mass = luaL_checkudata(L, 1, mass_tname);
	double density = luaL_checknumber(L, 2);
	double radius = luaL_checknumber(L, 3);

	dMassSetSphere(mass, density, radius);
	return 0;
}


/* --===== contact functions =====-- */

int contact_create(lua_State *L)
{
	lua_newuserdata(L, sizeof(dContact));
	luaL_getmetatable(L, contact_tname);
	lua_setmetatable(L, -2);
	return 1;
}


int contact_surface_set_mode(lua_State *L)
{
	dContact *contact = luaL_checkudata(L, 1, contact_tname);
	int mode = luaL_checkinteger(L, 2);
	contact->surface.mode = mode;
	return 0;
}


int contact_surface_set_mu(lua_State *L)
{
	dContact *contact = luaL_checkudata(L, 1, contact_tname);
	double mu = luaL_checknumber(L, 2);
	contact->surface.mu = mu;
	return 0;
}


int contact_surface_set_bounce(lua_State *L)
{
	dContact *contact = luaL_checkudata(L, 1, contact_tname);
	double bounce = luaL_checknumber(L, 2);
	contact->surface.bounce = bounce;
	return 0;
}


int contact_surface_set_bounce_vel(lua_State *L)
{
	dContact *contact = luaL_checkudata(L, 1, contact_tname);
	double bounce_vel = luaL_checknumber(L, 2);
	contact->surface.bounce_vel = bounce_vel;
	return 0;
}


int contact_surface_set_soft_cfm(lua_State *L)
{
	dContact *contact = luaL_checkudata(L, 1, contact_tname);
	double soft_cfm = luaL_checknumber(L, 2);
	contact->surface.soft_cfm = soft_cfm;
	return 0;
}