Setting up a 1st person camera with collision detection

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
soulstorm
Gnoblar
Posts: 24
Joined: Sat Jun 08, 2013 7:00 pm

Setting up a 1st person camera with collision detection

Post by soulstorm » Wed Oct 09, 2013 10:57 pm

Hello,

I am using Ogre 1.9 with bullet physics, 2.8.

I am trying to set up a first person camera that is at the same time collision aware (so that it cannot pass through walls, etc).

The problem is that I don't know what is a simple way of doing it.

After setting a the ground, I am using this for setting a camera rigid body, and attaching it to the camera node:

Code: Select all

btCollisionShape *cameraBox = new btBoxShape(btVector3(13.0f, 12.0f, 13.0f));
this->physicsEngine->getCollisionShapes().push_back(cameraBox);

btTransform startTransform;
startTransform.setIdentity();

btScalar mass = 1.0;
btVector3 localInertia(0,0,0);

cameraBox->calculateLocalInertia(mass, localInertia);
startTransform.setOrigin(btVector3(0.0f, 19, 15.0f));

btDefaultMotionState *myMotionState = new btDefaultMotionState(startTransform);

btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, cameraBox, localInertia);
btRigidBody *body = new btRigidBody(rbInfo);

body->setRestitution(1);
body->setFriction(100.0f);
		
body->setUserPointer((void *)this->mCameraNode);
physicsEngine->getDynamicsWorld()->addRigidBody(body);

physicsEngine->trackRigidBodyWithName(body, std::string("cameraBody"));

Code: Select all

	
bool Level2::mouseMoved( const OIS::MouseEvent &arg ){
}
As I see it, I will need to apply the physics, motion translations to the bullet physics object, and then, since in my code I just show the ogre nodes according to the physics objects each frame, the camera will move. I don't know, however if this is the correct approach.

Does anyone know how to approach this? I just need some general guidelines, about how to figure out the orientation of the camera node, etc.
0 x

amartin
Halfling
Posts: 87
Joined: Wed Aug 14, 2013 6:55 am
Location: Norway

Re: Setting up a 1st person camera with collision detection

Post by amartin » Thu Oct 10, 2013 1:10 am

Generally speaking for anything attached to a camera you want to simulate physics not have the physics engine do the physics. This allows you to fine tune your movement and colision to what feels right.

When I had a look into using bullet for this the general way you make this work is to put a kinematic object around the camera. This will allow other physics objects to bounce off the camera but will not interact with static object which are normally terrain and buildings. To handle those you need to either manually check for the collisions or to cast rays and stop when you get too close to things. This may seem like lots more work that just letting the physics engine handle the colisions for you but it is required if you want your movement to be smooth. You move the camera and everything else reacts to the motion.

If all you are looking at bullet for is collision detection I would suggest you take a look at raycasting to polygon with Minimal Ogre Collision as it is pretty quick to set up a ray in the direction you want to travel and not move if the ray hits something closer than a set distance. For a simple camera not moving through walls this is the best bet. If you actually want the physics for more than collision detection then just do the same thing with the bullet rays. Once you have that going you can then tweak till you are happy either more rays or using some form of intersection check.
0 x

soulstorm
Gnoblar
Posts: 24
Joined: Sat Jun 08, 2013 7:00 pm

Re: Setting up a 1st person camera with collision detection

Post by soulstorm » Thu Oct 10, 2013 2:37 pm

Thanks for the information. I have a few questions, however:
amartin wrote:Generally speaking for anything attached to a camera you want to simulate physics not have the physics engine do the physics. This allows you to fine tune your movement and colision to what feels right.

