From d6c6cbaee6db78fbdb6014f578495e5c97bdaa53 Mon Sep 17 00:00:00 2001 From: ejcoumans Date: Sun, 29 Oct 2006 03:36:16 +0000 Subject: [PATCH] enabled the vehicle demo again (still needs lots of tuning before it drives well) fixed some warnings --- Demos/VehicleDemo/VehicleDemo.cpp | 180 ++++++++---------- Demos/VehicleDemo/VehicleDemo.h | 10 +- .../btOverlappingPairCache.h | 2 +- .../CollisionDispatch/btCollisionDispatcher.h | 2 +- .../CollisionDispatch/btCollisionObject.h | 10 +- .../CollisionDispatch/btCollisionWorld.h | 2 +- .../btSimulationIslandManager.cpp | 6 +- .../CollisionDispatch/btUnionFind.h | 2 +- .../CollisionShapes/btCompoundShape.h | 2 +- .../Dynamics/btDiscreteDynamicsWorld.cpp | 12 +- src/BulletDynamics/Dynamics/btDynamicsWorld.h | 6 + .../Vehicle/btRaycastVehicle.cpp | 38 +++- src/BulletDynamics/Vehicle/btRaycastVehicle.h | 20 +- src/LinearMath/btDefaultMotionState.h | 2 +- src/btBulletCollisionCommon.h | 7 +- src/btBulletDynamicsCommon.h | 3 + 16 files changed, 179 insertions(+), 125 deletions(-) diff --git a/Demos/VehicleDemo/VehicleDemo.cpp b/Demos/VehicleDemo/VehicleDemo.cpp index 01853106e..112424c35 100644 --- a/Demos/VehicleDemo/VehicleDemo.cpp +++ b/Demos/VehicleDemo/VehicleDemo.cpp @@ -13,33 +13,16 @@ subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ -//Ignore this USE_PARALLEL_DISPATCHER define, it is for future optimizations -//#define USE_PARALLEL_DISPATCHER 1 /// September 2006: VehicleDemo is work in progress, this file is mostly just a placeholder /// This VehicleDemo file is very early in development, please check it later -#include "CcdPhysicsEnvironment.h" -#include "ParallelPhysicsEnvironment.h" -#include "CcdPhysicsController.h" #include "btBulletDynamicsCommon.h" -#include "PHY_IVehicle.h" - -#include "ParallelIslandDispatcher.h" - -#include "LinearMath/btQuickprof.h" -#include "LinearMath/btIDebugDraw.h" #include "GLDebugDrawer.h" - -#include "PHY_Pro.h" -#include "BMF_Api.h" #include //printf debugging -float deltaTime = 1.f/60.f; - - #include "GL_ShapeDrawer.h" #include "GlutStuff.h" @@ -49,14 +32,14 @@ const int maxProxies = 32766; const int maxOverlap = 65535; -DefaultMotionState wheelMotionState[4]; +btDefaultMotionState wheelMotionState[4]; ///PHY_IVehicle is the interface behind the constraint that implements the raycast vehicle (WrapperVehicle which holds a btRaycastVehicle) ///notice that for higher-quality slow-moving vehicles, another approach might be better ///implementing explicit hinged-wheel constraints with cylinder collision, rather then raycasts -PHY_IVehicle* gVehicleConstraint=0; +//PHY_IVehicle* gVehicleConstraint=0; float gEngineForce = 0.f; -float maxEngineForce = 1000.f; +float maxEngineForce = 100.f; float gVehicleSteering = 0.f; float steeringIncrement = 0.1f; float steeringClamp = 0.3f; @@ -104,32 +87,15 @@ m_maxCameraDistance(10.f) void VehicleDemo::setupPhysics() { - btCollisionDispatcher* dispatcher = new btCollisionDispatcher(); - ParallelIslandDispatcher* dispatcher2 = new ParallelIslandDispatcher(); - - btVector3 worldAabbMin(-30000,-30000,-30000); - btVector3 worldAabbMax(30000,30000,30000); - btOverlappingPairCache* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies); - //OverlappingPairCache* broadphase = new btSimpleBroadphase(maxProxies,maxOverlap); - -#ifdef USE_PARALLEL_DISPATCHER - m_physicsEnvironmentPtr = new ParallelPhysicsEnvironment(dispatcher2,broadphase); -#else - m_physicsEnvironmentPtr = new CcdPhysicsEnvironment(dispatcher,broadphase); -#endif - m_physicsEnvironmentPtr->setDeactivationTime(2.f); - - m_physicsEnvironmentPtr->setDebugDrawer(&debugDrawer); - - m_physicsEnvironmentPtr->setGravity(0,-10,0);//0,0);//-10,0); - int i; btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50)); + m_dynamicsWorld = new btDiscreteDynamicsWorld(); + #define USE_TRIMESH_GROUND 1 #ifdef USE_TRIMESH_GROUND - + int i; const float TRIANGLE_SIZE=20.f; @@ -152,7 +118,7 @@ const float TRIANGLE_SIZE=20.f; { for (int j=0;jSyncMotionStates(0.f); - /// create vehicle { - int constraintId; - - constraintId =m_physicsEnvironmentPtr->createConstraint( - m_carChassis,0, - PHY_VEHICLE_CONSTRAINT, - 0,0,0, - 0,0,0); - - ///never deactivate the vehicle - m_carChassis->getRigidBody()->SetActivationState(DISABLE_DEACTIVATION); - gVehicleConstraint = m_physicsEnvironmentPtr->getVehicleConstraint(constraintId); + m_vehicleRayCaster = new btDefaultVehicleRaycaster(m_dynamicsWorld); + m_vehicle = new btRaycastVehicle(m_tuning,m_carChassis,m_vehicleRayCaster); + + ///never deactivate the vehicle + m_carChassis->SetActivationState(DISABLE_DEACTIVATION); + m_dynamicsWorld->addVehicle(m_vehicle); + + btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),0,2*CUBE_HALF_EXTENTS-wheelRadius); - btRaycastVehicle::btVehicleTuning tuning; bool isFrontWheel=true; int rightIndex = 0; int upIndex = 1; int forwardIndex = 2; - gVehicleConstraint->setCoordinateSystem(rightIndex,upIndex,forwardIndex); - - gVehicleConstraint->addWheel(&wheelMotionState[0], - (PHY__Vector3&)connectionPointCS0, - (PHY__Vector3&)wheelDirectionCS0,(PHY__Vector3&)wheelAxleCS,suspensionRestLength,wheelRadius,isFrontWheel); + m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex); + m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),0,2*CUBE_HALF_EXTENTS-wheelRadius); - gVehicleConstraint->addWheel(&wheelMotionState[1], - (PHY__Vector3&)connectionPointCS0, - (PHY__Vector3&)wheelDirectionCS0,(PHY__Vector3&)wheelAxleCS,suspensionRestLength,wheelRadius,isFrontWheel); - + m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),0,-2*CUBE_HALF_EXTENTS+wheelRadius); isFrontWheel = false; - gVehicleConstraint->addWheel(&wheelMotionState[2], - (PHY__Vector3&)connectionPointCS0, - (PHY__Vector3&)wheelDirectionCS0,(PHY__Vector3&)wheelAxleCS,suspensionRestLength,wheelRadius,isFrontWheel); - + m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),0,-2*CUBE_HALF_EXTENTS+wheelRadius); - gVehicleConstraint->addWheel(&wheelMotionState[3], - (PHY__Vector3&)connectionPointCS0, - (PHY__Vector3&)wheelDirectionCS0,(PHY__Vector3&)wheelAxleCS,suspensionRestLength,wheelRadius,isFrontWheel); + m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); - - - gVehicleConstraint->SetSuspensionStiffness(suspensionStiffness,0); +/* gVehicleConstraint->SetSuspensionStiffness(suspensionStiffness,0); gVehicleConstraint->SetSuspensionStiffness(suspensionStiffness,1); gVehicleConstraint->SetSuspensionStiffness(suspensionStiffness,2); gVehicleConstraint->SetSuspensionStiffness(suspensionStiffness,3); @@ -262,6 +209,7 @@ const float TRIANGLE_SIZE=20.f; gVehicleConstraint->SetWheelFriction(wheelFriction,1); gVehicleConstraint->SetWheelFriction(wheelFriction,2); gVehicleConstraint->SetWheelFriction(wheelFriction,3); + */ } @@ -275,6 +223,7 @@ const float TRIANGLE_SIZE=20.f; //to be implemented by the demo void VehicleDemo::renderme() { + updateCamera(); debugDrawer.setDebugMode(getDebugMode()); @@ -284,38 +233,69 @@ void VehicleDemo::renderme() btCylinderShapeX wheelShape(btVector3(wheelWidth,wheelRadius,wheelRadius)); btVector3 wheelColor(1,0,0); - for (i=0;i<4;i++) + for (i=0;igetNumWheels();i++) { + //synchronize the wheels with the (interpolated) chassis worldtransform + m_vehicle->updateWheelTransform(i); //draw wheels (cylinders) - wheelMotionState[i].m_worldTransform.getOpenGLMatrix(m); + m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m); GL_ShapeDrawer::drawOpenGL(m,&wheelShape,wheelColor,getDebugMode()); } + DemoApplication::renderme(); } void VehicleDemo::clientMoveAndDisplay() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + float dt = m_clock.getTimeMilliseconds() * 0.001f; + m_clock.reset(); + + if (m_dynamicsWorld) + { + //during idle mode, just run 1 simulation step maximum + int maxSimSubSteps = m_idle ? 1 : 1; + if (m_idle) + dt = 1.0/420.f; -{ - int steerWheelIndex = 2; - gVehicleConstraint->applyEngineForce(gEngineForce,steerWheelIndex); - steerWheelIndex = 3; - gVehicleConstraint->applyEngineForce(gEngineForce,steerWheelIndex); - - steerWheelIndex = 0; - gVehicleConstraint->setSteeringValue(gVehicleSteering,steerWheelIndex); - steerWheelIndex = 1; - gVehicleConstraint->setSteeringValue(gVehicleSteering,steerWheelIndex); + int numSimSteps = m_dynamicsWorld->stepSimulation(dt,maxSimSubSteps); + if (!numSimSteps) + printf("Interpolated transforms\n"); + else + { + if (numSimSteps > maxSimSubSteps) + { + //detect dropping frames + printf("Dropped (%i) simulation steps out of %i\n",numSimSteps - maxSimSubSteps,numSimSteps); + } else + { + printf("Simulated (%i) steps\n",numSimSteps); + } + } } - m_physicsEnvironmentPtr->proceedDeltaTime(0.f,deltaTime); - + + + { + int steerWheelIndex = 2; + m_vehicle->applyEngineForce(gEngineForce,steerWheelIndex); + steerWheelIndex = 3; + m_vehicle->applyEngineForce(gEngineForce,steerWheelIndex); + + steerWheelIndex = 0; + m_vehicle->setSteeringValue(gVehicleSteering,steerWheelIndex); + steerWheelIndex = 1; + m_vehicle->setSteeringValue(gVehicleSteering,steerWheelIndex); + + } + + #ifdef USE_QUICKPROF @@ -328,6 +308,8 @@ void VehicleDemo::clientMoveAndDisplay() #ifdef USE_QUICKPROF btProfiler::endBlock("render"); #endif + + glFlush(); glutSwapBuffers(); @@ -341,9 +323,10 @@ void VehicleDemo::displayCallback(void) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - m_physicsEnvironmentPtr->UpdateAabbs(deltaTime); + + m_dynamicsWorld->updateAabbs(); //draw contactpoints - m_physicsEnvironmentPtr->CallbackTriggers(); + //m_physicsEnvironmentPtr->CallbackTriggers(); renderme(); @@ -359,14 +342,14 @@ void VehicleDemo::clientResetScene() { gEngineForce = 0.f; gVehicleSteering = 0.f; - m_carChassis->setPosition(0,0,0); - m_carChassis->setOrientation(0,0,0,1); + m_carChassis->setCenterOfMassTransform(btTransform::getIdentity()); } void VehicleDemo::specialKeyboard(int key, int x, int y) { + printf("key = %i x=%i y=%i\n",key,x,y); switch (key) @@ -404,6 +387,7 @@ void VehicleDemo::specialKeyboard(int key, int x, int y) // glutPostRedisplay(); + } @@ -414,8 +398,11 @@ void VehicleDemo::updateCamera() glMatrixMode(GL_PROJECTION); glLoadIdentity(); + btTransform chassisWorldTrans; + //look at the vehicle - m_cameraTargetPosition = m_carChassis->getRigidBody()->m_worldTransform.getOrigin(); + m_carChassis->getMotionState()->getWorldTransform(chassisWorldTrans); + m_cameraTargetPosition = chassisWorldTrans.getOrigin(); //interpolate the camera height m_cameraPosition[1] = (15.0*m_cameraPosition[1] + m_cameraTargetPosition[1] + m_cameraHeight)/16.0; @@ -443,5 +430,6 @@ void VehicleDemo::updateCamera() m_cameraUp.getX(),m_cameraUp.getY(),m_cameraUp.getZ()); glMatrixMode(GL_MODELVIEW); + } diff --git a/Demos/VehicleDemo/VehicleDemo.h b/Demos/VehicleDemo/VehicleDemo.h index cb4679289..ad9b2391d 100644 --- a/Demos/VehicleDemo/VehicleDemo.h +++ b/Demos/VehicleDemo/VehicleDemo.h @@ -15,6 +15,10 @@ subject to the following restrictions: #ifndef VEHICLE_DEMO_H #define VEHICLE_DEMO_H +class btVehicleTuning; +struct btVehicleRaycaster; +#include "BulletDynamics/Vehicle/btRaycastVehicle.h" + #include "DemoApplication.h" ///VehicleDemo shows how to setup and use the built-in raycast vehicle @@ -22,8 +26,12 @@ class VehicleDemo : public DemoApplication { public: - CcdPhysicsController* m_carChassis; + btRigidBody* m_carChassis; + btRaycastVehicle::btVehicleTuning m_tuning; + btVehicleRaycaster* m_vehicleRayCaster; + btRaycastVehicle* m_vehicle; + float m_cameraHeight; float m_minCameraDistance; diff --git a/src/BulletCollision/BroadphaseCollision/btOverlappingPairCache.h b/src/BulletCollision/BroadphaseCollision/btOverlappingPairCache.h index ab444b925..90f965ee3 100644 --- a/src/BulletCollision/BroadphaseCollision/btOverlappingPairCache.h +++ b/src/BulletCollision/BroadphaseCollision/btOverlappingPairCache.h @@ -66,7 +66,7 @@ class btOverlappingPairCache : public btBroadphaseInterface inline bool needsBroadphaseCollision(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1) const { - bool collides = proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask; + bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0; collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask); return collides; diff --git a/src/BulletCollision/CollisionDispatch/btCollisionDispatcher.h b/src/BulletCollision/CollisionDispatch/btCollisionDispatcher.h index e8af08a6a..4e97bce9d 100644 --- a/src/BulletCollision/CollisionDispatch/btCollisionDispatcher.h +++ b/src/BulletCollision/CollisionDispatch/btCollisionDispatcher.h @@ -78,7 +78,7 @@ public: int getNumManifolds() const { - return m_manifoldsPtr.size(); + return int( m_manifoldsPtr.size()); } btPersistentManifold** getInternalManifoldPointer() diff --git a/src/BulletCollision/CollisionDispatch/btCollisionObject.h b/src/BulletCollision/CollisionDispatch/btCollisionObject.h index 3838fc989..9b90d50e3 100644 --- a/src/BulletCollision/CollisionDispatch/btCollisionObject.h +++ b/src/BulletCollision/CollisionDispatch/btCollisionObject.h @@ -79,26 +79,26 @@ struct btCollisionObject inline bool mergesSimulationIslands() const { ///static objects, kinematic and object without contact response don't merge islands - return !(m_collisionFlags & (CF_STATIC_OBJECT | CF_KINEMATIC_OJBECT | CF_NO_CONTACT_RESPONSE) ); + return ((m_collisionFlags & (CF_STATIC_OBJECT | CF_KINEMATIC_OJBECT | CF_NO_CONTACT_RESPONSE) )==0); } inline bool isStaticObject() const { - return m_collisionFlags & CF_STATIC_OBJECT; + return (m_collisionFlags & CF_STATIC_OBJECT) != 0; } inline bool isKinematicObject() const { - return m_collisionFlags & CF_KINEMATIC_OJBECT; + return (m_collisionFlags & CF_KINEMATIC_OJBECT) != 0; } inline bool isStaticOrKinematicObject() const { - return m_collisionFlags & (CF_KINEMATIC_OJBECT | CF_STATIC_OBJECT); + return (m_collisionFlags & (CF_KINEMATIC_OJBECT | CF_STATIC_OBJECT)) != 0 ; } inline bool hasContactResponse() const { - return !(m_collisionFlags & CF_NO_CONTACT_RESPONSE); + return (m_collisionFlags & CF_NO_CONTACT_RESPONSE)==0; } diff --git a/src/BulletCollision/CollisionDispatch/btCollisionWorld.h b/src/BulletCollision/CollisionDispatch/btCollisionWorld.h index 2280399b2..a1cf3a0e5 100644 --- a/src/BulletCollision/CollisionDispatch/btCollisionWorld.h +++ b/src/BulletCollision/CollisionDispatch/btCollisionWorld.h @@ -205,7 +205,7 @@ public: int getNumCollisionObjects() const { - return m_collisionObjects.size(); + return int(m_collisionObjects.size()); } /// rayTest performs a raycast on all objects in the btCollisionWorld, and calls the resultCallback diff --git a/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp b/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp index b9ee58068..c82e0d268 100644 --- a/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp +++ b/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp @@ -1,4 +1,6 @@ + +#include "LinearMath/btScalar.h" #include "btSimulationIslandManager.h" #include "BulletCollision/BroadphaseCollision/btDispatcher.h" #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" @@ -51,7 +53,7 @@ void btSimulationIslandManager::findUnions(btDispatcher* dispatcher) void btSimulationIslandManager::updateActivationState(btCollisionWorld* colWorld,btDispatcher* dispatcher) { - initUnionFind(colWorld->getCollisionObjectArray().size()); + initUnionFind( int (colWorld->getCollisionObjectArray().size())); // put the index into m_controllers into m_tag { @@ -253,7 +255,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, } } - int numManifolds = islandmanifold.size(); + int numManifolds = int (islandmanifold.size()); // Sort manifolds, based on islands // Sort the vector using predicate and std::sort diff --git a/src/BulletCollision/CollisionDispatch/btUnionFind.h b/src/BulletCollision/CollisionDispatch/btUnionFind.h index 8db1580e4..b1baca9ff 100644 --- a/src/BulletCollision/CollisionDispatch/btUnionFind.h +++ b/src/BulletCollision/CollisionDispatch/btUnionFind.h @@ -45,7 +45,7 @@ class btUnionFind inline int getNumElements() const { - return m_elements.size(); + return int(m_elements.size()); } inline bool isRoot(int x) const { diff --git a/src/BulletCollision/CollisionShapes/btCompoundShape.h b/src/BulletCollision/CollisionShapes/btCompoundShape.h index 65a6809d4..c810a6548 100644 --- a/src/BulletCollision/CollisionShapes/btCompoundShape.h +++ b/src/BulletCollision/CollisionShapes/btCompoundShape.h @@ -46,7 +46,7 @@ public: int getNumChildShapes() const { - return m_childShapes.size(); + return int (m_childShapes.size()); } btCollisionShape* getChildShape(int index) diff --git a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp index 7701e3d4d..a037e9623 100644 --- a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp +++ b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp @@ -390,7 +390,7 @@ void btDiscreteDynamicsWorld::solveNoncontactConstraints(btContactSolverInfo& so BEGIN_PROFILE("solveNoncontactConstraints"); int i; - int numConstraints = m_constraints.size(); + int numConstraints = int(m_constraints.size()); ///constraint preparation: building jacobians for (i=0;i< numConstraints ; i++ ) @@ -424,7 +424,7 @@ void btDiscreteDynamicsWorld::calculateSimulationIslands() { int i; - int numConstraints = m_constraints.size(); + int numConstraints = int(m_constraints.size()); for (i=0;i< numConstraints ; i++ ) { btTypedConstraint* constraint = m_constraints[i]; @@ -691,10 +691,10 @@ void btDiscreteDynamicsWorld::debugDrawObject(const btTransform& worldTransform, float radius = coneShape->getRadius();//+coneShape->getMargin(); float height = coneShape->getHeight();//+coneShape->getMargin(); btVector3 start = worldTransform.getOrigin(); - getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0,0,0.5*height),start+worldTransform.getBasis() * btVector3(radius,0,-0.5*height),color); - getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0,0,0.5*height),start+worldTransform.getBasis() * btVector3(-radius,0,-0.5*height),color); - getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0,0,0.5*height),start+worldTransform.getBasis() * btVector3(0,radius,-0.5*height),color); - getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0,0,0.5*height),start+worldTransform.getBasis() * btVector3(0,-radius,-0.5*height),color); + getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0.f,0.f,0.5f*height),start+worldTransform.getBasis() * btVector3(radius,0.f,-0.5f*height),color); + getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0.f,0.f,0.5f*height),start+worldTransform.getBasis() * btVector3(-radius,0.f,-0.5f*height),color); + getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0.f,0.f,0.5f*height),start+worldTransform.getBasis() * btVector3(0.f,radius,-0.5f*height),color); + getDebugDrawer()->drawLine(start+worldTransform.getBasis() * btVector3(0.f,0.f,0.5f*height),start+worldTransform.getBasis() * btVector3(0.f,-radius,-0.5f*height),color); break; } diff --git a/src/BulletDynamics/Dynamics/btDynamicsWorld.h b/src/BulletDynamics/Dynamics/btDynamicsWorld.h index 9a527250a..7aebf533e 100644 --- a/src/BulletDynamics/Dynamics/btDynamicsWorld.h +++ b/src/BulletDynamics/Dynamics/btDynamicsWorld.h @@ -18,6 +18,7 @@ subject to the following restrictions: #include "BulletCollision/CollisionDispatch/btCollisionWorld.h" class btTypedConstraint; +class btRaycastVehicle; ///btDynamicsWorld is the baseclass for several dynamics implementation, basic, discrete, parallel, and continuous class btDynamicsWorld : public btCollisionWorld @@ -47,6 +48,11 @@ class btDynamicsWorld : public btCollisionWorld virtual void removeConstraint(btTypedConstraint* constraint) {}; + virtual void addVehicle(btRaycastVehicle* vehicle) {}; + + virtual void removeVehicle(btRaycastVehicle* vehicle) {}; + + virtual void setDebugDrawer(btIDebugDraw* debugDrawer) = 0; virtual btIDebugDraw* getDebugDrawer() = 0; diff --git a/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp b/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp index 63615cdb9..544d3e594 100644 --- a/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp +++ b/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp @@ -9,11 +9,12 @@ * It is provided "as is" without express or implied warranty. */ +#include "LinearMath/btVector3.h" #include "btRaycastVehicle.h" #include "BulletDynamics/ConstraintSolver/btSolve2LinearConstraint.h" #include "BulletDynamics/ConstraintSolver/btJacobianEntry.h" #include "LinearMath/btQuaternion.h" -#include "LinearMath/btVector3.h" +#include "BulletDynamics/Dynamics/btDynamicsWorld.h" #include "btVehicleRaycaster.h" #include "btWheelInfo.h" @@ -141,8 +142,12 @@ void btRaycastVehicle::updateWheelTransformsWS(btWheelInfo& wheel ) { wheel.m_raycastInfo.m_isInContact = false; - const btTransform& chassisTrans = getRigidBody()->getCenterOfMassTransform(); - + btTransform chassisTrans; + if (getRigidBody()->getMotionState()) + getRigidBody()->getMotionState()->getWorldTransform(chassisTrans); + else + chassisTrans = getRigidBody()->getCenterOfMassTransform(); + wheel.m_raycastInfo.m_hardPointWS = chassisTrans( wheel.m_chassisConnectionPointCS ); wheel.m_raycastInfo.m_wheelDirectionWS = chassisTrans.getBasis() * wheel.m_wheelDirectionCS ; wheel.m_raycastInfo.m_wheelAxleWS = chassisTrans.getBasis() * wheel.m_wheelAxleCS; @@ -166,6 +171,8 @@ btScalar btRaycastVehicle::rayCast(btWheelInfo& wheel) btVehicleRaycaster::btVehicleRaycasterResult rayResults; + assert(m_vehicleRaycaster); + void* object = m_vehicleRaycaster->castRay(source,target,rayResults); wheel.m_raycastInfo.m_groundObject = 0; @@ -593,3 +600,28 @@ void btRaycastVehicle::updateFriction(btScalar timeStep) delete[]forwardImpulse; delete[] sideImpulse; } + + +void* btDefaultVehicleRaycaster::castRay(const btVector3& from,const btVector3& to, btVehicleRaycasterResult& result) +{ +// RayResultCallback& resultCallback; + + btCollisionWorld::ClosestRayResultCallback rayCallback(from,to); + + m_dynamicsWorld->rayTest(from, to, rayCallback); + + if (rayCallback.HasHit()) + { + + btRigidBody* body = btRigidBody::upcast(rayCallback.m_collisionObject); + if (body) + { + result.m_hitPointInWorld = rayCallback.m_hitPointWorld; + result.m_hitNormalInWorld = rayCallback.m_hitNormalWorld; + result.m_hitNormalInWorld.normalize(); + result.m_distFraction = rayCallback.m_closestHitFraction; + return body; + } + } + return 0; +} diff --git a/src/BulletDynamics/Vehicle/btRaycastVehicle.h b/src/BulletDynamics/Vehicle/btRaycastVehicle.h index e29d7bf99..0e452565e 100644 --- a/src/BulletDynamics/Vehicle/btRaycastVehicle.h +++ b/src/BulletDynamics/Vehicle/btRaycastVehicle.h @@ -13,11 +13,11 @@ #include "BulletDynamics/Dynamics/btRigidBody.h" #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h" - +#include "btVehicleRaycaster.h" +class btDynamicsWorld; #include "btWheelInfo.h" -struct btVehicleRaycaster; class btVehicleTuning; ///rayCast vehicle, very special constraint that turn a rigidbody into a vehicle. @@ -93,7 +93,7 @@ public: btWheelInfo& addWheel( const btVector3& connectionPointCS0, const btVector3& wheelDirectionCS0,const btVector3& wheelAxleCS,btScalar suspensionRestLength,btScalar wheelRadius,const btVehicleTuning& tuning, bool isFrontWheel); inline int getNumWheels() const { - return m_wheelInfo.size(); + return int (m_wheelInfo.size()); } std::vector m_wheelInfo; @@ -163,5 +163,19 @@ public: }; +class btDefaultVehicleRaycaster : public btVehicleRaycaster +{ + btDynamicsWorld* m_dynamicsWorld; +public: + btDefaultVehicleRaycaster(btDynamicsWorld* world) + :m_dynamicsWorld(world) + { + } + + virtual void* castRay(const btVector3& from,const btVector3& to, btVehicleRaycasterResult& result); + +}; + + #endif //RAYCASTVEHICLE_H diff --git a/src/LinearMath/btDefaultMotionState.h b/src/LinearMath/btDefaultMotionState.h index 862ff26b1..5e5fd0c25 100644 --- a/src/LinearMath/btDefaultMotionState.h +++ b/src/LinearMath/btDefaultMotionState.h @@ -9,7 +9,7 @@ struct btDefaultMotionState : public btMotionState btTransform m_startWorldTrans; void* m_userPointer; - btDefaultMotionState(const btTransform& startTrans,const btTransform& centerOfMassOffset = btTransform::getIdentity()) + btDefaultMotionState(const btTransform& startTrans = btTransform::getIdentity(),const btTransform& centerOfMassOffset = btTransform::getIdentity()) : m_graphicsWorldTrans(startTrans), m_centerOfMassOffset(centerOfMassOffset), m_startWorldTrans(startTrans), diff --git a/src/btBulletCollisionCommon.h b/src/btBulletCollisionCommon.h index e9cfea535..9eb52b235 100644 --- a/src/btBulletCollisionCommon.h +++ b/src/btBulletCollisionCommon.h @@ -49,11 +49,12 @@ subject to the following restrictions: #include "BulletCollision/BroadphaseCollision/btAxisSweep3.h" -///Math library +///Math library & Utils #include "LinearMath/btQuaternion.h" #include "LinearMath/btTransform.h" - - +#include "LinearMath/btDefaultMotionState.h" +#include "LinearMath/btQuickprof.h" +#include "LinearMath/btIDebugDraw.h" #endif //BULLET_COLLISION_COMMON_H diff --git a/src/btBulletDynamicsCommon.h b/src/btBulletDynamicsCommon.h index 3a40fad99..bd411c6c2 100644 --- a/src/btBulletDynamicsCommon.h +++ b/src/btBulletDynamicsCommon.h @@ -34,5 +34,8 @@ subject to the following restrictions: + + + #endif //BULLET_DYNAMICS_COMMON_H