Please help me with vehicle.

hesong

10-08-2011 10:18:51

phyxcar.h
#include "ExampleApplication.h"

#include "NxOgre.h"
#include "NxOgreOGRE3D.h"
#include "NxOgreMachine.h"
#include "NxOgreWheel.h"

class Car : public NxOgre::Machine
{
public:

Car(const NxOgre::Vec3& position, NxOgre::Scene* scene, OGRE3DRenderSystem* renderSystem) : Machine(), mScene(scene), mRenderSystem(renderSystem)
{

NxOgre::RigidBodyDescription description;

description.mMass = 1000; // Make it heavy.
description.mWakeUpCounter = 1E8; // Never go to sleep.

NxOgre::Shapes shapes;
mChassisShape = new NxOgre::Box(1, 1, 1);
shapes.insert(mChassisShape);

addWheel(NxOgre::Vec3(-0.5,0,1), true, true, true);
addWheel(NxOgre::Vec3(0.5,0,1), true, true, true);
addWheel(NxOgre::Vec3(-0.5,0,-1), false, false, true);
addWheel(NxOgre::Vec3(0.5,0,-1), false, false, true);

for (unsigned int i=0;i < mWheels.size();i++)
shapes.insert(mWheels[i].mWheel);

mActor = mScene->createActor(shapes, position, description);

mPointRenderable = mRenderSystem->createPointRenderable("chassis.mesh");

mScene->registerMachine(this);
}

~Car()
{
mScene->unregisterMachine(this);
}

void addWheel(const NxOgre::Vec3& position, bool driving, bool steering, bool braking)
{
CarWheel w;

// Create the physics wheel, which our rules and point renderable will represent and work with.
NxOgre::WheelBlueprint* blueprint = new NxOgre::WheelBlueprint();
blueprint->mRadius = 0.433f * 0.5f;
blueprint->mLocalPose.set(position);
w.mWheel = new NxOgre::Wheel(blueprint);

// Create our rules about this wheel.
w.mDriving = driving;
w.mSteering = steering;
w.mBraking = braking;

mWheels.insert(w);

// Then create the machine part of that wheel, so rules can be applied to it, and it can be rendered.
createWheelMachinePart(w.mWheel, mRenderSystem->createPointRenderable("wheel.mesh"));
}

void drive(float torque)
{
for (unsigned int i=0;i < mWheels.size();i++)
if (mWheels[i].mDriving)
mWheels[i].mWheel->setMotorTorque(torque);
}

void brake(float torque)
{
for (unsigned int i=0;i < mWheels.size();i++)
if (mWheels[i].mBraking)
mWheels[i].mWheel->setBrakeTorque(torque);
}

void steer(float angle)
{
for (unsigned int i=0;i < mWheels.size();i++)
if (mWheels[i].mSteering)
mWheels[i].mWheel->setSteeringAngle(angle);
}
protected:

struct CarWheel
{
NxOgre::Wheel* mWheel;
bool mDriving;
bool mSteering;
bool mBraking;
};

NxOgre::Array<CarWheel> mWheels;

NxOgre::Scene* mScene;
OGRE3DRenderSystem* mRenderSystem;
NxOgre::Box* mChassisShape;
};


physx.cpp
#include "ExampleApplication.h"

#include "NxOgre.h"
#include "NxOgreOGRE3D.h"
#include "physxcar.h"
class BloodyMessTutorial2Listener : public ExampleFrameListener
{
public:
BloodyMessTutorial2Listener(RenderWindow *win, Camera *cam)
: ExampleFrameListener(win, cam)
{
mTimeController = NxOgre::TimeController::getSingleton();
}

bool frameStarted(const FrameEvent& evt)
{
mTimeController->advance(evt.timeSinceLastFrame);
return ExampleFrameListener::frameStarted(evt);
}

protected:
NxOgre::TimeController* mTimeController;
};

class BloodyMessTutorial2 : public ExampleApplication
{
protected:
NxOgre::World* mWorld2;
NxOgre::Scene* mScene2;
OGRE3DRenderSystem* mRenderSystem2;

OGRE3DBody* mCube2;
OGRE3DBody* mCubeTwo2;

void createScene()
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5f, 0.5f, 0.5f));

// Create a light
Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20, 80, 50);

// Position the camera
mCamera->setPosition(0, 20, 80);
mCamera->lookAt(0, 20, 0);

// Create the world
mWorld2 = NxOgre::World::createWorld();

// Create scene description
NxOgre::SceneDescription sceneDesc;
sceneDesc.mGravity = NxOgre::Vec3(0, -9.8f, 0);
sceneDesc.mName = "DemoScene";

// Create scene
mScene2 = mWorld2->createScene(sceneDesc);

// Set some physical scene values
mScene2->getMaterial(0)->setStaticFriction(0.5);
mScene2->getMaterial(0)->setDynamicFriction(0.5);
mScene2->getMaterial(0)->setRestitution(0.1);

// Create render system
mRenderSystem2 = new OGRE3DRenderSystem(mScene2);

// Car
Car(NxOgre::Vec3(50,50,50),mScene2,mRenderSystem2);

// Create floor plane (BloodyMess)
mScene2->createSceneGeometry(new NxOgre::PlaneGeometry(0, NxOgre::Vec3(0, 1, 0)), Matrix44_Identity);

// Create floor plane (Ogre)
MovablePlane *plane = new MovablePlane("Plane");
plane->d = 0;
plane->normal = Vector3::UNIT_Y;
Ogre::MeshManager::getSingleton().createPlane("PlaneMesh",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*plane, 120, 120, 1, 1, true, 1, 3, 3, Vector3::UNIT_Z);
Entity *planeEnt = mSceneMgr->createEntity("PlaneEntity", "PlaneMesh");
planeEnt->setMaterialName("Examples/GrassFloor");

Ogre::SceneNode* mPlaneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mPlaneNode->attachObject(planeEnt);
}

// Create a new frame listener
void createFrameListener()
{
mFrameListener = new BloodyMessTutorial2Listener(mWindow, mCamera);
mRoot->addFrameListener(mFrameListener);
}
};

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
// Create application object
BloodyMessTutorial2 app;

try {
app.go();
} catch(Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(),
"An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occurred: " << e.getFullDescription();
#endif
}

return 0;
}

#ifdef __cplusplus
}
#endif



Sorry,for my English. The car is constantly appears in the coordinates 0,0,0. What is the problem?

betajaen

10-08-2011 12:09:19

Constantly as in?

- A new car appears at 0,0,0 each frame?
- Or a single car appears at 0,0,0 at startup?

hesong

11-08-2011 05:45:36

Constantly as in?

- A new car appears at 0,0,0 each frame?
- Or a single car appears at 0,0,0 at startup?

hello betajaen
thank you for your reply.the car appers at 0,0,0 each frame.the wheel also appears at 0,0,0 .and i coule not change the car's coordinate with this cood mActor->setGlobalPosition( pos );
please forgive my poor english.