Simple FPS camera collison with objects eg wall, crate etc ?

jonnys

17-07-2008 22:11:40

Can someone tell me (give a code snippet or example project) THE SIMPLEST way how to use OgreOde and Ogre3d to setup up an FPS camera colliding with objects like a crate or a gun (to add ammo) or a wall etc?

btw I am new to OgreOde and any help would be appreciated.... :D

rewb0rn

18-07-2008 00:08:19

well in an fps you would instead need a player model so you can actually simulate the player for walking up stairs etc. there is a tutorial for a walking character in the wiki.

jonnys

18-07-2008 00:34:43

well in an fps you would instead need a player model so you can actually simulate the player for walking up stairs etc. there is a tutorial for a walking character in the wiki.

and then you would use that actual player model (that controls camera view) to do the collisions ?

electronics45

18-07-2008 06:33:57

and then you would use that actual player model (that controls camera view) to do the collisions ?

Thats right, but really you only need the OgreOde bodies (ignore the part in the tutorial about attaching the mesh), and then attach the cammera to the same scene node accommodating the OgreOde body.

Here's how I would do it (Note: I haven't tested this):
First, set up a space without internal collisions, as in the tutorial.
OgreOde::SimpleSpace* dollSpace = new OgreOde::SimpleSpace(mWorld, mSpace);
dollSpace->setInternalCollisions(false);


Create a scene node that will accommodate the camera, and Ode Bodies, then attach the camera.
playerNode = sceneMgr->getRootSceneNode()->createChildSceneNode("player");

// Create a seperate scene node for camera "pitch" to avoid gimbel lock
playerNode->createChildSceneNode("camPitchNode");

// attach camera
camPitchNode->attachObject(cam1);


Now we create player bodies and geoms. Refer to the "walking character" tutorial [1] for more information on what the following code does.


// mass of the player. Experiment with this setting.
Real playerMass = 2.5;

// Dimentions of body to represent player
Real playerWidth = 5;
Real playerHight = 12

// Setup a sphere geomety to represent, and act as the players feet
OgreOde::Body* playerBodyFeet = new OgreOde::Body(mWorld);
playerBodyFeet->setMass(OgreOde::SphereMass(playerMass,playerWidth / 2));
OgreOde::SphereGeometry* feetGeom = new OgreOde::SphereGeometry(playerWidth / 2,mWorld, dollSpace);
feetTrans = new OgreOde::TransformGeometry(mWorld, dollSpace);

feetTrans->setBody(playerBodyFeet);
feetTrans->setEncapsulatedGeometry(feetGeom);

// attach player feet body to our player node
playerNode->attachObject(playerBodyFeet);


// Setup a capsule geometry to represent the rest of the player.
OgreOde::Body* playerBodyTorso = new OgreOde::Body(mWorld);
playerBodyTorso->setMass(OgreOde::CapsuleMass(playerMass,playerWidth/2,Vector3::UNIT_Y,playerWidth/2));
playerBodyTorso->setAffectedByGravity(false);
// playerBodyTorso->setDamping(1,100);
OgreOde::TransformGeometry* torsoTrans = new OgreOde::TransformGeometry(mWorld,dollSpace);
OgreOde::CapsuleGeometry* torsoGeom = new OgreOde::CapsuleGeometry(playerWidth/2,PlayerHight-4*(playerWidth/2),mWorld, dollSpace);

torsoGeom->setPosition(Ogre::Vector3(0,playerHight-((playerHight-4*(playerWidth/2))/2+2*(playerWidth/2)),0));
torsoGeom->setOrientation(Quaternion(Degree(90),Vector3::UNIT_X));
torsoTrans->setBody(playerBodyTorso);
torsoTrans->setEncapsulatedGeometry(torsoGeom);
playerNode->attachObject(playerBodyTorso);

// Join player torso with player feet, with a hinge joint.
OgreOde::HingeJoint* joint = new OgreOde::HingeJoint(mWorld);
joint->attach(playerBodyTorso,playerBodyFeet);
joint->setAxis(Ogre::Vector3::UNIT_X); //set the rotation axis

// set Bodies initial possition to prevent "exploding" :)
playerBodyFeet->setOrientation(Quaternion(Radian(5.0),Ogre::Vector3(0,0,0)));
playerBodyFeet->setPosition(Vector3::ZERO);

playerBodyTorso->setOrientation(Quaternion(Radian(5.0),Ogre::Vector3(0,0,0)));
playerBodyTorso->setPosition(Vector3::ZERO);


And there you have it! :) Don't forget to keep the player upright every frame (explained in tutorial). Moving the player forward/backward are also explained in the tutorial. To yaw the player (look from side to side), or add pitch (look up/down) try calling a function similar to the following with the amount of mouse x, and y movement every frame:


void OrientateCamera(Real xMov, Real yMov)
{
Quaternion q (Degree(-xMov * (camRotateSpeed * 0.002)), Vector3(0,1,0));
playerTorso->setOrientation(playerTorso->getOrientation() * q);
playerFeet->setOrientation(q * playerFeet->getOrientation());

camPitchNode->pitch(Degree(-yMov * (camRotateSpeed * 0.002)));
}


[1] http://www.ogre3d.org/wiki/index.php/Og ... _Character