Need help with PhysicsCharacterNode.setWalkDirection(…) --Solved

Hello,



I’m trying to use the setWalkDirection method for moving my Character in my game.



What I want to do is : Making one of my character move from point A to point B in an exact amount of time.



For what I’ve understood, the speed of the character will depends of the direction vector given in argument to the method setWalkDirection(…).

But I dont know how this speed is calculated.



I’ve tried several things and the one which seem the nearest to what I want is :



[java]

float timeInSecond = (float)timeNeeded / 1000f;

Vector3f direction = new Vector3f( ( step.getDestination().getX() - currentPos.getX() ) / timeInSecond / 60.0f,

( step.getDestination().getY() - currentPos.getY() ) / timeInSecond / 60.0f ,

( step.getDestination().getZ() - currentPos.getZ() ) / timeInSecond / 60.0f ); // Why 60 ? Still asking myself !! TODO



physicNode.setWalkDirection( direction );



//Look at correct direction

Quaternion modelRotation = new Quaternion();

Vector3f look = new Vector3f( direction.getX() * 100000,

0 ,

direction.getZ() * 100000 ); //In order to look to the horizon

modelRotation.lookAt( look, Vector3f.UNIT_Y);

character.getSpatialNode().setLocalRotation( modelRotation );



if( moveAnimationChannel == null || moveAnimationChannel.getAnimationName() == null || ! moveAnimationChannel.getAnimationName().equals( “Walk” ) )

{

moveAnimationChannel.setAnim( “Walk” );

moveAnimationChannel.setLoopMode( LoopMode.Loop );

moveAnimationChannel.setSpeed( 0.5f );

}

[/java]



Can someone explain me how this speed is calculated ?



Thanks in advance



PS :

I’m trying to create a method like moveFromPointToPoint( origin, destination, timeToReachDestination )

If someone know a method in the API that do the same thing. Well, please don’t hide it :slight_smile:

The speed should be m/s where 1m=1unit in the physics.

And how does it works?



[java]

physicNode.setWalkDirection( new Vector3f( 1, 1, 1 ) );

[/java]

Each second, it goes 1 meter on the X, Y and Z coordinates?



Edit :

It is more like meter / minute

Here is a test I’ve done :



Move : From : 14.5, 0.055692673, 14.5
Move : To : 14.5, 0.0, 15.5
Move : Direction : (0.0, -9.282112E-4, 0.016666668)

It takes 1 second to reach ( 14.5, 0.0, 15.5 ).

I you go downwards its surely gonna take some time :stuck_out_tongue: (0/-9/0.01)?? The length of the vector is the speed in m/s.

Well int fact, it is ( 0, 0.0009, 0.0166 ).

So nearly ( 0, 0, 1/60 )



length ~= 1/60 → 1 unit done in one second

Uhm, I dont know, when I set the vector to 1,0,0 it pretty much moves with one unit per second I think…

Erf!!



Can there be other parameters that need to be taken into account when calculating this ?

Like the

[java]

character.setGravity(…)

[/java]

or a

[java]

character.setCcdMotionThreshold(…)

[/java]



My physics nodes where build from scaled Model. Does that impact something ?

Dont think so, no

Strange.



Well it works with a factor ( 1 / 60 ) on my computer. I will stay with that for the moment.



I’m not working with the last nighly build ( 11 / 16 / 2010 ) so I will se if it changes on my next update.



Thank you for your answers :slight_smile:

You say a vector of 0,0,1/60 moves your character 1 unit per second?

Edit: I saw I scale the vector down afterwards in the test, so it might be the vector is physics-fps dependent, explaining the factor.

Yes.



I’ve calculated it with the tpf of the method ‘simpleUpdate’ or via an external timer.



I will print you a log.



Current position : (14.5, 0.055692673, 14.5)
Walk direction : (0.0, -9.282112E-4, 0.016666668)
Time since last action : 0.1038125
Time since last action : 0.20265624
Time since last action : 0.29384375
Time since last action : 0.39371875
Time since last action : 0.49712503
Time since last action : 0.5951562
Time since last action : 0.695375
Time since last action : 0.7963126
Time since last action : 0.8960937
Time since last action : 0.9948126
Current position : (14.499998, 0.0, 15.48332)
Walk direction : (3.1789146E-8, 0.0, 0.016944664)
Time since last action : 0.10309376
Time since last action : 0.20193751
Time since last action : 0.301
Time since last action : 0.40021864
Time since last action : 0.49940616
Time since last action : 0.6012811
Time since last action : 0.69921875
Time since last action : 0.8005626
Time since last action : 0.9012812
Time since last action : 1.0004374
Current position : (14.499998, 0.0, 16.499737)


So each second, the character has moved 1 unit on Z coordinate. So walkDirection is nearly ( 0, 0, 1/60 )
1 Like

Ok, cool so you solved the mystery of the Character speed (see my edit above) :wink:

1 Like

Cool, I will stick with that then. :slight_smile: