[Solved :)] My car is flying? :(

Dirso

18-11-2006 17:43:49

Hi,

I'm using OIS for input management and OgreODE for a simple project: Two cars, human controlled, racing. I used the samples .mesh files from the Demo_GranTurismOgre.
When I was changing the OIS example with the OgreODE functions I had two problems:
1) My car is floating. It don't come to the floor
2) It doesn't respond to movement keys.
BTW: I couldn't use _world->setCollisionListener(this); It didn't compile.

I tried to derivate my GameState class from OgreOde::CollisionListener but i get a compile error "`scalar destroying"

I'm working with GameState. I have the GameState Singleton base class and an IntroState and a PlayState (both derived from GameState). I also have a GameManager class derivied from OIS::KeyListener, OIS::MouseListener and it's responsible for changing the game states.

I uploaded my source code to http://www.jrsoftwares.com.br/RacingTest.zip

Thanks you very much,
Dirso

Dirso

19-11-2006 12:55:34

Hi,

Now I solved the problem about derivating my GameState class from the OgreOde::CollisionListener. But my cars are still flying, does anyone know why it could be?

Thanks,
Dirso.

luis

19-11-2006 19:00:34

did you override heightAt method ?

you should add something like this to your new class:


Real yourClass::heightAt(const Vector3& position)
{
return mTerrainGeometry->getHeightAt(position);
}

Dirso

19-11-2006 19:29:34

I'm gonna try it. I didn't think about it because there is no such function in the Demo

Dirso

20-11-2006 12:32:03

I need more help! It didn't work and I don't know how to design this function (I'm new with Ogre)

luis

20-11-2006 19:02:29

need more help! It didn't work and I don't know how to design this function (I'm new with Ogre)

then i suggest you to read the landscape demo in OgreOde it has almost all you need to know ;)

Dirso

20-11-2006 20:08:44

Thanks a lot!
Dirso

Dirso

27-12-2006 17:55:56

I'm still stuck on this!
Now I'm using http://www.ogre3d.org/wiki/index.php/Managing_Game_States_with_OGRE as a sample of how to work with Game States.
How can I make my cars to fall into the ground? Why would I need a Terrain variable if GranTurismOgre does not?

Here is my code, please tell me what is wrong. Since it works fine in a ExampleFramework derivated sample, but not with game states.

void PlayState::enter()
{
mInputDevice = InputManager::getSingletonPtr()->getInputDevice();

mRoot = Root::getSingletonPtr();

//init
_world = 0;
_time_step = 0.01;
_track = 0;

mSceneMgr = mRoot->createSceneManager(ST_GENERIC);

// Create the camera
mCamera = mSceneMgr->createCamera("PlayerCam");
mCamera->setPosition(Vector3(125,-14,8));
mCamera->lookAt(mCamera->getPosition() + Vector3(0,0,1));
mCamera->setNearClipDistance( 1 );
mCamera->setFarClipDistance( 1000 );

mViewport = mRoot->getAutoCreatedWindow()->addViewport(mCamera);

ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
mSceneMgr->setSkyBox(true,"GranTurismOgre/Skybox");

// Create a light
Light* l = mSceneMgr->createLight("MainLight");

// Accept default settings: point light, white diffuse, just set position
// NB I could attach the light to a SceneNode if I wanted it to move automatically with
// other objects, but I don't
l->setPosition(20,800,50);
l->setSpecularColour(1,0.9,0);

l->setCastShadows(true);

mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_MODULATIVE);
mSceneMgr->setShadowColour(ColourValue(0.5,0.5,0.5));

_world = new OgreOde::World(mSceneMgr);

_world->setGravity(Vector3(0,-9.80665,0));
_world->setCFM(10e-5);
_world->setERP(0.8);
_world->setAutoSleep(true);
_world->setContactCorrectionVelocity(1.0);

// Create something that will step the world automatically
_world->setCollisionListener(this);

SceneNode *track_node = mSceneMgr->getRootSceneNode()->createChildSceneNode("track");
Entity *track_mesh = mSceneMgr->createEntity("track","racingcircuit.mesh");
track_node->attachObject(track_mesh);

OgreOde::EntityInformer ei(track_mesh);
_track = ei.createStaticTriangleMesh(_world->getDefaultSpace());

_vehicle = dotOgreOdeLoader.loadVehicle ("subaru.ogreode", "Subaru");
Vector3 v_pos = mCamera->getPosition() + (mCamera->getDirection() * 15.0);
//v_pos.y += 10;
_vehicle->setPosition(v_pos);
_vehicle->getWheel(0)->setPowerFactor(1);
_vehicle->getWheel(1)->setPowerFactor(1);
_vehicle->getWheel(2)->setPowerFactor(0);
_vehicle->getWheel(3)->setPowerFactor(0);

mRoot->startRendering();
}

