Networked "Physics-Vehicle Movement Component": existing examples or implementation hints?

I’m working on a multiplayer project that will include vehicle types such as hovercrafts + spaceships. I would like to be able to enable client-side prediction in the same vein as used by the existing CharacterMovementComponent, but it looks like this really hasn’t been done elsewhere in the engine for non-character anything, even for the stock wheeled-vehicle class. Flying vehicle types seem like they should actually be a bit easier than the character movement (and certainly easier than wheeled vehicles), so two questions here, in decreasing order of priority:

  1. I’m wondering if there is a prototypical example for this floating around (even if just for 6DOF, without additional physics constraints), or if I’m stuck rolling my own? It feels like a common enough problem that there ought to be, but Googling hasn’t turned up anything.
  2. If I’m stuck rolling my own, it seems like implementing INetworkPredictionInterface (and maybe RVOAvoidance) is the place to start in a PawnMovementController subclass. Am I missing any of the major moving pieces (pun intended) here?

I was about to come in here and be all “but vehicles do have rewind/replay” because that’s what I have legitimately believed for the last, I don’t know, 11 months? Then I dug through some code and of course I am completely mistaken.

There are a few people who have looked into this and made some nice progress ( and Acren come to mind, and I’m sure there are others). It’s worth reading through their threads (like this one) and answerhub posts to get an idea of some of the challenges.

To get to the point:

  1. I haven’t seen one, but that’s obviously (see paragraph one) not a good indicator of reality. If I come across something promising I will leave it here. Having realized that my vehicle game does not have rewind/replay implemented, I have decided to spend the end of this week and the weekend attempting an implementation for my projectiles and the wheeled vehicle class itself. I haven’t looked into how to replay physics deterministically but the way the vehicle wheel states are currently replicated as a function of inputs seems promising, and I suspect the CharacterMovementComponent implementation will reveal how to correctly acknowledge move timestamps between server and clients.
  2. I glanced through some of the CharacterMovementComponent code again, and I think implementing the INetworkPredictionInterface makes sense. I authored the RVOAvoidanceInterface class and I don’t think that you should implement that unless you’re anticipating a need for Reciprocal Velocity Obstacle Avoidance. You can always implement it later you need it; if I recall you should be able to just execute it in one place (eg, the server) and depend on the movement component’s velocity in order to supply an adjusted velocity.

Before getting started I recommend reading Glenn Fiedler’s Introduction to Networked Physics.

I’ll be honest: I was really hoping to avoid implementing the whole Fiedler dance-routine myself from scratch, but I’m glad my basic analysis of the situation was pretty much correct.

I’ll probably be needing AI players at some point, but I guess RVO isn’t a priority out of the gate.

Thanks for the response, and please let me know how your experiments go this week. I’ll be sure to post updates as well if I find anything particularly interesting in CMC.

Cool. I will do that, and I’ll publicize the relevant code if it goes well.

Just a quick update. I didn’t have time last weekend to get into attempting to predict and correct vehicle movement, but I did do an experimental physics projectile implementation. It went well, but I still prefer blending the client’s simulation with the server’s and snapping velocity in most cases. However, it occurred to me (because I think I saw float this idea somewhere) that instead of replaying position and velocity deltas for a projectile, I could Tick the scene component. Perhaps that would be a better replay than what I implemented, which was very simplified and would often discard the move queue to avoid a feedback cycle after a correction. Ticking the component feels like the equivalent of replaying the move for actors that move as a result of input.

I started digging into the vehicle code but didn’t have time to try anything interesting.

Thanks for the mentions, piincone :smiley:

I actually didn’t get too deep into this stuff; we ended up switching to LAN-only, and I basically implemented Valve’s buffer interpolation method, however to do so you need a nice synchronized timestamp system that works regardless of ping, so if you have trouble with that just let me know!

I’m also happy to answer anything that I can. This stuff is tough at times, and I’m not envious of you guys trying to get wheeled vehicles to work! So, good luck :slight_smile:

Did this already for my Hovertank Movement Component:

The system is really simple, what I do is first check ‘Replicated Movement’, which will first ensure that both vehicles remain in-sync with one another from Server-Side Movement. What I then do is create a Struct which has all of my input states. I later plan on quantizing these for optimization, since in reality I only care about the first decimal point in terms of granularity:



float ThrustInput;
float StrafeInput;
float SteerInput;
float PitchValue;
bool bIsJumping;


