Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ode/ode-0.9/ode/demo/demo_cylvssphere.cpp @ 216

Last change on this file since 216 was 216, checked in by mathiask, 16 years ago

[Physik] add ode-0.9

File size: 6.5 KB
Line 
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// Test for cylinder vs sphere, by Bram Stolk
24
25#include <ode/config.h>
26#include <assert.h>
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30#include <ode/ode.h>
31#include <drawstuff/drawstuff.h>
32
33#ifdef _MSC_VER
34#pragma warning(disable:4244 4305)  // for VC++, no precision loss complaints
35#endif
36
37
38// dynamics and collision objects (chassis, 3 wheels, environment)
39
40static dWorldID world;
41static dSpaceID space;
42
43static dBodyID cylbody;
44static dGeomID cylgeom;
45
46static dBodyID sphbody;
47static dGeomID sphgeom;
48
49static dJointGroupID contactgroup;
50static dGeomID world_mesh;
51
52static bool show_contacts = true;
53
54#define CYLRADIUS    0.6
55#define CYLLENGTH    2.0
56#define SPHERERADIUS 0.5
57
58   
59#ifdef dDOUBLE
60#define dsDrawBox dsDrawBoxD
61#define dsDrawLine dsDrawLineD
62#endif
63
64
65
66// this is called by dSpaceCollide when two objects in space are
67// potentially colliding.
68
69static void nearCallback (void *data, dGeomID o1, dGeomID o2)
70{
71  assert(o1);
72  assert(o2);
73
74  if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
75  {
76    fprintf(stderr,"testing space %p %p\n", o1,o2);
77    // colliding a space with something
78    dSpaceCollide2(o1,o2,data,&nearCallback);
79    // Note we do not want to test intersections within a space,
80    // only between spaces.
81    return;
82  }
83
84  const int N = 32;
85  dContact contact[N];
86  int n = dCollide (o1,o2,N,&(contact[0].geom),sizeof(dContact));
87  if (n > 0) 
88  {
89    for (int i=0; i<n; i++) 
90    {
91      contact[i].surface.mode = 0;
92      contact[i].surface.mu = 50.0; // was: dInfinity
93      dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
94      dJointAttach (c, dGeomGetBody(contact[i].geom.g1), dGeomGetBody(contact[i].geom.g2));
95      if (show_contacts) 
96      {
97        dMatrix3 RI;
98        dRSetIdentity (RI);
99        const dReal ss[3] = {0.12,0.12,0.12};
100        dsSetColorAlpha (0,0,1,0.5);
101        dsDrawBox (contact[i].geom.pos,RI,ss);
102        dReal *pos  = contact[i].geom.pos;
103        dReal depth = contact[i].geom.depth;
104        dReal *norm = contact[i].geom.normal;
105        dReal endp[3] = {pos[0]+depth*norm[0], pos[1]+depth*norm[1], pos[2]+depth*norm[2]};
106        dsSetColorAlpha (1,1,1,1);
107        dsDrawLine (contact[i].geom.pos, endp);
108      }
109    }
110  }
111}
112
113
114// start simulation - set viewpoint
115
116static void start()
117{
118  static float xyz[3] = {-8,-9,3};
119  static float hpr[3] = {45.0000f,-27.5000f,0.0000f};
120  dsSetViewpoint (xyz,hpr);
121}
122
123
124// called when a key pressed
125
126static void command (int cmd)
127{
128  switch (cmd) 
129  {
130    case ' ':
131      break;
132  }
133}
134
135
136
137// simulation loop
138
139static void simLoop (int pause)
140{
141  dSpaceCollide (space,0,&nearCallback);
142  if (!pause)
143  {
144    dWorldQuickStep (world, 0.01); // 100 Hz
145  }
146  dJointGroupEmpty (contactgroup);
147
148  dsSetColorAlpha (1,1,0,0.5);
149
150  const dReal *CPos = dBodyGetPosition(cylbody);
151  const dReal *CRot = dBodyGetRotation(cylbody);
152  float cpos[3] = {CPos[0], CPos[1], CPos[2]};
153  float crot[12] = { CRot[0], CRot[1], CRot[2], CRot[3], CRot[4], CRot[5], CRot[6], CRot[7], CRot[8], CRot[9], CRot[10], CRot[11] };
154  dsDrawCylinder
155  (
156    cpos,
157    crot,
158    CYLLENGTH,
159    CYLRADIUS
160  ); // single precision
161
162  const dReal *SPos = dBodyGetPosition(sphbody);
163  const dReal *SRot = dBodyGetRotation(sphbody);
164  float spos[3] = {SPos[0], SPos[1], SPos[2]};
165  float srot[12] = { SRot[0], SRot[1], SRot[2], SRot[3], SRot[4], SRot[5], SRot[6], SRot[7], SRot[8], SRot[9], SRot[10], SRot[11] };
166  dsDrawSphere
167  (
168    spos,
169    srot,
170    SPHERERADIUS
171  ); // single precision
172}
173
174
175int main (int argc, char **argv)
176{
177  dMass m;
178
179  // setup pointers to drawstuff callback functions
180  dsFunctions fn;
181  fn.version = DS_VERSION;
182  fn.start = &start;
183  fn.step = &simLoop;
184  fn.command = &command;
185  fn.stop = 0;
186  fn.path_to_textures = "../../drawstuff/textures";
187  if(argc==2)
188    fn.path_to_textures = argv[1];
189
190  // create world
191  dInitODE();
192  world = dWorldCreate();
193  space = dHashSpaceCreate (0);
194  contactgroup = dJointGroupCreate (0);
195  dWorldSetGravity (world,0,0,-9.8);
196  dWorldSetQuickStepNumIterations (world, 32);
197
198  dCreatePlane (space,0,0,1, 0.0);
199
200  cylbody = dBodyCreate (world);
201  dQuaternion q;
202#if 0
203  dQFromAxisAndAngle (q,1,0,0,M_PI*0.5);
204#else
205//  dQFromAxisAndAngle (q,1,0,0, M_PI * 1.0);
206  dQFromAxisAndAngle (q,1,0,0, M_PI * -0.77);
207#endif
208  dBodySetQuaternion (cylbody,q);
209  dMassSetCylinder (&m,1.0,3,CYLRADIUS,CYLLENGTH);
210  dBodySetMass (cylbody,&m);
211  cylgeom = dCreateCylinder(0, CYLRADIUS, CYLLENGTH);
212  dGeomSetBody (cylgeom,cylbody);
213  dBodySetPosition (cylbody, 0, 0, 3);
214  dSpaceAdd (space, cylgeom);
215
216  sphbody = dBodyCreate (world);
217  dMassSetSphere (&m,1,SPHERERADIUS);
218  dBodySetMass (sphbody,&m);
219  sphgeom = dCreateSphere(0, SPHERERADIUS);
220  dGeomSetBody (sphgeom,sphbody);
221  dBodySetPosition (sphbody, 0, 0, 5.5);
222  dSpaceAdd (space, sphgeom);
223
224  // run simulation
225  dsSimulationLoop (argc,argv,352,288,&fn);
226
227  dJointGroupEmpty (contactgroup);
228  dJointGroupDestroy (contactgroup);
229
230  dGeomDestroy(sphgeom);
231  dGeomDestroy (cylgeom);
232
233  dSpaceDestroy (space);
234  dWorldDestroy (world);
235  dCloseODE();
236  return 0;
237}
238
Note: See TracBrowser for help on using the repository browser.