[Solved] Camera + Objects + Gravity = Doesn't work well

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Dragfang
Posts: 21
Joined: Tue Mar 02, 2010 4:08 pm

[Solved] Camera + Objects + Gravity = Doesn't work well

Post by Dragfang »

Hi guys, I'm currently working on a game-project kinda like MineCraft if you guys seen that. I create a terrain from a flat heightmap to start with, and then I place cubes on it.

The problem is that I (the camera) can't jump/climb up on the cubes. If I build a nice flat area of cubes and try jumping up on it the gravity just keeps trying to pull me down through the cubes and I (the camera) starts to slide towards the end of the flat area of cubes down on the terrain again. Also I can't jump while being on these cubes. I would like these cubes to interact with the camera's gravity and collision just like the terrain does.

I used to create the camera with the terrain's selector, and I thought that was the reason for it not working. So I checked the demo example and saw that they used metaSelector, so I tried that, but without luck.
Relevant code:
Adding camera and creating metaSelector:

Code: Select all

	ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0, 100.0f, .4f, -1, 0, 0, false, 3.f);
	camera->setPosition(core::vector3df(500,100,500));
	camera->setTarget(core::vector3df(100,30,100));

	ITriangleSelector* cameraSelector = 0;
	ISceneNodeAnimator* cameraAnim = 0;

	// Create meta selector
	IMetaTriangleSelector* metaSelector; 
	metaSelector = smgr->createMetaTriangleSelector();

	if (cameraSelector)
	{
		ISceneNodeAnimatorCollisionResponse* cameraAnim = smgr->createCollisionResponseAnimator(metaSelector, camera, vector3df(25,50,25), vector3df(0,-10,0), vector3df(0,45,0), 0.005f);
		//ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(metaSelector, camera, vector3df(25,50,25), vector3df(0,-10,0), vector3df(0,45,0), 0.005f);
// I used this one before trying metaSelector.
		cameraSelector->drop(); // As soon as we're done with the selector, drop it.
		camera->addAnimator(cameraAnim);
		cameraAnim->drop();  // And likewise, drop the animator when we're done referring to it.
	}
Creating Terrain and adding it's selector to metaSelector.

Code: Select all

	ITerrainSceneNode* terrainNode = smgr->addTerrainSceneNode("media/terrain-heightmap.bmp");
	  
	terrainNode->setScale(vector3df(80, 8.8f, 80));
	terrainNode->setMaterialFlag(EMF_LIGHTING, false);

	terrainNode->setMaterialTexture(0, driver->getTexture("media/terrain-texture3.jpg"));
	
	terrainNode->setMaterialType(EMT_DETAIL_MAP);
	terrainNode->scaleTexture(40.0f, 20.0f);



	// create triangle selector for the terrain	
	ITriangleSelector* terrainSelector = smgr->createTerrainTriangleSelector(terrainNode, 0);
	terrainNode->setTriangleSelector(terrainSelector);

	// create triangle for camera aka collision controll
	if (terrainSelector)
	{
		ISceneNodeAnimator* terrainAnim = smgr->createCollisionResponseAnimator(
			terrainSelector, camera, vector3df(30,50,30),
			vector3df(0,-10,0),
			vector3df(0,10,0));
		camera->addAnimator(terrainAnim);
		terrainAnim->drop();
	}
	metaSelector->addTriangleSelector(terrainSelector);
All my cubes are in an array and I add their selector to the metaselector by:

Code: Select all

metaSelector->addTriangleSelector(objectSelector[objectId]);
I uploaded the game aswell here: http://data.fuskbugg.se/skogsturken/Irr ... eCraft.rar
Incase you don't understand what my problem is / if u wanna try it :)

Thanks in advance!
Last edited by Dragfang on Thu Oct 14, 2010 7:45 pm, edited 1 time in total.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

The cameras movespeed is 0.4, but the gravity is -10. Seems the gravity affects the player a lot more than moving would. You might try lowering the gravity. I tried the demo, and i couldn't jump at all. But then i don't know the controls either.

Also, since you add the terrain selector to the meta-selector, you don't need to create yet another collision response animator. Doing so will leave you with 2 animators, both trying to control the player, which might be bad.
Also, you use different settings on the animators. =/
Dragfang
Posts: 21
Joined: Tue Mar 02, 2010 4:08 pm

Post by Dragfang »

Luben wrote:The cameras movespeed is 0.4, but the gravity is -10. Seems the gravity affects the player a lot more than moving would. You might try lowering the gravity. I tried the demo, and i couldn't jump at all. But then i don't know the controls either.

Also, since you add the terrain selector to the meta-selector, you don't need to create yet another collision response animator. Doing so will leave you with 2 animators, both trying to control the player, which might be bad.
Also, you use different settings on the animators. =/
Oh I forgot to write down the controls. I use the normal FPS-camera controls I.E. arrows for moving and 'J' for jumping. To place out cubes you press B first and then 1 or 2 depending on if u want a white or grey cube.

Ill try lowering gravity some. Also I did not know that you should only have one animator. Should I remove the one next to where i create the terrain then?
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

I imagine having 2 different collision animators may cause them to fight with each other, if one wants to collide and the other doesnt, or if both wants to collide with different things. So yes i guess :)
(No guarantee that i'm not wrong though ;] )
Dragfang
Posts: 21
Joined: Tue Mar 02, 2010 4:08 pm

Post by Dragfang »

Luben wrote:I imagine having 2 different collision animators may cause them to fight with each other, if one wants to collide and the other doesnt, or if both wants to collide with different things. So yes i guess :)
(No guarantee that i'm not wrong though ;] )
After looking into my code I found that I had a collision animator for every cube I made :P I tried to remove them and everything worked great so thanks a lot! :)

I learned in the examples that to a 3d object you have 4 things, mesh, node, selector and animator. I guess you don't need the animator for every object then?
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Only for the dynamic objecs you want to collide with(mostly static) things :)
Dragfang
Posts: 21
Joined: Tue Mar 02, 2010 4:08 pm

Post by Dragfang »

Luben wrote:Only for the dynamic objecs you want to collide with(mostly static) things :)
Ahh I see, well everything works perfect now so thanks a lot for you help :)
Post Reply