I create two variables of that struct in the class, ONE is replicated and the other isn’t. The non-replicated struct is used in the movement component to actually move the craft and perform all calculations, and ultimately is what updates the physics of the craft. That Struct is ALSO the one that is updated via the keyboard/input component - so clients simulate their own movement locally and instantly.

I then send this movement data, on-tick, via an unreliable RPC to the server. This is the unfortunate downside as it requires me to send the RPC on tick, though for now it’s fine since I’m only doing this for one vehicle per-client and therefore the overhead is pretty small. As it’s also unreliable, the engine can choose when to send and not send packets depending on bandwidth. I might be able to get away with sending this on a Timer in future to reduce the amount of sends too, since Tick isn’t always going to have the same Delta time, and therefore bandwidth usage will fluctuate up and down depending on framerate.

The server then processes the movement on the craft from the same Input, and sends it back via the ‘ReplicatedInput’ struct. When this struct is received on the client, they update their local un-replicated copy of the struct, so that their input level matches that on the Server at that time. Additionally, the ReplicatedMovement from the Server (since it also processed the input) is sent back with it, and the position is updated on the client from that automatically.


There are two caveats to this approach at the moment. The first is that unless the Server is sending back Replicated Movement constantly (I.e, calculating it) - there’s the potential for a Client-Side collision to knock the two out-of-sync. I don’t think this is an issue really, since I’m fairly certain that this is what the CharacterMovementComponent does anyway.

The other issue (that is caused by the one above), which only became apparent in high-latency situations - is that the clients input gets simulated, but it gets overridden by the ‘Replicated Movement’. Effectively what happens is the Client waits for the Server to simulate the movement and then moves accordingly. If I can get some finer control over the order of operations here, I can work around this though. For LAN play, the controller latency is practically nothing anyway.


EDIT: I also forgot, I haven’t yet got my implementation working with AI / pathfinding, since it’s based on Input from the keyboard, and the current AI system in engine quite literally just 'set’s the velocity of an object. That’s fine, if you’re working with characters. For anything else it pretty much sucks. I’ll have to write my own interface between the two eventually, which talks directly to the input of the craft rather than essentially hacking it.

Very cool. Do you then replay the client’s inputs since the client move that was executed on the server and received by the owning client again (in the case of a disagreement)? If so, what does the replay look like? Are you ticking the component outside of the owning actor’s Tick(), or just reapplying deltas?

Character movement uses clientside prediction + rewind/replay, so the server lets the owning client know when it disagrees with the results of a move so the client can rewind back to the move in question, correct it, and replay all the clientside moves since then. If the moves aren’t replayed, you’ll notice the client’s simulation snapping back to the last one received from the server, which is of course movement from the past as far as the client is concerned.

I noticed with the wheeled vehicles that the controls start to get a bit floaty as soon as some normal latency is involved. I’m planning to give the client control, but I’ll have to sort out replaying physics states and acking timestamps between client and server (might be pestering you about that soon, @).

Of course! I am very curious about the timestamp acking. When I implemented the moves buffer for projectiles I just used a move count. Not sure if that’s a horrible idea or not. :smiley:

Not sure on that either! Could work out well or be totally terrible. Do you have any idea how you’d sync a time value across the network? Think about the value you get from GetRealWorldSeconds(), and the fact that it’ll differ on clients and server, so you could calculate an offset maybe an apply it? Maybe :wink:

No not yet, because ‘Replicated Movement’ just sets the actors location anyway, and usually the discrepancy is so tiny or non-existent than the movement is fine. In latent situations though that probably won’t be the case, so I’ll need something a bit more robust for that.

Yeah this is something I’m probably going to have to do eventually, the issue I have is because the Physics System in Unreal isn’t deterministic - there’s so much room for error. I still want to keep my physics-based movement because it’s so lightweight and fluid, but I may simply have to bite the bullet eventually and switch to something like CMC.

Glad to find this thread - I’m going to need to implement a vehicle 6DOF MovementComponent implementing INetworkPredictionInterface soon myself.

I’ve implemented a basic velocity and acceleration based movement in Blueprint but of course found it isn’t good enough for anything greater than local latency.

@TheJamsh - would you be willing to share your Hovertank Movement Component code with us? I’d be happy to share any adaptation of it for 6DOF movement back with everyone.

I still haven’t had much time to implement this (or even think about it), but it occurred to me that I could send the client move timestamp with the move, and the server would return the same move’s timestamp when it returned the result of the move to the client. Seems like it can all remain relative since the purpose is to ack the move request/response for correction - the client doesn’t need to know what time it is on the server, just needs to know if the server disagrees and which move it’s talking about. Think that would work? I’m hoping to find time to try it out in the next week or so.

  • Post Redacted - Some Content now NDA

