Anyone know a better way of movement?

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
danielmccarthy
Posts: 51
Joined: Fri May 30, 2014 12:55 am

Anyone know a better way of movement?

Post by danielmccarthy »

Hi, This is my current code and it causes the player to jerk back and forth it is not smooth movement at all.

Code: Select all

#include "Movement.h"
#include "GameObject.h"
#include <Irrlicht.h>
#include <Windows.h>
 
using namespace irr;
using namespace scene;
 
Movement::Movement(GameObject* gameObj)
{
    this->gameObjToMove = gameObj;
    this->targetX = 0;
    this->targetY = 0;
    this->movementTimer = 0;
    this->positionUpdateRequired = false;
}
 
Movement::~Movement()
{
 
}
 
void Movement::process()
{
 /* Move the game object if a position update is required */
 
    if (gameObjToMove->positionUpdateRequired)
    {
        IAnimatedMeshSceneNode* sceneNode = (IAnimatedMeshSceneNode*)gameObjToMove->getObjSceneNode();
        core::vector3df nodeCoords = sceneNode->getPosition();
        time_t tCount = GetTickCount();
        time_t timeSince_Millis = tCount - this->movementTimer;
        /* Some processors are slower than others because of this it is important
         * to calculate how many + 1 movements they have missed out on and move the player
         * appropriately. */
        s16 posToAdd;
        if ((timeSince_Millis > 100))
        {
 
        /* This code needs revising, as it is not working completely right */
            posToAdd = (timeSince_Millis) / 100;
            if (posToAdd == 0)
                posToAdd = 1;
            // Prevents jumping loop when close to coordinates
            else if ((nodeCoords.X > this->targetX && nodeCoords.X < this->targetX + posToAdd)
                 || (nodeCoords.Y > this->targetY && nodeCoords.Y < this->targetY + posToAdd))
            {
                posToAdd = 1;
            }
 
            posToAdd = 1;
 
            if(nodeCoords.X > this->targetX)
                    nodeCoords.X -= posToAdd;
            else if (nodeCoords.X < this->targetX)
                    nodeCoords.X += posToAdd;
 
            if(nodeCoords.Z > this->targetY)
                    nodeCoords.Z -= posToAdd;
            else if (nodeCoords.Z < this->targetY)
                    nodeCoords.Z += posToAdd;
 
            sceneNode->setPosition(core::vector3df(nodeCoords.X, 0, nodeCoords.Z));
            sceneNode->updateAbsolutePosition();
            if (nodeCoords.X == this->targetX && nodeCoords.Y == this->targetY)
            {
                this->positionUpdateRequired = false;
                this->targetX = 0;
                this->targetY = 0;
            }
 
            this->movementTimer = GetTickCount();
 
        }
    }
}
void Movement::walkTo(unsigned short targetX, unsigned short targetY)
{
    this->targetX = targetX;
    this->targetY = targetY;
    this->positionUpdateRequired = true;
    this->movementTimer = GetTickCount();
}
 
My implementation at the moment is you extend your class to the Movement class.

Anyone know a way to get smoother movement please?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Anyone know a better way of movement?

Post by mongoose7 »

1. You are always adding 1 whereas the amount you should add depends on the scale. You should have

Code: Select all

float posToAdd = (float) timeSince_Millis*scale/100.0f;
2. You can't compare two floating point numbers for equality. You should use code like

Code: Select all

if (nodeCoords.X > this->targetX - epsilon && nodeCoords.X < the->targetX + epsilon)
{
}
else if (nodeCoords.X > this->targetX)
{
}
else
{
}
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Re: Anyone know a better way of movement?

Post by Virion »

multiply your movement with delta time to get smooth and framerate-independent movement.
http://irrlicht.sourceforge.net/forum/v ... hp?t=35143
Post Reply