Scene object resetting its position

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.

Scene object resetting its position

Postby Kearney401 » Wed Apr 04, 2012 4:02 pm

I'm having troubles with a scene object resetting its position every time in my while loop. I want it to keep its position and continue moving on in the direction of my mouse. Can anybody see what's going wrong with it? Could it possibly be the ray for my mouse?

Below is the player(cube) code, all I am doing there is setting up my position and other stuff.
cpp Code: Select all
 
        irr::scene::IAnimatedMesh* cube = game.getDevice()->getSceneManager()->getMesh("Cube.x");
        node = game.getDevice()->getSceneManager()->addAnimatedMeshSceneNode(cube);
        node->setMaterialFlag(irr::video::EMF_LIGHTING, false);
        node->setPosition(vector3df(0.0f, -10.0f, 0.0f));
        node->setScale(vector3df(1.0f, 1.0f, 1.0f));   
 


This next part is where I think the problems are coming from

cpp Code: Select all
 
int main()
{
 
        Player player(0);
        game.setCaption(L"Project Frog BaseBall");
        game.setDimensions(irr::core::dimension2d<irr::u32>(800, 600));
        FreeCamera* camera = new FreeCamera();
        camera->setPosition(irr::core::vector3df(0.0f, 0.0f, -30.f));
        camera->setTarget(irr::core::vector3df(0.0f, 0.0f, 1.0f));     
        camera->setYaw(0.0f);
        camera->setPitch(0.0f);
        camera->setTarget(irr::core::vector3df(0.0f, 0.0f, 0.0f));;
 
        game.setCamera(camera);
        game.initialise();
        game.loadContent();
       
        irr::u32 prevTime = game.getDevice()->getTimer()->getRealTime();
        irr::u32 currTime;
        irr::f32 deltaTime;
        const f32 MOVEMENT_SPEED = 500.f;
        core::vector3df nodePos = player.getNode()->getPosition(); //Getting the position of the player cube
       
        while (game.getDevice()->run())
        {
                currTime = game.getDevice()->getTimer()->getRealTime();
                deltaTime = (float)(currTime - prevTime) / 1000.0f;
 
                if (handler.isKeyDown(KEY_ESCAPE))
                        break;
                if (handler.isKeyDown(KEY_LEFT) || handler.isKeyDown(KEY_KEY_A))
                        camera->move(vector3df(-0.2f, 0.0f, 0.0f));
                if (handler.isKeyDown(KEY_RIGHT) || handler.isKeyDown(KEY_KEY_D))
                        camera->move(vector3df(0.2f, 0.0f, 0.0f));
                if (handler.isKeyDown(KEY_UP) || handler.isKeyDown(KEY_KEY_W))
                        camera->move(vector3df(0.0f, 0.2f, 0.0f));
                if (handler.isKeyDown(KEY_DOWN) || handler.isKeyDown(KEY_KEY_S))
                        camera->move(vector3df(0.0f, -0.2f, 0.0f));
 
                int deltaX = handler.getCurrentMouse().Position.X - handler.getPrevMouse().Position.X;
                int deltaY = handler.getCurrentMouse().Position.Y - handler.getPrevMouse().Position.Y;
 
                irr::core::vector3df mousePosition = handler.getCurrentMouse().Position - handler.getPrevMouse().Position;
 
               
                core::plane3df plane(nodePos, irr::core::vector3df(0,0,-1)); //Setting the plane up for if statement
 
                core::line3df ray;
                ray.start.X = deltaX;
                ray.start.Y = deltaY;
                ray.start.Z = 0;
                ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 1000.f;          
 
                //cout<<"X: " <<ray.start.X<<endl;
                //cout<<"Y: " <<ray.start.Y<<endl;
               
               
                if(plane.getIntersectionWithLine(ray.start, ray.getVector(), mousePosition))
                {
                        core::vector3df toMousePosition(mousePosition - nodePos);
                        const f32 availableMovement = MOVEMENT_SPEED * deltaTime;
                       
                        if(toMousePosition.getLength() <= availableMovement)
                                nodePos = mousePosition;
                        else
                                nodePos += toMousePosition.normalize() * availableMovement;
                }
               
                player.getNode()->setPosition(nodePos);
 
                game.update(deltaTime);
                game.render();
                prevTime = currTime;   
        }
        game.unloadcontent();
        game.shutdown();
}
 


Any input would be much appreciated! :D
Kearney401
 
Posts: 1
Joined: Wed Apr 04, 2012 3:50 pm

Return to Beginners Help

Who is online

Users browsing this forum: No registered users and 1 guest