void PlayState::exit()
{
delete _track;
delete _world;

mSceneMgr->clearScene();
mSceneMgr->destroyAllCameras();
mRoot->getAutoCreatedWindow()->removeAllViewports();

std::cout << "PlayState exit\n";
}

douglaz

27-12-2006 18:16:02

Copy the code from frame listener:

GranTurismOgreFrameListener::GranTurismOgreFrameListener(RenderWindow* win, Camera* cam, Real time_step, Root *root, OgreOde::World *world) :
...

Because you need to setup the _stepper.

douglaz

27-12-2006 18:31:35

I have done the same thing that you is trying. The code is here (ActionState == your PlayState):

ActionState.cpp


#include <cassert>
#include "ActionState.h"

using namespace Ogre;
using namespace OgreOde;
using namespace OgreOde_Prefab;
using namespace OgreOde_Loader;

static const String carNames[] = {
"Jeep",
"JeepSway",
"Subaru"
};
static const String carFileNames[] = {
"jeep.ogreode",
"jeep.ogreode",
"subaru.ogreode"
};
static int sSelectedCar = 1;
static int maxNumCar = 3;

ActionState* ActionState::mActionState;

void ActionState::enter( void ) {
mRoot = Root::getSingletonPtr();
mOverlayMgr = OverlayManager::getSingletonPtr();
mInputDevice = InputManager::getSingletonPtr()->getKeyboard();
mSceneMgr = mRoot->getSceneManager( "ST_GENERIC" );
mCamera = mSceneMgr->createCamera( "PlayerCam" );
mViewport = mRoot->getAutoCreatedWindow()->addViewport( mCamera );

//Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);


mInfoOverlay = mOverlayMgr->getByName( "Overlay/Info" );
mActionOverlay = mOverlayMgr->getByName( "Overlay/PlayState" );
mMouseOverlay = mOverlayMgr->getByName( "Overlay/MousePointer" );

mInfoInstruction = mOverlayMgr->getOverlayElement( "Info/Instruction" );
mInfoNotification = mOverlayMgr->getOverlayElement( "Info/Notification" );
mMousePointer = mOverlayMgr->getOverlayElement( "MousePointer/Pointer" );

mInfoOverlay->show();
//mActionOverlay->show();
//mMouseOverlay->show();

mInfoInstruction->setCaption( "Press space for pause" );


_time_step = 0.01;


// Adjusting camera
mCamera->setPosition(Vector3(125,-14,8));
mCamera->lookAt(mCamera->getPosition() + Ogre::Vector3(0,0,1));
mCamera->setNearClipDistance( 1 );
mCamera->setFarClipDistance( 1000 );

// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));

// Create a light
Light* l = mSceneMgr->createLight("MainLight");
// Accept default settings: point light, white diffuse, just set position
// NB I could attach the light to a SceneNode if I wanted it to move automatically with
// other objects, but I don't
l->setPosition(20,800,50);
l->setSpecularColour(1,0.9,0);

l->setCastShadows(true);

mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_MODULATIVE);
mSceneMgr->setShadowColour(ColourValue(0.5,0.5,0.5));

_world = new OgreOde::World(mSceneMgr);

_world->setGravity(Vector3(0,-9.80665,0));
_world->setCFM(10e-5);
_world->setERP(0.8);
_world->setAutoSleep(true);
_world->setContactCorrectionVelocity(1.0);

// memory leak here
_world->setCollisionListener(new ActionState::Collision());

