r1cky17
12-10-2007 13:15:20
hi, i try to connect the vehicle and the osm file..
i can show the map, the car, and the autoTracking camera.
but the vehicle and the map it's not connected.
like i can't drive the car in the map
and if i close the application an error is coming.
how to connect that car and the map?
i have try to connect the car and the map, it's work. but i add autoTraking camera, and it can't connect.
r1cky17
12-10-2007 15:35:29
i can connect to the map right now
but it's still error if i close the application
can anyone look for my source here, and find what wrong with my source.
i think it should be in the destructor. but i don't know where is it.
thx
#pragma once
class SimpleVehicle : public OgreNewt::Vehicle
{
// Attributes ------------------------------------------------------------------------------
protected:
SceneManager *mSceneMgr;
SceneNode *mCarNode; // Main character node
SceneNode *mSightNode; // "Sight" node - The character is supposed to be looking here
SceneNode *mCameraNode; // Node for the chase camera
Entity *mCar; // Character entity
String mName;
public:
// we also make our own Tire class based on the OgreNewt::Vehicle::Tire class, to store some extra information
class SimpleTire : public OgreNewt::Vehicle::Tire
{
public:
SimpleTire(OgreNewt::Vehicle* vehicle, Ogre::Quaternion localorient, Ogre::Vector3 localpos, Ogre::Vector3 pin,
Ogre::Real mass, Ogre::Real width, Ogre::Real radius, Ogre::Real susShock, Ogre::Real susSpring, Ogre::Real susLength, int ColID, bool steer ) : Tire(vehicle, localorient, localpos, pin, mass, width, radius, susShock, susSpring, susLength, ColID )
{
mSteeringTire = steer;
}
~SimpleTire()
{
// destroy entity, and scene node.
Ogre::Entity* ent = (Ogre::Entity*)m_node->getAttachedObject(static_cast<unsigned short>(0));
m_node->detachAllObjects();
m_node->getCreator()->destroyEntity( ent );
m_node->getParentSceneNode()->removeAndDestroyChild( m_node->getName() );
}
bool mSteeringTire;
};
SimpleVehicle (String name, SceneManager *sceneMgr, OgreNewt::World* world, Ogre::Vector3& position, Ogre::Quaternion& orient, Ogre::Real mass) : OgreNewt::Vehicle()
{
// Setup basic member references
mWorld = world;
mName = name;
mSceneMgr = sceneMgr;
mPosition = position;
mEntityCount = 5;
// Setup basic node structure to handle 3rd person cameras
mCarNode = mSceneMgr->getRootSceneNode ()->createChildSceneNode (mName);
mSightNode = mCarNode->createChildSceneNode (mName + "_sight", Vector3 (0, 0, 100));
mCameraNode = mCarNode->createChildSceneNode (mName + "_camera", Vector3 (0, 50, -100));
// Give this character a shape :)
Ogre::Vector3 size(1, 1, 1);
OgreNewt::Body* bod = makeCarBody(size, mPosition, orient, mass);
init( bod, Ogre::Vector3(0,1,0) );
}
~SimpleVehicle (void)
{
std::vector< SimpleTire* > toDelete;
// delete tire objects.
for (SimpleTire* tire = (SimpleTire*)getFirstTire(); tire; tire=(SimpleTire*)getNextTire(tire))
{
toDelete.push_back( tire );
}
while (!toDelete.empty())
{
SimpleTire* tire = toDelete.back();
delete tire;
toDelete.pop_back();
}
// finally, destroy entity and node from chassis.
Ogre::Entity* ent = (Ogre::Entity*)((Ogre::SceneNode*)m_chassis->getOgreNode())->getAttachedObject(static_cast<unsigned short>(0));
((Ogre::SceneNode*)m_chassis->getOgreNode())->detachAllObjects();
((Ogre::SceneNode*)m_chassis->getOgreNode())->getCreator()->destroyEntity( ent );
((Ogre::SceneNode*)m_chassis->getOgreNode())->getParentSceneNode()->removeAndDestroyChild( m_chassis->getOgreNode()->getName() );
destroy();
mCarNode->detachAllObjects ();
if (mCar)
delete mCar;
mCarNode->removeAndDestroyAllChildren ();
mSceneMgr->destroySceneNode (mName + "_sight");
mSceneMgr->destroySceneNode (mName + "_camera");
mSceneMgr->destroySceneNode (mName);
}
// this is a function created specifically for this vehicle to allow control of it.
void setTorqueSteering(Ogre::Real torque, Ogre::Degree steering) { mTorque = torque; mSteering = steering; }
OgreNewt::Body* getChassisBody() { return mChassisBody; }
Ogre::Vector3 getPosition()
{
Ogre::Vector3 Pos;
Ogre::Quaternion Orient;
mChassisBody->getPositionOrientation(Pos, Orient);
return Pos;
}
Ogre::Quaternion getOrientation()
{
Ogre::Vector3 Pos;
Ogre::Quaternion Orient;
mChassisBody->getPositionOrientation(Pos, Orient);
return Orient;
}
void setPositionOrientation(Ogre::Vector3 pos, Ogre::Quaternion orient)
{
mChassisBody->setPositionOrientation(pos, orient);
}
void update (Real elapsedTime, InputReader *input)
{
// Handle movement
if (input->isKeyDown (KC_W))
mCarNode->translate (mCarNode->getOrientation () * Vector3 (0, 0, 100 * elapsedTime));
if (input->isKeyDown (KC_S))
mCarNode->translate (mCarNode->getOrientation () * Vector3 (0, 0, -50 * elapsedTime));
if (input->isKeyDown (KC_A))
mCarNode->yaw (Radian (2 * elapsedTime));
if (input->isKeyDown (KC_D))
mCarNode->yaw (Radian (-2 * elapsedTime));
}
// Change visibility - Useful for 1st person view ;)
void setVisible (bool visible) { mCarNode->setVisible (visible); }
void setup()
{
// okay, we have the main chassis all setup. let's do a few things to it:
m_chassis->setStandardForceCallback();
// we don't want the vehicle to freeze, because we'll be unable to control it.
m_chassis->setAutoFreeze(0);
//m_chassis->setLinearDamping( 0.01f );
// okay, let's add tires!
// all offsets here are in local space of the vehicle.
Ogre::Vector3 offset(3.2,-0.5,1.7);
//Ogre::Vector3 offset(1.0,-0.2,0.2);
Ogre::Vector3 tireOffset[4];
Ogre::Vector3 axis[2];
axis[0] = tireOffset[0]-tireOffset[1];
axis[1] = tireOffset[2]-tireOffset[3];
int tireNum = 0;
for (int x=-1;x<=1;x+=2)
{
for (int z=-1;z<=1;z+=2)
{
// okay, let's create the tire itself. we'll use the OgreNewt::Vehicle::Tire class for this. most of the
// parameters are self-explanatory... try changing some of them to see what happens.
Ogre::Quaternion tireorient = Ogre::Quaternion(Ogre::Degree(0), Ogre::Vector3::UNIT_Y);
Ogre::Vector3 tirepos = offset * Ogre::Vector3(x,1,z);
Ogre::Vector3 pin(0,0,x);
//Ogre::Real mass = 150.0;
Ogre::Real mass = 50.0;
Ogre::Real width = 1.4;
Ogre::Real radius = 1.4;
Ogre::Real susShock = 30.0;
//Ogre::Real susSpring = 50.0;
Ogre::Real susSpring = 100.0;
//Ogre::Real susLength = 2;
Ogre::Real susLength = 0.5;
bool steering;
tirepos.x-=0.5;
tirepos.z+=0.1;
// first, load the visual mesh that represents the tire.
//Ogre::Entity* ent = mSceneMgr->createEntity("Tire"+Ogre::StringConverter::toString(mEntityCount++), "cartire0"+Ogre::StringConverter::toString(tireNum+1)+".mesh");
//Ogre::Entity* ent = mSceneMgr->createEntity("Tire"+Ogre::StringConverter::toString(mEntityCount++), "cabrio_wheel.mesh");
Ogre::Entity* mTire = mSceneMgr->createEntity("Tire"+Ogre::StringConverter::toString(tireNum), "cabrio_wheel.mesh");
// make a scene node for the tire.
Ogre::SceneNode* mTireNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mTireNode->attachObject( mTire );
mTireNode->setScale( Ogre::Vector3(radius, radius, width) );
if (x > 0)
steering = true;
else
steering = false;
// create the actual tire!
SimpleTire* tire = new SimpleTire(this, tireorient, tirepos, pin, mass, width, radius, susShock, susSpring, susLength, 0, steering);
tireNum++;
// attach the tire to the node.
tire->attachToNode( mTireNode );
}
}
//setTireFriction();
}
// This is the important callback, which is the meat of controlling the vehicle.
void userCallback()
{
// loop through wheels, adding torque and steering, and updating their positions.
for (SimpleTire* tire = (SimpleTire*)getFirstTire(); tire; tire=(SimpleTire*)getNextTire(tire))
{
// tire slip settings.
Ogre::Real speed = tire->getOmega();
Ogre::Real load = tire->getNormalLoad();
tire->setMaxSideSlipSpeed(speed);
tire->setSideSlipCoefficient(speed * load * load);
// set the torque and steering! non-steering tires get the torque.
// is this a steering tire?
if (tire->mSteeringTire)
tire->setSteeringAngle( mSteering );
else
{
// maintaining speed at a stable level
if (abs(speed) <= 10)
tire->setTorque( mTorque );
}
//tire slip longitudinal
speed = tire->getLongitudinalSpeed();
tire->setMaxLongitudinalSlipSpeed(speed);
tire->setLongitudinalSlipCoefficient(speed * load);
// finally, this command updates the location of the visual mesh.
tire->updateNode();
}
}
void setTireFriction()
{
const OgreNewt::MaterialID* roadMaterial = mWorld->getDefaultMaterialID();
const OgreNewt::MaterialID* carMaterial = new OgreNewt::MaterialID(mWorld);
OgreNewt::MaterialPair* roadTyreFriction = new OgreNewt::MaterialPair(mWorld, roadMaterial, carMaterial);
roadTyreFriction->setDefaultFriction(0.5, 1.0);
m_chassis->setMaterialGroupID(carMaterial);
}
OgreNewt::Body* makeCarBody( Ogre::Vector3& size, Ogre::Vector3& pos, Ogre::Quaternion& orient, Ogre::Real mass )
{
// base mass on the size of the object.
//Ogre::Real mass = size.x * size.y * size.z * 1000.0;
mMass = size.x * size.y * size.z * mass;
// calculate the inertia based on box formula and mass
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( mMass, size * 20 );
// Give this character a shape :)
mCar = mSceneMgr->createEntity (mName, "carbody.mesh");
mCarNode->setOrientation( orient );
mCarNode->attachObject (mCar);
mCarNode->setScale( size );
//make collision car to the world
OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::ConvexHull( mWorld, mCarNode );
OgreNewt::Body* bod = new OgreNewt::Body( mWorld, col );
delete col;
bod->attachToNode( mCarNode );
bod->setMassMatrix( mMass, inertia );
bod->setStandardForceCallback();
//box1->setMaterialName( "Simple/BumpyMetal" );
mCar->setNormaliseNormals(true);
bod->setPositionOrientation( pos, orient );
mChassisBody = bod;
return bod;
}
SceneNode *getSightNode () { return mSightNode; }
SceneNode *getCameraNode () { return mCameraNode; }
Vector3 getWorldPosition () { return mCarNode->getWorldPosition (); }
OgreNewt::Body* mChassisBody;
OgreNewt::World* mWorld;
Ogre::Vector3 mPosition;
// for steering, etc.
Ogre::Real mTorque;
Ogre::Degree mSteering;
int mEntityCount;
Ogre::Real mMass;
};
r1cky17
13-10-2007 08:22:50
hi, i can fix the error right now
it's on the desctrutor
this is the new code
std::vector< SimpleTire* > toDelete;
for (SimpleTire* tire = (SimpleTire*)getFirstTire(); tire; tire=(SimpleTire*)getNextTire(tire))
{
toDelete.push_back( tire );
}
while (!toDelete.empty())
{
SimpleTire* tire = toDelete.back();
delete tire;
toDelete.pop_back();
}
mCarNode->detachAllObjects ();
mCarNode->removeAndDestroyAllChildren ();
mSceneMgr->destroyEntity (mName);
mSceneMgr->destroySceneNode (mName);
but the quaternation it's not right.
i make initialitation like this
Mobil = new SimpleVehicle ("MobilPlayer", mSceneMgr, m_World, pos, Quaternion(Degree(180), Vector3::UNIT_Y), 500);
is that right? or it's quaternion is for the camera quaternion?
the body of my car is turning left, but my camera is in the front side.
Lioric
16-10-2007 03:55:39
Probably you might get a solution for your issue in the forum of the physics lib your are using in your project, as it seems its related to it