Cannot get gravity to work (nor collision code) for camera

GiDEoN

26-05-2006 13:32:29

I cannot get OgreNewt to work the way I like it :(, after trying 1,5 week i've decided I ask for help.

The camera system I use: http://www.ogre3d.org/wiki/index.php/Creating_a_simple_first-person_camera_system

My code is below:

Here I setup the camerasystem. It uses 4 nodes as the tutorial says. It works just fine except I can fly, I want the gravity of 'Newton' to solve this:) as if I solve it with gravity, I can walk stairs (with an invisible plane over it). And thats all I need in my project(besides not going through walls). void GameHandler::setCamera (Ogre::Camera *cam)
{
mCamera = cam;
mCamNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

// Create the camera's yaw node as a child of camera's top node.
mCamYawNode = mCamNode->createChildSceneNode();

// Create the camera's pitch node as a child of camera's yaw node.
mCamPitchNode = mCamYawNode->createChildSceneNode();

//Create camera roll node
mCamRollNode = mCamPitchNode->createChildSceneNode();
mCamRollNode->attachObject ( mCamera );
//mCamRollNode->setFixedYawAxis( true );

//Set camera position
mCamNode->setPosition ( Vector3(0, 2000, -2500 ));

/*
Newton
*/
Ogre::Vector3 size( 1.0 , 1.0, 1.0);

//Make collision (use the Ho ;))
colHo = new OgreNewt::CollisionPrimitives::Ellipsoid( mWorld, size );

//Make body
camBody = new OgreNewt::Body( mWorld, colHo );
camBody->setAutoFreeze(0);

//Gravity
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcEllipsoidSolid( 70.0, size );
camBody->setMassMatrix( 70.0, inertia );

//Damping
camBody->setLinearDamping ( 0.01f );
camBody->setAngularDamping ( Vector3(0.01f, 0.01f, 0.01f ));

//Attach
camBody->setPositionOrientation ( mCamNode->getWorldPosition(), mCamNode->getWorldOrientation() );
camBody->attachToNode( mCamNode );

//Callback
camBody->setStandardForceCallback();

//Upfactor
OgreNewt::BasicJoints::UpVector* mUpVec = new OgreNewt::BasicJoints::UpVector(mWorld, camBody, Ogre::Vector3::UNIT_Y);

//Set Userdata
camBody->setUserData(this);

//clean up
delete colHo;

//Log event
mXmlHandler->writeLog( "Added psychics to camera" );

OgreNewt::Debugger::getSingleton().init( mSceneMgr );
}


My terraincode: (removed, working properly)

My movement code:
bool GameHandler::keyPressed(const OIS::KeyEvent &e)
{
//Camere movement
if( e.key == OIS::KC_UP || e.key == OIS::KC_W )
mDirection.z -= mMove;

if( e.key == OIS::KC_DOWN || e.key == OIS::KC_S )
mDirection.z += mMove;

if( e.key == OIS::KC_LEFT || e.key == OIS::KC_A )
mDirection.x -= mMove;

if( e.key == OIS::KC_RIGHT || e.key == OIS::KC_D )
mDirection.x += mMove;

return true;
}

bool GameHandler::keyReleased(const OIS::KeyEvent &e)
{
//Camere movement
if( e.key == OIS::KC_UP || e.key == OIS::KC_W )
mDirection.z += mMove;

if( e.key == OIS::KC_DOWN || e.key == OIS::KC_S )
mDirection.z -= mMove;

if( e.key == OIS::KC_LEFT || e.key == OIS::KC_A )
mDirection.x += mMove;

if( e.key == OIS::KC_RIGHT || e.key == OIS::KC_D )
mDirection.x -= mMove;

if( e.key == OIS::KC_F1 )
OgreNewt::Debugger::getSingleton().showLines( mWorld );

if( e.key == OIS::KC_F2 )
OgreNewt::Debugger::getSingleton().hideLines();
}


Framestarted event:
bool GameHandler::frameStarted(const Ogre::FrameEvent &evt)
{
// Yaws the camera according to the mouse relative movement.
mCamYawNode->yaw(this->mRotX);

// Pitches the camera according to the mouse relative movement.
mCamPitchNode->pitch(this->mRotY);

// Translates the camera according to the translate vector which is
// controlled by the keyboard arrows.
//
// NOTE: We multiply the translateVector by the cameraPitchNode's
// orientation quaternion and the cameraYawNode's orientation
// quaternion to translate the camera accoding to the camera's
// orientation around the Y-axis and the X-axis.
mCamNode->translate( mCamYawNode->getOrientation() * mCamPitchNode->getOrientation() * mDirection * evt.timeSinceLastFrame, Ogre::SceneNode::TS_LOCAL);

// Angle of rotation around the X-axis.
pitchAngle = (2 * Ogre::Degree(Ogre::Math::ACos( mCamPitchNode->getOrientation().w)).valueDegrees());

// Just to determine the sign of the angle we pick up above, the
// value itself does not interest us.
pitchAngleSign = mCamPitchNode->getOrientation().x;

// Limit the pitch between -90 degress and +90 degrees, Quake3-style.
if(pitchAngle > 90.0f)
{
if(pitchAngleSign > 0)
// Set orientation to 90 degrees on X-axis.
mCamPitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f), Ogre::Math::Sqrt(0.5f), 0, 0));
else if(pitchAngleSign < 0)
// Sets orientation to -90 degrees on X-axis.
mCamPitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f), -Ogre::Math::Sqrt(0.5f), 0, 0));
}