SceneNode *track_node = mSceneMgr->getRootSceneNode()->createChildSceneNode("track");
Entity *track_mesh = mSceneMgr->createEntity("track","racingcircuit.mesh");
track_node->attachObject(track_mesh);

OgreOde::EntityInformer ei(track_mesh);
_track = ei.createStaticTriangleMesh(_world, _world->getDefaultSpace());
track_mesh->setUserObject (_track);

// Frame listener stuff

dotOgreOdeLoader = new OgreOde_Loader::DotLoader(_world);

const int _stepper_mode_choice = 2;
const int _stepper_choice = 3;
const Ogre::Real time_scale = Ogre::Real(1.0);
const Ogre::Real max_frame_time = Ogre::Real(1.0 / 4);
const Ogre::Real frame_rate = Ogre::Real(1.0 / 60);


StepHandler::StepModeType stepModeType;

switch(_stepper_mode_choice)
{
case 0: stepModeType = StepHandler::BasicStep; break;
case 1: stepModeType = StepHandler::FastStep; break;
case 2: stepModeType = StepHandler::QuickStep; break;

default: stepModeType = StepHandler::QuickStep; break;
}

switch (_stepper_choice)
{
case 0:
_stepper = new OgreOde::StepHandler(_world,
StepHandler::QuickStep,
_time_step,
max_frame_time,
time_scale);

break;
case 1:
_stepper = new OgreOde::ExactVariableStepHandler(_world,
stepModeType,
_time_step,
max_frame_time,
time_scale);

break;
case 2:
_stepper = new OgreOde::ForwardFixedStepHandler(_world,
stepModeType,
_time_step,
max_frame_time,
time_scale);

break;
case 3:
default:
_stepper = new OgreOde::ForwardFixedInterpolatedStepHandler (_world,
stepModeType,
_time_step,
frame_rate,
max_frame_time,
time_scale);
break;
}

_stepper->setAutomatic(OgreOde::StepHandler::AutoMode_PostFrame, mRoot);

Root::getSingleton().setFrameSmoothingPeriod(5.0f);

_vehicle = 0;
changeCar();

}

void ActionState::changeCar()
{
sSelectedCar = (sSelectedCar + 1) % maxNumCar;


delete _vehicle;
_vehicle = static_cast <OgreOde_Prefab::Vehicle *> (dotOgreOdeLoader->loadObject (carFileNames[sSelectedCar], carNames[sSelectedCar]));

assert(_vehicle);

// Move the vehicle
Vector3 v_pos = mCamera->getPosition() + (mCamera->getDirection() * 15.0);
//v_pos.y += 10;
_vehicle->setPosition(v_pos);

}

// Making a CollisionListener
bool ActionState::Collision::collision(OgreOde::Contact* contact)
{
if(!OgreOde_Prefab::Vehicle::handleTyreCollision(contact))
{
contact->setBouncyness(0.0);
contact->setCoulombFriction(18.0);
}
return true;
}

void ActionState::exit( void ) {
mInfoOverlay->hide();
mActionOverlay->hide();
mMouseOverlay->hide();

mSceneMgr->clearScene();
mSceneMgr->destroyAllCameras();
mRoot->getAutoCreatedWindow()->removeAllViewports();

delete _track;
delete _world;
delete _stepper;
delete dotOgreOdeLoader;
}

void ActionState::pause( void ) {
mInfoOverlay->hide();
mActionOverlay->hide();
mMouseOverlay->hide();
}

void ActionState::resume( void ) {
mInfoOverlay->show();
//mActionOverlay->show();
//mMouseOverlay->show();

mInfoInstruction->setCaption( "Press space for pause" );
}

