Ball Sync Issue

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Ball Sync Issue

arjun
2019-07-16 06:13:19

Hey Guys,
I am syncing a ball (like when the player kicks the ball, to shoot or pass). It works really well for the master client and is synced pretty well on the non-master clients as well. The issue is when any non-master client kicks the ball, the sync is not accurate enough and the ball behaves in a weird way for all clients connected to a room.
I am using RPC calls when any player client kicks the ball to send the information to add force to the ball with respective direction and force parameters.
This is the RPC call.
this.photonView.RPC("ShootBallRPC", RpcTarget.All, direction, height, directionPower, heightPower);

This is the RPC Method.
private void ShootBallRPC(Vector3 direction, Vector3 height, float directionPower, float heightPower)
{
owner = null;
rb.AddForce(((direction * directionPower) + (height * heightPower)), ForceMode.Impulse);
}

I can't really understand what might be the issue here. One issue I can think of is adding the force in non FixedUpdate() so that may be resulting in weird ball movement when the client kicks the ball. Can anyone please help me out or give me some guidance on what alternate thing I can do to resolve or sort out such issue? Thanks!

Comments

develax
2019-07-16 09:28:00

Hi, @arjun,

What does it mean "ball behaves in a weird way" ?

Physics is not supposed to be absolutely identical on different clients even if all the objects on the map are in identical states. So, I would sync slowly moving objects with standard sync methods as it's done for your players: PhotonView + (IPunObservable or PhotonTransformView/PhotonRidigbodyView).

arjun
2019-07-16 10:26:28

Hi @develax,
Thanks for the response. Yes, I am syncing ball with the methods u specified above.
"ball behaves in a weird way": By this I mean, when any non-master client player kicks the ball, each client experience the ball movement in a different way (like it is not smooth enough, for some it is quick enough and for some the ball lags while moving) even though the direction and force applied is same through RPC call. I know this may be because of different calculations done by different clients system. But I need to smooth it out and be efficient enough (ball needs to be moved with same velocity/force) for all clients connected. Looking for ways I can fix this issue. Any guidance appreciated. Really appreciate your time. Thank You.

develax
2019-07-16 11:39:18

@arjun ,
have you tried to sync not only the position and rotation of the object but also its velocities?

arjun
2019-07-16 11:55:17

@develax,
Yes, I have tried to sync the velocity also.

develax
2019-07-16 12:14:54

@arjun,
I don't know which algorithms you use and how weird it looks like (maybe mine are not better), I just can say that these ideas are the best I have checked so far:
https://forum.photonengine.com/discussion/comment/11424/#Comment_11424
http://wiki.unity3d.com/index.php?title=NetworkView_Position_Sync
They still have to be adjusted to your specific needs.

arjun
2019-07-16 12:38:13

@develax,
Thanks for the links. Really appreciate your time. I am researching a lot and looking to sort out this issue.

develax
2019-07-16 12:40:50

@arjun,
I'll be glad to know if you find something that works better or just with a different approach.

arjun
2019-07-16 12:47:24

@develax,
Yup, I will let you know once I can find a fix.

develax
2019-07-16 16:27:56

@arjun,
here is how it works at the moment. I'm still not very happy with the result. The big window is the client.

arjun
2019-07-17 04:49:36

@develax,
The ball rolling seems stable enough on both clients. In my case, the ball dribbling and rolling is also stable enough. The only issue occurs when I apply force ForceMode.Impulse (when a player kicks the ball). The ball is not stable on clients connected to the room. Can you please try applying force and observe how the ball moves/rolls for both clients?
One Question: The ball is a scene object predefined in the hierarchy right? and the master client which creates the room is the owner of the ball.

develax
2019-07-17 08:38:26

@arjun,
Yes, all the objects except tanks are part of the map. Physics works on both sides but only the master's physics is considered to be authoritative: all other clients play their own physics but when they receive data from the master (position + velocity) they make corrections.

I'll try applying force today.

develax
2019-07-17 12:58:22

@arjun

arjun
2019-07-18 04:32:38

@develax,
Seems like a little lag on first-up when impulse is applied. Same happening with the ball when a player kicks it.

develax
2019-07-18 04:59:26

@arjun,
I think the problem is that the "kick" (or "explosion" in my case is sent via RPC) which is pushed to the network by Photon immediately while the ball's position is sent via OnPhotonSerializeView which sends data 10 times per second so there can be a delay up to 100 ms after the PRC reaches another client. In addition, the interpolation takes another 150 ms and we get up to 250 ms delay in the end.

develax
2019-07-18 05:10:21

@arjun,
Perhaps the client's prediction could help. Here's a good series of articles on this topic
https://www.gabrielgambetta.com/client-side-prediction-server-reconciliation.html
but I don't know yet how to apply it to Unity physics as it will never be exactly the same position on different clients (in terms of floating-point numbers) so the prediction wouldn't work as described in that article.

arjun
2019-07-18 07:45:02

@develax,
Yup need to look for reducing delay so that syncing can be somewhat accurate and don't result in lag.

develax
2019-07-25 21:02:52

@arjun
I've improved it a bit but the delay is still sometimes noticeable. My mobile internet is quite slow, lags up to 200-300ms, that's could be the reason.

Here is a good talk "Networking for Physics Programmers by Glenn Fiedler"
https://www.youtube.com/watch?v=Z9X4lysFr64

arjun
2019-07-29 13:33:48

@develax,
how did u improve it? Can u please share code?

develax
2019-07-30 06:39:14

@arjun
I've added a few tricks for this in different classes but the main idea is to kick the object a bit earlier on the master-client. The larger the latency the earlier the ball should be kicked. It can be latency time between the master-client (150ms for example) and the client (that actually kicks the ball) multiplied by the player's velocity. But it shouldn't be too large otherwise it would be noticeable for the player on the master-client as a distance between the other player and the ball, which would look unnatural. The faster the player moves the less noticeable the distance is, but anyway you have to limit it to some value.

If we had a real server where everything is simulated we wouldn't have such a problem because other clients would see only the interpolated replication of the server's world. With PUN the master-client plays the server role for some parts like physics and the client role at the same time, so every trick is visible for the user that's why that shouldn't be noticeable.

Back to top