//Reset mMouse rotations
mRotX = 0;
mRotY = 0;

//Update Newton
mWorld->update ( evt.timeSinceLastFrame );

return true;
}


Updated Question:
I've implemented the 'shooting blocks mechanism' from Example 1 and that works just fine. So the 'Newton World' is setup right, and Newton is gettings its updates ( using mWorld->update ( evt.timesincelastframe ) )

When I press F1 to show the debugging lines(i know that its F3 when I use default files like basiclistener. But i'm not using any exampleApplication/fameListener file ) I can see the collision object that is supposed to be attached to the cameranode. But its not moving while my camera is! I asumed because I attached the collisionbody to mCamNode, it would move with it (but it isn't)

What am I doing wrong? I've looked at all examples(too bad their ain't really a FPS camera tutorial with gravity).

Also currently none of the collision methods are working for the camera. The camera can go skyhigh and through the floor. How do I fix this?
I'm sure someone who understand this can give me a few hints. I'm clueless.

//another note
I'm not using ExampleApplication.H or ExampleFramelistener.h from Ogre. My gameloop renders 1 frame at a time using mRoot->renderOneFrame();
I've searched the forums and tried out a lot, i just cannot get it to work :oops:

shoki

26-05-2006 14:35:08

in one word : LOL !
You've post on one of my post to say that I had to search on myself, and that I don't have to ask help.
So I give you one advice, redo the tutorials 10 times more :wink:
Shoki

GiDEoN

26-05-2006 14:52:40

in one word : LOL !
You've post on one of my post to say that I had to search on myself, and that I don't have to ask help.
So I give you one advice, redo the tutorials 10 times more :wink:
Shoki
Yes, but its not the way you're describing it now. I argue with you on other things. You said i'm new and thats cause I can't do it. I did the tutorials a few times. OgreNewt has only 1 tutorial. I've looked at the examples but just can't get it working. And where do I state that you shouldn't ask help.
So instead of posting a message that contains no useful information at all, don't post. At least I gave a hint of what I thought it could be, you're just flaming!

Anyone else has an useful comment?

walaber

26-05-2006 16:16:16

make sure you are setting the size of the OgreNewt::World to the size of your terrain.

GiDEoN

26-05-2006 16:21:40

make sure you are setting the size of the OgreNewt::World to the size of your terrain.Thanks for the tip. Going to investigate how :lol: Will post update when I get more news. I think this is it though, even looked at the OgreNewt:World function but it took 1 optional argument (another world). Well, back to VS2005 :twisted:

Update:
Great :) seems to do atleast something. Camera ain't falling down yet, but the 'shooting' objects do. So i'm onto something. Thanks for the help!

Update2:
After pressing F3 I can see that the 'collision thing' i've attached to the mCamNode (main node which the camera is attached too) isn't moving an inch. Its stuck on the position it starts from. The camera moves just like it should, like nothing is attached. Hope anyone can give me a pointer :roll:

GiDEoN

27-05-2006 16:47:09

i've updated the topicstart :)

Sourcecode for those who are willing to take a look: http://projects.bakx.us/Source%20Virtual%20Home.rar (was planning to release all source anyways when this project finishes in 3 weeks :P )

raicuandi

28-05-2006 17:32:31

I'm sory, I'm not familiar enough yet with OgreNewt/Newton or any kinds of more-than-basic physics, BUT if I'd had to make a FPS-like camera use in Ogre and OgreNewt (as a first oppinion that came into my mind), I'd make a body that represents the see-er (the guy who sees) - don't know, probably just a collision primitive, like a box or something - and

put the camera in the same position as that collision body, + some extra height to look as if you're viewing from the "head"

I don't know how to do it nicely, but in the last option, I'd update the camera position every frame, based on that body's position (NOT orientation or others, just position).

Hope it helps! :)
Andi

GiDEoN

29-05-2006 03:15:44

I'm sory, I'm not familiar enough yet with OgreNewt/Newton or any kinds of more-than-basic physics, BUT if I'd had to make a FPS-like camera use in Ogre and OgreNewt (as a first oppinion that came into my mind), I'd make a body that represents the see-er (the guy who sees) - don't know, probably just a collision primitive, like a box or something - and

put the camera in the same position as that collision body, + some extra height to look as if you're viewing from the "head"

I don't know how to do it nicely, but in the last option, I'd update the camera position every frame, based on that body's position (NOT orientation or others, just position).

Hope it helps! :)
Andi


Been playing around with it, and decided to shelve it for another week. Finishing other things off my project now and will look at this later..

Right now I've implemented a simple hack
//Check camera - terrain collision
Vector3 camNodePos = mCamNode->getPosition();

if ( camNodePos.y < 110 )
mCamNode->setPosition( camNodePos.x, 110, camNodePos.z );
which does the trick. Although I rather would have done it with gravity :roll:
If anyone knows what i'm after and is willing to share the howto buidl it, feel free and thanks :)