When I had a look into using bullet for this the general way you make this work is to put a kinematic object around the camera. This will allow other physics objects to bounce off the camera but will not interact with static object which are normally terrain and buildings. To handle those you need to either manually check for the collisions or to cast rays and stop when you get too close to things. This may seem like lots more work that just letting the physics engine handle the colisions for you but it is required if you want your movement to be smooth. You move the camera and everything else reacts to the motion.
In other words, you tell me to work in creating a camera with custom physics, and let the physics engine handle the rest of the world, am I right?
amartin wrote:If all you are looking at bullet for is collision detection I would suggest you take a look at raycasting to polygon with Minimal Ogre Collision as it is pretty quick to set up a ray in the direction you want to travel and not move if the ray hits something closer than a set distance. For a simple camera not moving through walls this is the best bet. If you actually want the physics for more than collision detection then just do the same thing with the bullet rays. Once you have that going you can then tweak till you are happy either more rays or using some form of intersection check.
Actually, I intend to use the physics engine for many other things, so I don't want to get rid of it. I get your point, that using custom camera physics will ensure smooth camera movement. However, what happens if I want to implement realistic jumping, or actually pushing simple objects like blocks? Won't that need the physics engine? Is it preferable to use the custom first person camera?
0 x

amartin
Halfling
Posts: 87
Joined: Wed Aug 14, 2013 6:55 am
Location: Norway

Re: Setting up a 1st person camera with collision detection

Post by amartin » Fri Oct 11, 2013 1:00 am

soulstorm wrote:Actually, I intend to use the physics engine for many other things, so I don't want to get rid of it. I get your point, that using custom camera physics will ensure smooth camera movement. However, what happens if I want to implement realistic jumping, or actually pushing simple objects like blocks? Won't that need the physics engine? Is it preferable to use the custom first person camera?
Ok first off I'm not actually telling you to create custom physics for the camera. What you really want to do is pretend that the camera has physics so that movement looks the way you want movement to look instead of attempting to apply all the correct forces to an object in the physics engine to get it to do what you are after. Since bullet expects you to do this there is a Kinematic character controller class but since bullet was overkill for what I needed I have not used it. Keep the physics engine since you actually need the power but for some objects you want to tell the physics engine where the object goes rather than having it tell you where the object is. I assume that you are doing some form of player controller there is lots of information on the internet I'd suggest reading through some of the approaches and picking one that seems to suite you.

The basics for what you want are
Pushing objects: Put a kinematic object around the camera and move that as you move the camera around the scene. This will push the rigid bodies around as you move. You can still force it inside objects if you want to though and it won't interact with static objects.

Not falling through the floor/ walking through walls: Walls are probably static objects they do not interact with kinematic objects so you need to decide how you handle this yourself. Bullet has a ray you can use as well as a queurie that checks to see if the object collides with anything. Make the check then don't move if the new position would be invalid. Rays are simplest at first but get harder as the scene and motion gets more complicated.

Jumping/falling: This is really up to personal preference and mathimatical ability. You can do anything from manually setting a vertical motion vector when the jump starts and modifying that each frame then using it to determine the jump position to creating an invisible rigid body in the physics engine for the jump and applying the position of that each frame your camera. I would go with some for of jump vector it's not accurate with the physics but much nicer on the player. Jumping next to a small wall the physics will probably kill the forward motion so you won't be able to get to the top without a run up. The manual vector you can keep the horizontal and not apply it if there is something in the way so straight onto the top of the wall every time.

Thats the basics I ended up looking into all of this then removing bullet since I didn't need it at the time.
0 x

soulstorm
Gnoblar
Posts: 24
Joined: Sat Jun 08, 2013 7:00 pm

Re: Setting up a 1st person camera with collision detection

Post by soulstorm » Sat Oct 12, 2013 5:16 pm

