Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/ogrebullet/Dynamics/Constraints/OgreBulletDynamicsRaycastVehicle.cpp @ 2276

Last change on this file since 2276 was 2192, checked in by rgrieder, 16 years ago

Reverted all changes of attempt to update physics branch.

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1/***************************************************************************
2
3This source file is part of OGREBULLET
4(Object-oriented Graphics Rendering Engine Bullet Wrapper)
5For the latest info, see http://www.ogre3d.org/phpBB2addons/viewforum.php?f=10
6
7Copyright (c) 2007 tuan.kuranes@gmail.com (Use it Freely, even Statically, but have to contribute any changes)
8
9
10
11This program is free software; you can redistribute it and/or modify it under
12the terms of the GPL General Public License with runtime exception as published by the Free Software
13Foundation; either version 2 of the License, or (at your option) any later
14version.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GPL General Public License with runtime exception for more details.
19
20You should have received a copy of the GPL General Public License with runtime exception along with
21this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22Place - Suite 330, Boston, MA 02111-1307, USA, or go to
23http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24-----------------------------------------------------------------------------
25*/
26
27#include "OgreBulletDynamics.h"
28
29#include "OgreBulletDynamicsWorld.h"
30#include "OgreBulletDynamicsRigidBody.h"
31#include "OgreBulletDynamicsConstraint.h"
32#include "Constraints/OgreBulletDynamicsRaycastVehicle.h"
33
34using namespace Ogre;
35
36namespace OgreBulletDynamics
37{
38    // -------------------------------------------------------------------------
39    VehicleRayCaster::VehicleRayCaster(DynamicsWorld *world)
40    {
41        mBulletVehicleRayCaster = new btDefaultVehicleRaycaster(world->getBulletDynamicsWorld ());
42    }
43    // -------------------------------------------------------------------------
44    VehicleRayCaster::~VehicleRayCaster()
45    {
46        delete mBulletVehicleRayCaster;
47    }   
48    // -------------------------------------------------------------------------
49    VehicleTuning::VehicleTuning(const Ogre::Real suspensionStiffness,
50                                const Ogre::Real suspensionCompression,
51                                const Ogre::Real suspensionDamping,
52                                const Ogre::Real maxSuspensionTravelCm,
53                                const Ogre::Real frictionSlip)
54    {
55        mBulletTuning = new btRaycastVehicle::btVehicleTuning();
56
57        mBulletTuning->m_suspensionStiffness =  suspensionStiffness;
58        mBulletTuning->m_suspensionCompression = suspensionCompression;
59        mBulletTuning->m_suspensionDamping = suspensionDamping;
60        mBulletTuning->m_maxSuspensionTravelCm = maxSuspensionTravelCm;
61        mBulletTuning->m_frictionSlip = frictionSlip;
62    }
63    // -------------------------------------------------------------------------
64    VehicleTuning::~VehicleTuning()
65    {
66    }
67    // -------------------------------------------------------------------------
68    RaycastVehicle::RaycastVehicle(WheeledRigidBody *body, 
69                                VehicleTuning *vt,
70                                VehicleRayCaster *caster) :
71        TypedConstraint(body),
72        mTuning(vt),
73        mRayCaster(caster)
74    {
75        if (mRayCaster == 0)
76            mRayCaster = new VehicleRayCaster(mWorld);
77
78        btRaycastVehicle *v = new btRaycastVehicle(
79                *(mTuning->getBulletTuning()),
80                body->getBulletRigidBody (), 
81                mRayCaster->getBulletVehicleRayCaster()
82                );
83        mConstraint = v;
84        mWorld->addVehicle(this);
85        body->setVehicle (this);
86    }
87    // -------------------------------------------------------------------------
88    RaycastVehicle::~RaycastVehicle()
89    {
90    }
91    // -------------------------------------------------------------------------
92    void RaycastVehicle::setCoordinateSystem(int rightIndex,int upIndex,int forwardIndex)
93    {
94        getBulletVehicle()->setCoordinateSystem(rightIndex, upIndex, forwardIndex);
95    }
96    // -------------------------------------------------------------------------
97    void RaycastVehicle::addWheel(SceneNode *node,
98                                  const Ogre::Vector3 &connectionPoint,
99                                  const Ogre::Vector3 &wheelDirection,
100                                  const Ogre::Vector3 &wheelAxle,
101
102                                  const Ogre::Real suspensionRestLength,
103                                  const Ogre::Real wheelRadius,
104                                  const bool isFrontWheel,
105                                 
106                                  const Ogre::Real wheelFriction,
107                                  const Ogre::Real rollInfluence)
108    {
109        btRaycastVehicle *v = static_cast<btRaycastVehicle *> (mConstraint);
110
111        mWheelsInfo.push_back (
112            &v->addWheel(OgreBulletCollisions::OgreBtConverter::to(connectionPoint),
113                        OgreBulletCollisions::OgreBtConverter::to(wheelDirection),
114                        OgreBulletCollisions::OgreBtConverter::to(wheelAxle),
115                        suspensionRestLength,
116                        wheelRadius, 
117                        *(mTuning->getBulletTuning ()),
118                        isFrontWheel));
119
120        {
121            const size_t wheelCurrent = mWheelsInfo.size() - 1;
122            mWheelsInfo[wheelCurrent]->m_suspensionStiffness = mTuning->getBulletTuning()->m_suspensionStiffness;
123            mWheelsInfo[wheelCurrent]->m_wheelsDampingRelaxation = mTuning->getBulletTuning()->m_suspensionDamping;
124            mWheelsInfo[wheelCurrent]->m_wheelsDampingCompression = mTuning->getBulletTuning()->m_suspensionCompression;
125
126            mWheelsInfo[wheelCurrent]->m_frictionSlip = wheelFriction;
127            mWheelsInfo[wheelCurrent]->m_rollInfluence = rollInfluence;
128        }
129
130        // create wheel scene Node
131        {
132            node->setPosition (connectionPoint);
133
134            mWheelNodes.push_back ( node);
135            const size_t wheelCurrent = mWheelsInfo.size() - 1;
136
137            //mWheelsInfo[wheelCurrent]->
138        }
139    }
140    // -------------------------------------------------------------------------
141    void RaycastVehicle::setWheelsAttached()
142    {
143        btRaycastVehicle *v = static_cast<btRaycastVehicle *> (mConstraint);
144        for (int i=0; i < v->getNumWheels(); i++)
145        {
146            btWheelInfo& wheel = v->getWheelInfo(i);
147
148            wheel.m_suspensionStiffness = mTuning->getBulletTuning()->m_suspensionStiffness;
149            wheel.m_wheelsDampingRelaxation = mTuning->getBulletTuning()->m_suspensionDamping;
150            wheel.m_wheelsDampingCompression = mTuning->getBulletTuning()->m_suspensionCompression;
151
152            //wheel.m_frictionSlip = mBulletTuning->getBulletTuning()->m_wheelFriction;
153            //wheel.m_rollInfluence = mBulletTuning->getBulletTuning()->m_rollInfluence;
154        }
155    }
156    // -------------------------------------------------------------------------
157    void RaycastVehicle::setTransform()
158    {
159        //should update wheels as well ?
160        for (int i=0; i < getBulletVehicle()->getNumWheels(); i++)
161        {
162            //synchronize the wheels with the (interpolated) chassis world transform
163            getBulletVehicle()->updateWheelTransform(i, true);
164            //draw wheels (cylinders)
165            const btTransform &w = getBulletVehicle()->getWheelInfo(i).m_worldTransform;
166
167            mWheelNodes[i]->setPosition (w.getOrigin()[0], w.getOrigin()[1], w.getOrigin()[2]);
168            mWheelNodes[i]->setOrientation (w.getRotation().getW(), w.getRotation().getX(), w.getRotation().getY(), w.getRotation().getZ());
169
170        }
171    }
172    // -------------------------------------------------------------------------
173    void RaycastVehicle::applyEngineForce (float engineForce, int wheel)
174    {
175        getBulletVehicle()->applyEngineForce (engineForce, wheel);
176    }
177    // -------------------------------------------------------------------------
178    void RaycastVehicle::setSteeringValue(float steering, int wheel)
179    {
180        getBulletVehicle()->setSteeringValue (steering, wheel);
181    }
182    // -------------------------------------------------------------------------
183}
Note: See TracBrowser for help on using the repository browser.