void ActionState::update( unsigned long lTimeElapsed ) {
if(!_stepper->isPaused())
{
// Car controls: i->accelerate, j->turn left, l->turn right, k->brake
_vehicle->setInputs(mInputDevice->isKeyDown(OIS::KC_J),
mInputDevice->isKeyDown(OIS::KC_L),
mInputDevice->isKeyDown(OIS::KC_I),
mInputDevice->isKeyDown(OIS::KC_K));
_vehicle->update(lTimeElapsed);

// Thanks to Ahmed!
const Ogre::Real followFactor = 0.2;
const Ogre::Real camHeight = 2.0;
const Ogre::Real camDistance = 7.0;
const Ogre::Real camLookAhead = 8.0;

Quaternion q = _vehicle->getSceneNode()->getOrientation();
Vector3 toCam = _vehicle->getSceneNode()->getPosition();

toCam.y += camHeight;
toCam.z -= camDistance * q.zAxis().z;
toCam.x -= camDistance * q.zAxis().x;

mCamera->move( (toCam - mCamera->getPosition()) * followFactor );
mCamera->lookAt(_vehicle->getSceneNode()->getPosition() + ((_vehicle->getSceneNode()->getOrientation() * Ogre::Vector3::UNIT_Z) * camLookAhead));
}

}

void ActionState::keyPressed( const OIS::KeyEvent &e ) {

}

void ActionState::keyReleased( const OIS::KeyEvent &e ) {
if( e.key == OIS::KC_SPACE ) {
this->pushState( PauseState::getSingletonPtr() );
}
else if( e.key == OIS::KC_ESCAPE ) {
this->requestShutdown();
}
else if(e.key == OIS::KC_N) {
changeCar();
}
}

void ActionState::mouseMoved( const OIS::MouseEvent &e ) {
//const OIS::MouseState &mouseState = e.state;
//mMousePointer->setTop( mouseState.Y.abs );
//mMousePointer->setLeft( mouseState.X.abs );
//mRotX += Degree(-mouseState.X.rel * 0.13);
//mRotY += Degree(-mouseState.Y.rel * 0.13);
}

void ActionState::mousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id ) {
}

void ActionState::mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id ) {
}

ActionState* ActionState::getSingletonPtr( void ) {
if( !mActionState ) {
mActionState = new ActionState();
}

return mActionState;
}


ActionState.h

#ifndef _ACTIONSTATE_H
#define _ACTIONSTATE_H
#include <OgreCamera.h>
#include <OgreRenderSystem.h>


#include "OgreOde_Core.h"
#include "OgreOde_Prefab.h"
#include "OgreOde_Loader.h"

#include "InputManager.h"
#include "GameState.h"
#include "PauseState.h"

#ifdef __cplusplus
extern "C"
{
#endif

class ActionState : public GameState {
public:
~ActionState( void );

void enter( void );
void exit( void );

void pause( void );
void resume( void );
void update( unsigned long lTimeElapsed );

void keyPressed( const OIS::KeyEvent &e );
void keyReleased( const OIS::KeyEvent &e );

void mouseMoved( const OIS::MouseEvent &e );
void mousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id );
void mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id );

// Creating CollisionListener
class Collision : public OgreOde::CollisionListener {
public:
bool collision(OgreOde::Contact* contact);
};
// Game specific stuff
void changeCar();

static ActionState* getSingletonPtr( void );
private:
ActionState( void ) { }
ActionState( const ActionState& ) { }
ActionState & operator = ( const ActionState& );

Ogre::Root *mRoot;
Ogre::Camera *mCamera;
Ogre::SceneManager *mSceneMgr;
Ogre::Viewport *mViewport;
Ogre::OverlayManager *mOverlayMgr;
Ogre::Overlay *mInfoOverlay;
Ogre::Overlay *mActionOverlay;
Ogre::Overlay *mMouseOverlay;
Ogre::OverlayElement *mMousePointer;
Ogre::OverlayElement *mInfoInstruction;
Ogre::OverlayElement *mInfoNotification;


// OgreOde variables
OgreOde::StepHandler *_stepper;
OgreOde::World *_world;
OgreOde_Prefab::Vehicle *_vehicle;
OgreOde_Loader::DotLoader *dotOgreOdeLoader;
OgreOde::TriangleMeshGeometry *_track;
Ogre::Real _time_step;

OIS::Keyboard *mInputDevice;


static ActionState *mActionState;
};


#ifdef __cplusplus
}
#endif

#endif /* _ACTIONSTATE_H */

Dirso

27-12-2006 18:52:26

It worked!!! Thanks man!!! You're really good!!!

Happy New Year!!!
Dirso