Thanks for your answers, cuteAlien.
You know, I don't think the problem with gravity is only mine.
I think this is a problem inside Irrlicht and it may seem strange to many people who will try to make a basic game with Irrlicht only (although there may not be so many since there are many many full-featured game engines on the market).
My development knowledge may be too limited and I should perhaps consider finishing my little basic game on a real game engine which may be more accessible for me
.
However, to thank you for the time you spent trying to help me and better demonstrate the problem I observed, I made a small code that allows you to easily verify that the gravity and the jump depend on the framerate .
I took your code and modified it a bit for the demonstration.
With this code, you will be able to verify that by enlarging the size of the window (which decreases the framerate), the jump of the cube is much lower, and the speed of fall is lower.
The functioning is simple:
every 5 seconds, the cube jumps, and the maximum Y position is written to the console when the cube falls to the ground.
On my computer, when the window is 800X600, the Cube reaches 349. When the window is maximized (1600X900), the Cube reaches 93.
In my little game, the top of the jump and the speed of gravity change depending on the number of items displayed on the screen ...
Here is the code to test that:
Code: Select all
#include <irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
int main()
{
IrrlichtDevice * device = createDevice(video::EDT_OPENGL, dimension2d<u32>(800,600));
if (!device) return 1;
// Enable window resize because enlarging the window drops the framerate
device->setResizable(true);//Resizable window
scene::ISceneManager* smgr = device->getSceneManager();
video::IVideoDriver * videoDriver = device->getVideoDriver ();
irr::scene::ICameraSceneNode * camera = smgr->addCameraSceneNode(0, core::vector3df(0, 100.0f, -200), core::vector3df(0,50.0f,0));
scene::IMeshSceneNode * n1 = smgr->addCubeSceneNode(10.0f, 0, -1, core::vector3df(0, 5, 0));
n1->getMaterial(0).Lighting = false;
smgr->getMeshManipulator()->setVertexColors(n1->getMesh(), SColor(255,255,0,0));
const irr::scene::IGeometryCreator *geomentryCreator = smgr->getGeometryCreator();
irr::scene::IMesh* plane = geomentryCreator->createPlaneMesh(irr::core::dimension2d<irr::f32>(5.0f, 5.0f), irr::core::dimension2d<irr::u32>(5.0f, 5.0f));
scene::IMeshSceneNode * planeSceneNode = smgr->addMeshSceneNode(plane, 0, -1, core::vector3df(0.0f, 0.0f, 0.0f));
planeSceneNode->getMaterial(0).Lighting = false;
smgr->getMeshManipulator()->setVertexColors(planeSceneNode->getMesh(), SColor(255,0,255,0));
scene::ITriangleSelector * world = smgr->createTriangleSelector((planeSceneNode)->getMesh(), planeSceneNode);
scene::ISceneNodeAnimatorCollisionResponse* colResponse = smgr->createCollisionResponseAnimator(
world, n1,
core::vector3df(18, 5.0f, 18), //ellipsoidRadius
core::vector3df(0, -2.0f, 0) //gravityPerSecond
);
n1->addAnimator(colResponse);
colResponse->drop();
// Variables to call "jump()" every 5 seconds
irr::u32 lastJump = device->getTimer()->getRealTime();
irr::u32 delayBetweenJumps = 5000; // Jump every 5 seconds
// Record higher position when jumping
irr::f32 jumpMaxPosition = 0.0f;
bool isJumping = false;
while ( device->run())
{
irr::u32 time = device->getTimer()->getRealTime();
// Display max value when jump is finished
if(isJumping && colResponse->collisionOccurred()){
isJumping = false;
std::cout << "Jump max value: " << jumpMaxPosition << std::endl;
}
// Get max position Y
if(n1->getPosition().Y > jumpMaxPosition){
jumpMaxPosition = n1->getPosition().Y;
}
if(time - lastJump >= delayBetweenJumps){
// Reset higher position
jumpMaxPosition = 0.0f;
// Jump now
colResponse->jump(2.0f);
lastJump = time;
isJumping = true;
}
videoDriver->beginScene(true, true, video::SColor(255, 255, 255, 255));
smgr->drawAll();
videoDriver->endScene();
}
device->closeDevice();
device->drop();
return 0;
}
Perhaps one solution would be to force the framerate to stay at a constant rate. I don't know if I have the skills to do something very reliable at this level. Another solution would be to use a physics engine or to completely change for a full game engine.
I specify that I have a fairly weak computer without a powerful graphics card, and my framerate easily falls to 30.
And, please excuse me for my bad english, I try to do my best with what I know (and a little google translation)
.