# Tracking object velocity and direction

Hello,

I'm playing around with some server/client programming and need to know the best way to set and get an object's direction and velocity.  Velocity seems easy via the getLinearVelocity() function, but what about direction?  Seems a "getWalkDirection" function would be helpful here, but I did not see one.

Also I need to process AI movement which could be as easy as choosing a direction & velocity and then calculating it's x, y, z coords for error tracking.

Finally, I'm not even sure if I need Jbullet-jme for this, or if there are basic movement velocity settings in jme2, I would actually prefer simple movement rather than a full physics engine as this is going to be kind of a slow paced MMORPG type thing rather than a realist physics world.

Any guidance is appreciated, thank you!

well you could do the physic/more map player collision detection clientside, but then you have to take care of cheaters.

Transmit the Rotation Quaternion, then you have the direction.

Thank you, I was checking out the FlagRush source, and it looks like I could use the setLocalTranslation… I just need to do it more smoothly with some prediction rather than waiting for the server to give you every single location to setLocalTranslation() to.

Thanks again!

Hmm, I may be overthinking this and not seeing the obvious easy answer, so before I give myself a brain aneurysm, perhaps someone can steer me in the right direction…

For managing character movement I'm currently planning on holding the x, y, z coords on the server and passing them to the clients.  I also want to hold and pass to clients the velocity of each character.  The thing that is hurting my brain is how do I determine/decide the velocity?

I was going to work in a speed rate of world units per second.  Lets say I assign a max velocity of a given character to 60 Units per second and the character is moving to Vector 600, 0, 1000 from Vector 0, 0, 0.

First thing that comes to mind is simply compare character.x & character.z to target.x & target.z and in this given case add 60 to character.x & character.z over a second.

Thing that is wrong with this is moving 60 units in the x direction and 60 units in the z direction is a higher velocity than 60 units per second.  I believe this particular problem is pretty easy to solve with some simple math, however is this even necessary?

Is there an easier way to…

a) express Velocity between Server & Client.  I looked at the setLinearVelocity function in jbullet-jme, but I'm not sure that I need jbullet-jme since this game does not require a real physics engine.

b) How do I determine what the velocity value should be from an AI perspective?  If an AI character is at 0,0,0 and I want to move it to 200, 0, 500 how do I determine what it's velocity should be in the x, y, z directions?

Sorry if my explanation is poor, but I suppose I'm asking what a character.moveTo(new Vector3f(x, y, z), maxVelocity) function would look like for the AI?  And once I get this information, I would have to pass it along to the clients and plug in to a setLinearVelocity type function, although I'm not sure if I'm going to have to use jbullet-jme yet.

I played around with jbullet-jme, and believe it's brilliant, but I also believe it would be much harder to sync up the world when you factor in all the variables of a physics engine as opposed to a simple characters are moving at a static rate.

Thank you for any insight.

Well actually I dont do any intelligent tricks,

instead, camera turning is cleintside (modified flycamera)

position is stored serverside and sent every 50ms

between the 50ms the client interpolates the position linear,

the client sends every 50ms all data (pressed key, rotation) to the server

actualy there is nearly no delay feelable, however if you do the turning server side, the game instantly feels very shitty, seems like we recognize a delayed movement rarly while a delayed turning instantly tells us something is wrong.

Empire Phoenix said:

Well actually I dont do any intelligent tricks,

instead, camera turning is cleintside (modified flycamera)
position is stored serverside and sent every 50ms
between the 50ms the client interpolates the position linear,
the client sends every 50ms all data (pressed key, rotation) to the server
actualy there is nearly no delay feelable, however if you do the turning server side, the game instantly feels very shitty, seems like we recognize a delayed movement rarly while a delayed turning instantly tells us something is wrong.

Another way to improve it is not to sync the position every 50 ms, but instead to let the client update it freely and only sync it if it goes way beyond of what the server has.
Did you try doing dial-up simulation, e.g having 200 ms latency, bandwidth limits, etc. It might reveal problems that you didn't see before.

If an AI character is at 0,0,0 and I want to move it to 200, 0, 500 how do I determine what it's velocity should be in the x, y, z directions?

Quite simple, subtract the destination position from the character's position, and then normalize it. If you did it correctly, then for the values 0,0,0 and 200,0,500 you should get 0.37139, 0, 0.92848.
Another way to improve it is not to sync the position every 50 ms, but instead to let the client update it freely and only sync it if it goes way beyond of what the server has.
Did you try doing dial-up simulation, e.g having 200 ms latency, bandwidth limits, etc. It might reveal problems that you didn't see before.

