Movement and gravity Problem

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
m3rk
Posts: 7
Joined: Tue Dec 09, 2008 1:50 am

Movement and gravity Problem

Post by m3rk »

I'm having a big problem with the movement and the gravity. Because if I hold the W or the forward key, it ignores the gravity. Is there checkgravity() function or something like that?

Code: Select all

vector3df rotation = camera->getRotation();
                vector3df newcamera = camera->getPosition();                     
                if(receiver.IsKeyDown(KEY_KEY_W))
                { 
                       vector3df move (0,0,2);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera); 
                }
                else if(receiver.IsKeyDown(KEY_KEY_S))
                {
                       vector3df move (0,0,-2);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera); 
                }
               else if(receiver.IsKeyDown(KEY_KEY_A))
                {
                       vector3df move (-2,0,0);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera); 
                }
                else if(receiver.IsKeyDown(KEY_KEY_D))
                {
                       vector3df move (2,0,0);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera); 
                }
Is there any way to get the code in up,down,left,right movement used in arrow key? Because I noticed its better and more efficient.
nathanf534
Posts: 199
Joined: Tue Dec 09, 2008 2:55 am

Post by nathanf534 »

Your problem with gravity, is because you havn't enabled gravity. In order to have gravity, you need to:
1. Implement it yourself using some formula for gravity, or
2. Enable collision detection with smgr->createCollisionResponseAnimator(
triangle selector, node, 1,2,3,4);
where node is what you want collision detection for, 1 is radius of an ellipse used to check collision, 2 is gravity per second, 3 is ellipse translation, and 4 is ellipse sliding value
while(signatureEmpty){cout<<wittyComment();}
m3rk
Posts: 7
Joined: Tue Dec 09, 2008 1:50 am

Post by m3rk »

No! I've enabled already the gravity to camera.

Code: Select all

// create collision response animator and attach it to the camera
        ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
                selector, camera[1], vector3df(60,100,60),
                vector3df(0,-3,0),
                vector3df(0,0,0));
I don't have any problems with gravity when I use the up,down,right,left button. But when I use my own event which is W for Forward, S for Backward and etc, it moves first then after pressing it, it check the gravity so I it go down.
nathanf534
Posts: 199
Joined: Tue Dec 09, 2008 2:55 am

Post by nathanf534 »

Your problem might be that you are moving faster than the gravity can pull you down, so you might want to decrease how fast you move, or increase gravity. Or if you had a flat world, make it so you only move in the x, or z direction.

EDIT: From what code you have supplied, it looks like you are moving at 2 units per frame , and the gravity is 3 units per second, so since there is about a minimum of 60 frames per second, you are moving at, at least 120 units per second, but gravity is only pulling you down at 3 units per second, so it won't seem like gravity is working
while(signatureEmpty){cout<<wittyComment();}
fennec
Posts: 55
Joined: Fri Oct 10, 2008 7:23 am

Post by fennec »

I think I ran into this problem once before.

The act of moving the object actually disables the effect of the collision animator's gravity so long as you are moving it. So the strength of gravity is not a problem.

I wasn't sure how to solve it so I just made my own gravity and my own simple collision to keep my character above the terrain.
zeroZshadow
Posts: 43
Joined: Mon Dec 01, 2008 6:35 pm

Post by zeroZshadow »

in what order does your main loop work ??

draw
handle stuff

or

handle stuff
draw ??

i had this problem with the first type
fixed it by switching to the second type
m3rk
Posts: 7
Joined: Tue Dec 09, 2008 1:50 am

Post by m3rk »

@nathanf534
Tried setting the gravity to 300 but still the same.

@fennec
I'm planning to make one also but I think it will take time. Maybe there's a solution to this.

@zeroZshadow

Code: Select all

      driver->beginScene(true, true, 0 );
                smgr->drawAll();
                env->drawAll();
                vector3df rotation = camera->getRotation();
                vector3df newcamera = camera->getPosition();                     
                if(receiver.IsKeyDown(KEY_KEY_W))
                {
                       vector3df move (0,0,2);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
                else if(receiver.IsKeyDown(KEY_KEY_S))
                {
                       vector3df move (0,0,-2);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
               else if(receiver.IsKeyDown(KEY_KEY_A))
                {
                       vector3df move (-2,0,0);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
                else if(receiver.IsKeyDown(KEY_KEY_D))
                {
                       vector3df move (2,0,0);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
                driver->endScene();
Tried doing this also but still the same.

Code: Select all

                vector3df rotation = camera->getRotation();
                vector3df newcamera = camera->getPosition();                     
                if(receiver.IsKeyDown(KEY_KEY_W))
                {
                       vector3df move (0,0,2);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
                else if(receiver.IsKeyDown(KEY_KEY_S))
                {
                       vector3df move (0,0,-2);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
               else if(receiver.IsKeyDown(KEY_KEY_A))
                {
                       vector3df move (-2,0,0);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
                else if(receiver.IsKeyDown(KEY_KEY_D))
                {
                       vector3df move (2,0,0);
                       matrix4 mat;
                       mat.setRotationDegrees(rotation);
                       mat.rotateVect(move);
                       newcamera += move;
                       camera->setPosition(newcamera);
                }
                driver->beginScene(true, true, 0 );
                smgr->drawAll();
                env->drawAll();
                driver->endScene();
zeroZshadow
Posts: 43
Joined: Mon Dec 01, 2008 6:35 pm

Post by zeroZshadow »

the first one is wrong anyways, so i'd keep the second one

(you don't do code in begin/endscene)
m3rk
Posts: 7
Joined: Tue Dec 09, 2008 1:50 am

Post by m3rk »

Ok thanks for the tip, zeroZshadow. :)
Post Reply