/************************************************************************* * * * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. * * All rights reserved. Email: russ@q12.org Web: www.q12.org * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of EITHER: * * (1) The GNU Lesser General Public License as published by the Free * * Software Foundation; either version 2.1 of the License, or (at * * your option) any later version. The text of the GNU Lesser * * General Public License is included with this library in the * * file LICENSE.TXT. * * (2) The BSD-style license that is included with this library in * * the file LICENSE-BSD.TXT. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * * LICENSE.TXT and LICENSE-BSD.TXT for more details. * * * *************************************************************************/ /* buggy with suspension. this also shows you how to use geom groups. */ #include #include #include #ifdef _MSC_VER #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints #endif // select correct drawing functions #ifdef dDOUBLE #define dsDrawBox dsDrawBoxD #define dsDrawSphere dsDrawSphereD #define dsDrawCylinder dsDrawCylinderD #define dsDrawCappedCylinder dsDrawCappedCylinderD #endif // some constants #define LENGTH 0.7 // chassis length #define WIDTH 0.4 // chassis width #define HEIGHT 0.2 // chassis height #define RADIUS 0.22 // wheel radius #define STARTZ 0.4 // starting height of chassis #define CMASS 1 // chassis mass #define WMASS 0.2 // wheel mass // dynamics and collision objects (chassis, 4 wheels, environment, obstacles, chain) static dWorldID world; static dSpaceID space; // chain stuff static const float chain_radius = 0.1; static const float chain_mass = 0.1; static const int chain_num = 10; static dBodyID chain_body[chain_num]; static dGeomID chain_geom[chain_num]; static dJointID chain_joint[chain_num-1]; // 1 chasses, 4 wheels static dBodyID body[5]; // joint[0] is left front wheel, joint[1] is right front wheel static dJointID joint[4]; static int joint_exists[4]; static dJointGroupID contactgroup; static dGeomID ground; static dSpaceID car_space; static dGeomID box[1]; static dGeomID sphere[4]; static dGeomID ground_box; static const int obstacle_num = 25; static dGeomID obstacle[obstacle_num]; // things that the user controls static dReal speed=0,steer=0; // user commands // this is called by dSpaceCollide when two objects in space are // potentially colliding. static void nearCallback (void *data, dGeomID o1, dGeomID o2) { int i,n; // // do not collide objects that are connected // dBodyID b1 = dGeomGetBody (o1), // b2 = dGeomGetBody (o2); // if (b1 && b2 && dAreConnected(b1, b2)) return; const int N = 10; dContact contact[N]; n = dCollide (o1,o2,N,&contact[0].geom,sizeof(dContact)); if (n > 0) { for (i=0; i 0.1) v = 0.1; if (v < -0.1) v = -0.1; v *= 10.0; dJointSetHinge2Param (joint[i],dParamVel,v); dJointSetHinge2Param (joint[i],dParamFMax,0.2); dJointSetHinge2Param (joint[i],dParamLoStop,-0.75); dJointSetHinge2Param (joint[i],dParamHiStop,0.75); dJointSetHinge2Param (joint[i],dParamFudgeFactor,0.1); } } dSpaceCollide (space,0,&nearCallback); //dWorldStep (world,0.05); dWorldStepFast1 (world,0.05,5); // remove all contact joints dJointGroupEmpty (contactgroup); } dsSetColor (0,1,1); dsSetTexture (DS_WOOD); dReal sides[3] = {LENGTH,WIDTH,HEIGHT}; dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides); dsSetColor (1,1,1); for (i=1; i<=4; i++) dsDrawCylinder (dBodyGetPosition(body[i]), dBodyGetRotation(body[i]), 0.2, RADIUS); dVector3 ss; dGeomBoxGetLengths (ground_box,ss); dsDrawBox (dGeomGetPosition(ground_box),dGeomGetRotation(ground_box),ss); dsSetColor (1,0,0); for (i=0; i