amartin wrote:
soulstorm wrote:Actually, I intend to use the physics engine for many other things, so I don't want to get rid of it. I get your point, that using custom camera physics will ensure smooth camera movement. However, what happens if I want to implement realistic jumping, or actually pushing simple objects like blocks? Won't that need the physics engine? Is it preferable to use the custom first person camera?
Ok first off I'm not actually telling you to create custom physics for the camera. What you really want to do is pretend that the camera has physics so that movement looks the way you want movement to look instead of attempting to apply all the correct forces to an object in the physics engine to get it to do what you are after. Since bullet expects you to do this there is a Kinematic character controller class but since bullet was overkill for what I needed I have not used it. Keep the physics engine since you actually need the power but for some objects you want to tell the physics engine where the object goes rather than having it tell you where the object is. I assume that you are doing some form of player controller there is lots of information on the internet I'd suggest reading through some of the approaches and picking one that seems to suite you.

The basics for what you want are
Pushing objects: Put a kinematic object around the camera and move that as you move the camera around the scene. This will push the rigid bodies around as you move. You can still force it inside objects if you want to though and it won't interact with static objects.

Not falling through the floor/ walking through walls: Walls are probably static objects they do not interact with kinematic objects so you need to decide how you handle this yourself. Bullet has a ray you can use as well as a queurie that checks to see if the object collides with anything. Make the check then don't move if the new position would be invalid. Rays are simplest at first but get harder as the scene and motion gets more complicated.

Jumping/falling: This is really up to personal preference and mathimatical ability. You can do anything from manually setting a vertical motion vector when the jump starts and modifying that each frame then using it to determine the jump position to creating an invisible rigid body in the physics engine for the jump and applying the position of that each frame your camera. I would go with some for of jump vector it's not accurate with the physics but much nicer on the player. Jumping next to a small wall the physics will probably kill the forward motion so you won't be able to get to the top without a run up. The manual vector you can keep the horizontal and not apply it if there is something in the way so straight onto the top of the wall every time.

Thats the basics I ended up looking into all of this then removing bullet since I didn't need it at the time.
Thanks a lot for all this valuable information. I will check these information and will make tests and proof of concepts to see what suits my case. Based on what you say, I too believe that putting a kinematic object around the camera and telling it where the camera goes so that it can move with the latter is preferable than letting the camera follow the kinematic object. I will try to do something with this approach, then.

Thanks a lot.
0 x

bradhawk
Gnoblar
Posts: 5
Joined: Sat Aug 31, 2013 4:40 pm

Re: Setting up a 1st person camera with collision detection

Post by bradhawk » Wed Oct 16, 2013 5:43 pm

Hi! I recently successfully to create FPS Camera using Bullet, and btOgre wrapper.

Here some vids : http://www.youtube.com/watch?v=j4E_pK_Gu38

I would like to share the source code, but It's still messy. If you don't mind, just ask me :)

PS: In the video, the pitch of camera still not working, but the source code I have it has full functional FPS Camera, Thanks.
0 x

soulstorm
Gnoblar
Posts: 24
Joined: Sat Jun 08, 2013 7:00 pm

Re: Setting up a 1st person camera with collision detection

Post by soulstorm » Sat Oct 19, 2013 12:34 pm

Thanks for sharing! Good job! What version of Ogre and Bullet did you use?
0 x

bradhawk
Gnoblar
Posts: 5
Joined: Sat Aug 31, 2013 4:40 pm

Re: Setting up a 1st person camera with collision detection

Post by bradhawk » Sun Oct 20, 2013 5:39 pm

Hi,
I'm using OGRE 1.8, Bullet 2.81, and using btOgre wrapper :)

Thanks
0 x

bingofuel
Gnoblar
Posts: 1
Joined: Wed Apr 29, 2015 1:20 pm

Re: Setting up a 1st person camera with collision detection

Post by bingofuel » Wed Apr 29, 2015 1:23 pm

bradhawk Can you describe the basic idea of how to make it work?
0 x

retron
Gnoblar
Posts: 12
Joined: Mon Apr 13, 2015 4:46 pm

Re: Setting up a 1st person camera with collision detection

Post by retron » Fri Jul 03, 2015 2:58 am

Can you share a sample of the integration?
0 x

Post Reply