Best method for character movment.

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Best method for character movment.

Post by The_Glitch »

Okay I'm just looking to setup a basic character movement. This character moves independent of the camera, camera is really just a 3rd person type in the scene.

Right now I was just using a vector which got the characters position and rotation and in the loop I was moving a character along the X axis and then trying to get him to rotate and begin moving on that new X axis direction but trouble is he moves fine and begins to rotate but just continues on the positions old normal facing.

Basically begins moving forward then rotates lets say 45.0 degrees ,but then doesn't move along that new direction but just keeps going the old direction facing.

Code: Select all

 
 
        vector3df pos_character = node->getPosition();
    vector3df rotation_character = node->getRotation();
 
                        pos_character.X++;  // Loop is here
            rotation_character.Y++;
 
 
            node->setPosition(pos_character / 32);
            node->setRotation(rotation_character / 32);
 
 
I'm sure there is a code snippet somewhere but most I found were for the camera mostly tied to a node or "player" mines more for AI not controlled by the camera.

Also the above is for prototyping ^^ an example I know it's crap LOL
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Best method for character movment.

Post by mongoose7 »

I would think that rotation is applied before translation, as the reverse would not make sense. So you need to position the character using world coordinates. Alternatively, parent the character to an empty scene node. But I would think it best to work directly in world coordinates because I'm sure there will come a time when you will want to know where your character actually is.
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Best method for character movment.

Post by The_Glitch »

What's the best way to start working in world coordinates in Irrlicht?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Best method for character movment.

Post by mongoose7 »

rot = rot + deltarot;
pos.x = pos.x + deltapos*cos(rot);
pos.z = pos.z + deltapos*sin(rot);
node->setRotation(vector3df(0, rot, 0));
node->setPosition(pos);

Rotate around the Y-axis, change the current position in the direction of the current rotation.
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Best method for character movment.

Post by The_Glitch »

Yeah I worked out something earlier today what do you think of this:

Code: Select all

 
 
                          vector3df rotation_character = node->getRotation();
 
                        vector3df moveforward= vector3df(0.05f, 0, 0);
            node->getAbsoluteTransformation().transformVect(moveforward);
            node->setPosition(moveforward);
 
               node->setRotation(rotation_character );
 
 
 
Only thing I need is to make it frame independent.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Best method for character movment.

Post by mongoose7 »

I'm not sure. 'moveforward' is a "vector", not a "position". I would think it would need to be added to the current position. But if it works then OK.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Best method for character movment.

Post by Seven »

Search for a posting of mine with get in. Get left. Getup. Getpointinfront. Etc... It will give you what you need
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Best method for character movment.

Post by The_Glitch »

Alright I'll check and see if I can find it.
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Best method for character movment.

Post by The_Glitch »

I found your post I tried your method I must be using it wrong because with the method I posted above, If I tell the node in the loop to move along the X axis and any rotation applied the node does move in the direction of the new axis after any rotation's. Yours didn't seem to do that but I'm positive I must be using it wrong.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Best method for character movment.

Post by Seven »

Code: Select all

 
    // simple functions for AI useage
        virtual vector3df getPointInFront(float distance)   {   vector3df p = getPosition(); p += distance * getIn(); return p; }
        virtual vector3df getPointInBack(float distance)            {   vector3df p = getPosition(); p -= distance * getIn(); return p;       }
        virtual vector3df getPointAbove(float distance)     {   vector3df p = getPosition(); p += distance * getUp(); return p;     }
        virtual vector3df getPointBelow(float distance)     {   vector3df p = getPosition(); p -= distance * getUp(); return p;     }
     
        vector3df CSObject::getPointInFrontAndAbove(float d, float u)  
        {  
            vector3df p = getPointInFront(d);
            p.Y = getPointAbove(u).Y;
            return p;  
        };
     
        vector3df CSObject::getPointLeftAndAbove(float d, float u) 
        {  
            vector3df p = getPosition();
            p -= d * getLeft();
            p.Y = getPointAbove(u).Y;
            return p;  
        };
     
        vector3df CSObject::getPointInBackAndAbove(float d, float u)   
        {  
            vector3df p = getPointInBack(d);
            p.Y = getPointAbove(u).Y;
            return p;  
        };
     
        vector3df CSObject::getIn()
        {
            if (getPhysicsObject()) return getPhysicsObject()->getIn();
            else
            if (getPrimarySceneNode())
            {
                matrix4 mat = getPrimarySceneNode()->getRelativeTransformation();
                vector3df in(mat[8],mat[9],mat[10]);
                in.normalize();
                return in;
            } else return vector3df(0,0,0);
        }
     
        vector3df CSObject::getUp()
        {
            if (getPhysicsObject()) return getPhysicsObject()->getUp();
            else
            if (getPrimarySceneNode())
            {
                matrix4 mat = getPrimarySceneNode()->getRelativeTransformation();
                vector3df up(mat[4],mat[5],mat[6]);
                up.normalize();
                return up;
            }
            else return vector3df(0,0,0);
        }
     
        vector3df CSObject::getLeft()
        {
            if (getPhysicsObject()) return getPhysicsObject()->getLeft();
            else
            if (getPrimarySceneNode())
            {
                matrix4 mat = getPrimarySceneNode()->getRelativeTransformation();
                vector3df left(mat[0],mat[1],mat[2]);
                left.normalize();
                return left;
            }
            else return vector3df(0,0,0);
        }
     
 
Seven
     
    Posts: 713
    Joined: Mon Nov 14, 2005 2:03 pm
 
Top
 

Code: Select all

 
IAnimatedMeshSceneNode* node = createthenode();
 
to rotate the node
node->setRotation(somerotation);
 
to move forward
node->setPosition(node->getPosition() * getIn(node) * speed;
 
to move backward
node->setPosition(node->getPosition() * getIn(node) * -speed;
 
to strafe left
node->setPosition(node->getPosition() * getLeft(node) * speed;
 
to strafe right
node->setPosition(node->getPosition() * getLeft(node) * -speed;
 
to turn left
node->setRotation(node->getRotation() + somerotationvector;
 
 
Ruxify
Posts: 33
Joined: Tue Oct 16, 2012 12:37 am

Re: Best method for character movment.

Post by Ruxify »

Here's how I would do it.

Code: Select all

//First get the position and rotation.
vector3df opos = mynode->getAbsolutePosition();
vector3df orot = mynode->getRotation();
 
//Now apply translation/rotation.
float movement_speed = 1 * time_step; //time_step is the time since the last frame in seconds.
float turning_speed = 2 * time_step; //This prevents movement speed changes at different frame rates.
 
//Use += and -= for different directions.
orot.Y += turning_speed;
 
opos.X += movement_speed * sin((orot.Y * 3.14) / 180);
opos.Z += movement_speed * cos((orot.Y * 3.14) / 180);
 
//Set the new position and rotation.
mynode->setPosition(opos);
mynode->setRotation(orot);
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Best method for character movment.

Post by The_Glitch »

Thanks bud I'll try your method also.
Post Reply