Hey TheJamesh, thanks for sharing that! I’m looking at getting back to my 6DOF movement stuff (the old way was all Client-authoritative and only worked on LAN).

Before I get into your code in the morning, I had a few quick questions. I apologise if you’ve explained this (it’s late!), but are you using a Character? I wasn’t sure if you were replacing the default CharacterMovementComponent or just adding one to a Pawn. The latter definitely makes more sense though.

Additionally; how can you be replicating movement with the default checkbox, yet have your system at the same time? Are they not at odds?

Thanks again for the code. Did you manage to solve the problems you were talking about?

EDIT:

piinecone, did I ever explain the time sync stuff to you? I can’t remember if we talked about it at some stage.

Not solved yet, currently working on a Radar system before coming back to this… needed a break :stuck_out_tongue: The code above is part of a pretty basic PawnMovementComponent of my own, though honestly what I’ve done could just be done as part of the Pawn right now. I just wasn’t sure how tarted up it was going to get, especially when it comes to pathing and prediction etc.

You’re pretty much right, the Replicated Movement and Client simulation are fighting each other, which is why I get the latency from the controls. Other than that though, it all works. The trick is that the Physics function is being called on tick both Client & Server side, so that keeps them from going out of sync since velocity and angular velocity are always being updated. I also handle damping and drag in there as well, so when a collision happens, the drag affects the calculation on both ends and causes position/rotation updates to be sent out from the server, causing them to remain in sync. If I switch off the movement simulation and a collision occurs client-side, they stay out of sync until the server sends a position update.

I really want to look more into CMC’s system, since I want to implement based-movement (e.g., moving with the object I’m hovering on) and imparted velocity. This is still just the beginning :stuck_out_tongue: Any progress I make though, I’ll post here in one form or another!

Unfortunately since Unreal doesn’t use deterministic / seeded physics, I have to use replicated movement or otherwise I just risk client-side collisions not being handled the same way.

Okay thanks, I’ll have to spend some time parsing this thread and your post.

How have you setup your component hierarchy for the hover tanks, and how do you ensure that they rotate around the right point? For reference, when I apply torque to my root bone: http://i.gyazo.com/542581caffce3ab890b2c906e9829f94.gif Just wondering if your tanks are skeletal/static meshes attached to a basic sphere or something, and then offset, with the torque/force added to that sphere rather than the mesh, so you can tweak the point around which it rotates? Not sure which way to go with this part.

EDIT: The above was made in a rush, hope it makes sense. I ask only because your project is an example of the type of physics-driven movement not many are doing. The rotation in my gif is how it used to work in my old game, and it’d be a nice thing to fix :slight_smile:

The root object of my Tanks are the meshes themselves, so there’s no trickery :slight_smile:

What I do is multiply my Torque / Angular velocity values by the Front, Right and Up vectors (depending which axis I want to rotate around), which ensures it always pivots around it’s correct axes, and also prevents any ‘gimbal lock’ issues that you’d otherwise have to use Quats for. In the example code above for example:



Alpha += Up * SteerVal;


I’m adding to Angular Acceleration (Alpha) here, which is just the Up axis of the actor (well, the UpdatedComponent) multiplied by whatever the steer value currently is (pretty much straight off of the mouse).

I get the axes like this btw (every frame):



        FVector Front = UpdatedPrimitive->GetForwardVector();
	FVector Right = UpdatedPrimitive->GetRightVector();
	FVector Up = UpdatedPrimitive->GetUpVector();


Okay cool. Just thought you might’ve done so to make your tanks rotate as desired. Any tips for configuring pawn/mesh rotation? As in, center of mass / axis of rotation stuff.

Just looking into making my own pawn movement component, and looking at the character’s; it doesn’t seem to itself be replicated? I would’ve thought it’d be receiving RPCs and replicated variables in order to handle it internally and adjust the character’s position, but it seems not! Seems totally crazy to me. Am I right in thinking that the the pawn/character should handle the RPC/replication, and then hand that off to the movement component to let it do correction/smoothing?

Not really! I made the Center of my mesh at 0, 0, 0 in the model which works well for me!

It’s been a while since I looked but you might be right, the issue is that Clients can only call RPCS on objects that they ‘Own’. However I have been sending RPC’s in a component of my own in another case without issue, so I think so long as you own the Actor that owns the Component, you’re fine. It might just be the way they did it since it worked better for them, or possibly when they implemented it, you couldn’t call RPC’s on Actor Components! No idea on that one though…