diff options
Diffstat (limited to 'libraries/ode-0.9/ode/demo/demo_chain1.c')
-rw-r--r-- | libraries/ode-0.9/ode/demo/demo_chain1.c | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/libraries/ode-0.9/ode/demo/demo_chain1.c b/libraries/ode-0.9/ode/demo/demo_chain1.c new file mode 100644 index 0000000..e20c5dc --- /dev/null +++ b/libraries/ode-0.9/ode/demo/demo_chain1.c | |||
@@ -0,0 +1,171 @@ | |||
1 | /************************************************************************* | ||
2 | * * | ||
3 | * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. * | ||
4 | * All rights reserved. Email: russ@q12.org Web: www.q12.org * | ||
5 | * * | ||
6 | * This library is free software; you can redistribute it and/or * | ||
7 | * modify it under the terms of EITHER: * | ||
8 | * (1) The GNU Lesser General Public License as published by the Free * | ||
9 | * Software Foundation; either version 2.1 of the License, or (at * | ||
10 | * your option) any later version. The text of the GNU Lesser * | ||
11 | * General Public License is included with this library in the * | ||
12 | * file LICENSE.TXT. * | ||
13 | * (2) The BSD-style license that is included with this library in * | ||
14 | * the file LICENSE-BSD.TXT. * | ||
15 | * * | ||
16 | * This library is distributed in the hope that it will be useful, * | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * | ||
19 | * LICENSE.TXT and LICENSE-BSD.TXT for more details. * | ||
20 | * * | ||
21 | *************************************************************************/ | ||
22 | |||
23 | /* exercise the C interface */ | ||
24 | |||
25 | #include <stdio.h> | ||
26 | #include "ode/ode.h" | ||
27 | #include "drawstuff/drawstuff.h" | ||
28 | |||
29 | #ifdef _MSC_VER | ||
30 | #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints | ||
31 | #endif | ||
32 | |||
33 | /* select correct drawing functions */ | ||
34 | |||
35 | #ifdef dDOUBLE | ||
36 | #define dsDrawBox dsDrawBoxD | ||
37 | #define dsDrawSphere dsDrawSphereD | ||
38 | #define dsDrawCylinder dsDrawCylinderD | ||
39 | #define dsDrawCapsule dsDrawCapsuleD | ||
40 | #endif | ||
41 | |||
42 | |||
43 | /* some constants */ | ||
44 | |||
45 | #define NUM 10 /* number of boxes */ | ||
46 | #define SIDE (0.2) /* side length of a box */ | ||
47 | #define MASS (1.0) /* mass of a box */ | ||
48 | #define RADIUS (0.1732f) /* sphere radius */ | ||
49 | |||
50 | |||
51 | /* dynamics and collision objects */ | ||
52 | |||
53 | static dWorldID world; | ||
54 | static dSpaceID space; | ||
55 | static dBodyID body[NUM]; | ||
56 | static dJointID joint[NUM-1]; | ||
57 | static dJointGroupID contactgroup; | ||
58 | static dGeomID sphere[NUM]; | ||
59 | |||
60 | |||
61 | /* this is called by dSpaceCollide when two objects in space are | ||
62 | * potentially colliding. | ||
63 | */ | ||
64 | |||
65 | static void nearCallback (void *data, dGeomID o1, dGeomID o2) | ||
66 | { | ||
67 | /* exit without doing anything if the two bodies are connected by a joint */ | ||
68 | dBodyID b1,b2; | ||
69 | dContact contact; | ||
70 | |||
71 | b1 = dGeomGetBody(o1); | ||
72 | b2 = dGeomGetBody(o2); | ||
73 | if (b1 && b2 && dAreConnected (b1,b2)) return; | ||
74 | |||
75 | contact.surface.mode = 0; | ||
76 | contact.surface.mu = 0.1; | ||
77 | contact.surface.mu2 = 0; | ||
78 | if (dCollide (o1,o2,1,&contact.geom,sizeof(dContactGeom))) { | ||
79 | dJointID c = dJointCreateContact (world,contactgroup,&contact); | ||
80 | dJointAttach (c,b1,b2); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | |||
85 | /* start simulation - set viewpoint */ | ||
86 | |||
87 | static void start() | ||
88 | { | ||
89 | static float xyz[3] = {2.1640f,-1.3079f,1.7600f}; | ||
90 | static float hpr[3] = {125.5000f,-17.0000f,0.0000f}; | ||
91 | dsSetViewpoint (xyz,hpr); | ||
92 | } | ||
93 | |||
94 | |||
95 | /* simulation loop */ | ||
96 | |||
97 | static void simLoop (int pause) | ||
98 | { | ||
99 | int i; | ||
100 | if (!pause) { | ||
101 | static double angle = 0; | ||
102 | angle += 0.05; | ||
103 | dBodyAddForce (body[NUM-1],0,0,1.5*(sin(angle)+1.0)); | ||
104 | |||
105 | dSpaceCollide (space,0,&nearCallback); | ||
106 | dWorldStep (world,0.05); | ||
107 | |||
108 | /* remove all contact joints */ | ||
109 | dJointGroupEmpty (contactgroup); | ||
110 | } | ||
111 | |||
112 | dsSetColor (1,1,0); | ||
113 | dsSetTexture (DS_WOOD); | ||
114 | for (i=0; i<NUM; i++) dsDrawSphere (dBodyGetPosition(body[i]), | ||
115 | dBodyGetRotation(body[i]),RADIUS); | ||
116 | } | ||
117 | |||
118 | |||
119 | int main (int argc, char **argv) | ||
120 | { | ||
121 | int i; | ||
122 | dReal k; | ||
123 | dMass m; | ||
124 | |||
125 | /* setup pointers to drawstuff callback functions */ | ||
126 | dsFunctions fn; | ||
127 | fn.version = DS_VERSION; | ||
128 | fn.start = &start; | ||
129 | fn.step = &simLoop; | ||
130 | fn.command = 0; | ||
131 | fn.stop = 0; | ||
132 | fn.path_to_textures = "../../drawstuff/textures"; | ||
133 | if(argc==2) | ||
134 | { | ||
135 | fn.path_to_textures = argv[1]; | ||
136 | } | ||
137 | |||
138 | /* create world */ | ||
139 | dInitODE(); | ||
140 | world = dWorldCreate(); | ||
141 | space = dHashSpaceCreate (0); | ||
142 | contactgroup = dJointGroupCreate (1000000); | ||
143 | dWorldSetGravity (world,0,0,-0.5); | ||
144 | dCreatePlane (space,0,0,1,0); | ||
145 | |||
146 | for (i=0; i<NUM; i++) { | ||
147 | body[i] = dBodyCreate (world); | ||
148 | k = i*SIDE; | ||
149 | dBodySetPosition (body[i],k,k,k+0.4); | ||
150 | dMassSetBox (&m,1,SIDE,SIDE,SIDE); | ||
151 | dMassAdjust (&m,MASS); | ||
152 | dBodySetMass (body[i],&m); | ||
153 | sphere[i] = dCreateSphere (space,RADIUS); | ||
154 | dGeomSetBody (sphere[i],body[i]); | ||
155 | } | ||
156 | for (i=0; i<(NUM-1); i++) { | ||
157 | joint[i] = dJointCreateBall (world,0); | ||
158 | dJointAttach (joint[i],body[i],body[i+1]); | ||
159 | k = (i+0.5)*SIDE; | ||
160 | dJointSetBallAnchor (joint[i],k,k,k+0.4); | ||
161 | } | ||
162 | |||
163 | /* run simulation */ | ||
164 | dsSimulationLoop (argc,argv,352,288,&fn); | ||
165 | |||
166 | dJointGroupDestroy (contactgroup); | ||
167 | dSpaceDestroy (space); | ||
168 | dWorldDestroy (world); | ||
169 | dCloseODE(); | ||
170 | return 0; | ||
171 | } | ||