First Person Camera Collision

mutambo

13-04-2007 15:15:18

Hello everybody,

I am absolute beginner in Mogre but I'm trying to make program for school project. It is about modeling city to 3D, and I don't how to make collision of camera with buildings. I tried to write C# code from C++ code http://www.ogre3d.org/phpBB2addons/viewtopic.php?t=2110 but it is not working.

Could anyone help please??

Tanks

DaCracker

13-04-2007 15:43:44

You should really use a physics lib for that purpose. I
believe that there is a port of OgreNewt to the .NET framework.
Search the forums and see what you can find, good luck! :)

mutambo

13-04-2007 16:03:21

I have already done that.

I tried to use MogreNewt Library. As I said I have converted every line to what the C# equivelent would be from previous link. I checked almost every forum, but I can fing only C++ examples, no C#.

Thanks anyway :D

bleubleu

13-04-2007 16:12:45

A physics lib might be an overkill, but for a quick school project, it's probably the way to go.

Look at the first MogreNewt example called "Basis". It will show you how to create a treecollision from an ogre entity (a terrain in this case). This tree collision is then used to create a body. You can now test collision with the body. You could then attach a sphere body around the camera to avoid the camera going through a wall.

I doubt you will find a complete solution on the forums. It is a school project, you will have to do some work at some point...

Mast

mutambo

13-04-2007 16:18:48

Ok, it looks like I'm just asking for help and it looks that I haven't tried anything. I found samples. And I think little bit know how it works. Here you can see my code.

Variables:

private float _Gravity = -9.8f;
private World _World;
private Vector3 _CamSize = new Vector3(1, 1, 1);
private SceneNode _CamNode;
private SceneNode _CamViewNode;
private Body _CamBody;
private CollisionPrimitives.Ellipsoid _CamColl;

and rest of important code:

void camera_force_callback(Body body)
{
float mass;
Vector3 inertia;

body.getMassMatrix(out mass, out inertia);
Vector3 force = new Vector3(0, this._Gravity, 0);
force *= mass;

body.addForce(force);
body.setOmega(new Vector3(0, this._CameraRotationX, 0));
}

public override void CreateScene()
{
// Newton initialization
this._World = new World();
Vector3 cam_size = new Vector3(10, 10, 10);
float cam_mass = 40;
MogreNewt.Debugger.Instance.init(sceneMgr);


// sky box.
sceneMgr.SetSkyBox(true, "Examples/CloudyNoonSkyBox");

// shadows on!
sceneMgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;

// floor object!
Entity city;
SceneNode citynode;
city = sceneMgr.CreateEntity("City", "mesto.mesh");
citynode = sceneMgr.RootSceneNode.CreateChildSceneNode("CityNode");
citynode.AttachObject(city);

city.CastShadows = false;

// using the new "SceneParser" TreeCollision primitive. this will automatically parse an entire tree of
// SceneNodes (parsing all children), and add collision for all meshes in the tree.

// Collision of city model
MogreNewt.CollisionPrimitives.TreeCollisionSceneParser stat_col = new MogreNewt.CollisionPrimitives.TreeCollisionSceneParser(this._World);
stat_col.parseScene(citynode, true);
MogreNewt.Body bod = new MogreNewt.Body(this._World, stat_col);
stat_col.Dispose();
bod.attachToNode(citynode);
bod.setPositionOrientation(new Vector3(0.0f, 0.0f, 0.0f), Quaternion.IDENTITY);


//Collision of camera
this._CamNode = sceneMgr.RootSceneNode.CreateChildSceneNode("CamNode");
this._CamNode.SetScale(this._CamSize);
this._CamColl = new CollisionPrimitives.Ellipsoid(this._World, this._CamSize);
this._CamBody = new Body(this._World, this._CamColl);
this._CamColl.Dispose();
this._CamBody.attachToNode(this._CamNode);

Vector3 cam_inertia = MomentOfInertia.CalcEllipsoidSolid(cam_mass, cam_size);

this._CamBody.setMassMatrix(cam_mass, cam_inertia);
this._CamBody.ForceCallback += new ForceCallbackHandler(camera_force_callback);

// create an upvector. This forces the body to rotate just in the X and Z axes.
BasicJoints.UpVector uv2 = new BasicJoints.UpVector(this._World, this._CamBody, Vector3.UNIT_Y);

this._CamViewNode = this._CamNode.CreateChildSceneNode("CamViewNode", new Vector3(0, 0, 0));
this._CamViewNode.AttachObject(camera);



// position camera
camera.SetPosition(-3, -3, -50);
camera.LookAt(0, 0, 0);

//make a light
Light light;

light = sceneMgr.CreateLight("Light1");
light.Type = Light.LightTypes.LT_POINT;
light.SetPosition(0.0f, 100.0f, 100.0f);
}

I know I am not a programmer, but I thought it could work :D