This is how I would like to do it, transmit changes in velocity, and do an x, y, z compare every given interval to make sure the locations do not get out of wack between client and server.

If an AI character is at 0,0,0 and I want to move it to 200, 0, 500 how do I determine what it's velocity should be in the x, y, z directions?

Quite simple, subtract the destination position from the character's position, and then normalize it. If you did it correctly, then for the values 0,0,0 and 200,0,500 you should get 0.37139, 0, 0.92848.
[/quote]

Thank you!  What do these values 0.37139, 0, 0.92848 represent?  The direction to travel to get to 200, 0, 500?  Or did you plug in my velocity example of 60 units per second?  And what exactly does the normalize function do?  I'm going to go read the description in the javadocs real quick, but in case you can give a better explanation.  :)

Also, is it a lot more work to get jbullet-jme working for this client/server environment than it is to do simple setLocalTranslations?  I'm also keeping in mind that if I do not use jbullet-jme, I'm going to have to come up with other collision detection methods for the terrain and other world objects like buildings, trees, etc.

Edit: Sorry, some of this can be answered from the Bullet tutorial, I'm just trying to understand what's behind the functions because the server has to help calculate positions without the help of jME functions.
Thank you!  What do these values 0.37139, 0, 0.92848 represent?  The direction to travel to get to 200, 0, 500?  Or did you plug in my velocity example of 60 units per second?  And what exactly does the normalize function do?  I'm going to go read the description in the javadocs real quick, but in case you can give a better explanation.  Smiley

The values represent a direction vector. Direction vectors are unit vectors, which means they have a length of 1.0. You can convert any vector to a unit vector by normalizing it.
To get a velocity of 60 units per second with that direction, you simply multiply it by the scalar 60.0.

Also, is it a lot more work to get jbullet-jme working for this client/server environment than it is to do simple setLocalTranslations?  I'm also keeping in mind that if I do not use jbullet-jme, I'm going to have to come up with other collision detection methods for the terrain and other world objects like buildings, trees, etc.

Physics is sometimes considered a bit too much to handle for a server, especially MMO servers. However, if you only use PhysicsCharacterNodes (which don't use real physics), the strain might be less, normen might know better. Implementing your own collision algorithm isn't simple, as you have to account moving objects and implement collision response.

Well I tested it over my umts when it was over limit and reduced to isdn speed, was around that ping the, worked fine enough. (And only affected me, for all others thee was no change)

Actually JBullet is quite fast, it can handle around 2500 moving colliding objects acceptable (or several thousand pseudo bullets based on the jbullet ray). Using good optimized collisionshapes is the key to success here

-> around 300 player + stuff for terrain and map

So it depends on what scale you plan a mmorpg and what server system, but i usually prefer server side calculations, as they cannot be cheated that easy. Also the server is less more than an arraylist of position rotation and  jbulletshapes, (Not to mention that norman will add gpu physic,wich will change that dramatically, so you 'only' need to get a server with a grafic/tesla card then)

Excellent, thank you very much kind sirs!

So I have come to the realization that I need to use a physics engine on the server. I believe this means I need to create a full jme virtual world and that I either need to prevent the rendering of the world or drop the frame rate to the minimal level required to update the clients.

So, is there an easy way to stop JME from rendering the world and just update object positions physics etc and/or can I have the update calls occur to say 5-20 FPS without any adverse effects on the world?

I remember Norman saying jbullet updates at 60 FPS will that occur regardless of what the frame rate is set to?

And is there any feature or effect I can shut off in jbullet to lower the CPU utilization to make it more MMO friendly?  I'm just looking for basic collision detection and whether other players can actually see each other, etc.

Thank you

sb81 said:

I remember Norman saying jbullet updates at 60 FPS will that occur regardless of what the frame rate is set to?

And is there any feature or effect I can shut off in jbullet to lower the CPU utilization to make it more MMO friendly?  I'm just looking for basic collision detection and whether other players can actually see each other, etc.

Well 60fps is the "physics accuracy" of jbullet. First of all you dont have to do the graphics on your server, so that part can go to the physics as well, you only need to move around physics nodes. You can lower the accuracy of the physics with getPhysicsSpace().setAccuracy() if you really need to do that but what you get is.. well.. inaccurate physics ;)