分享

Unity3d 网络同步算法

 遥远的桥zz 2011-06-25
作者:JESSE
* 语言:C#

* 用途:网络同步

* 项目:网络赛车竞技游戏

* 难点:竞技游戏的同步算法需要达到非常高的准确度,对于一定范围的网络延迟需要保持游戏的准确性。

对于非竞技性游戏而言,也需要有一定的准确度,但并不一定需要本算法,但可以以此算法进行借鉴性研究,因为网络同步的原理是相同的
本算法运用了,持续模拟行进轨迹,预拉扯算法(也就是导航推测算法)以及加上的三次参数法,所写得。

* 出处:本人项目原封不动的代码。



* * 本人保证绝无半点保留。只希望能让一些不知道怎么写网络的技术人员能有所借鉴,只为能提高中国技术人员水平。

*

* 本人崇尚算法第一,一切以数学 和 算法能力 来判断一个程序员的优劣性。



* 任何其他版本都非本人所写。只要你能看明白,就算是掌握了一门高科技了。

* 请持续关注博客,关于网络同步的各种探讨和总结,以及游戏构架的经验。



****************************************/





using UnityEngine;

using System.Collections;

using System.Collections.Generic;

using System;



public class NetworkController : MonoBehaviour

{

    // Use this for initialization

    void Start ()

    {

        this.rigidbody.useGravity = false;

    }

   

    // Update is called once per frame

    void Update ()

    {

        NetworkListen();

    }

   

    #region 网络监听

    private void NetworkListen()

    {

        this.rigidbody.useGravity = false;

        this.rigidbody.angularVelocity = Vector3.zero;

        

        if( SelfInfo.GamePlayerInfo.ContainsKey(this.name) && SelfInfo.GamePlayerInfo[this.name].que.Count > 0 )

        {

            long networktime = ( BaseInfo.NetWorkTime + BaseInfo.DelayBackTime + (long)((Time.time - BaseInfo.GameTime)*1000f) );

            int i;

            for( i = 0 ; i<SelfInfo.GamePlayerInfo[this.name].que.Count ; i++ )

            {

                if( networktime <= long.Parse( (string)SelfInfo.GamePlayerInfo[this.name].que[i].data[18] ) )

                {

                    break;

                }

            }

            if( i == SelfInfo.GamePlayerInfo[this.name].que.Count )

                i--;

            

            MessageElement message_item = SelfInfo.GamePlayerInfo[this.name].que[ i ];

            MessageElement message_item2;

            if( i > 0 )

            {

                message_item2 = SelfInfo.GamePlayerInfo[this.name].que[ i-1 ];

                SelfInfo.GamePlayerInfo[this.name].que.RemoveRange(0,i-1);

            }

            else

            {

                message_item2 = SelfInfo.GamePlayerInfo[this.name].que[ i ];

            }

            

            Vector3 Nowvelocity = new Vector3(float.Parse((string)message_item.data[11]) , float.Parse((string)message_item.data[12]) , float.Parse((string)message_item.data[13]) );

            Vector3 Nowvelocity2 = new Vector3(float.Parse((string)message_item2.data[11]) , float.Parse((string)message_item2.data[12]) , float.Parse((string)message_item2.data[13]) );

            Vector3 NowVelocityA = new Vector3(float.Parse((string)message_item.data[14]) , float.Parse((string)message_item.data[15]) , float.Parse((string)message_item.data[16]) );

            Vector3 NowVelocityA2 = new Vector3(float.Parse((string)message_item2.data[14]) , float.Parse((string)message_item2.data[15]) , float.Parse((string)message_item2.data[16]) );

            Quaternion Nowrotation = new Quaternion(float.Parse((string)message_item.data[7]) , float.Parse((string)message_item.data[8]) , float.Parse((string)message_item.data[9]) , float.Parse((string)message_item.data[10]) );

            Quaternion Nowrotation2 = new Quaternion(float.Parse((string)message_item2.data[7]) , float.Parse((string)message_item2.data[8]) , float.Parse((string)message_item2.data[9]) , float.Parse((string)message_item2.data[10]) );

            Vector3 Nowposition = new Vector3( float.Parse((string)message_item.data[4]) , float.Parse((string)message_item.data[5]) , float.Parse((string)message_item.data[6]) );

            Vector3 Nowposition2 = new Vector3( float.Parse((string)message_item2.data[4]) , float.Parse((string)message_item2.data[5]) , float.Parse((string)message_item2.data[6]) );

            

            //~ Debug.Log(networktime - long.Parse((string)message_item.data[18]));

            long TimeLimitExceed = 500;

            //忽略大于固定延迟时间

            if( networktime - long.Parse((string)message_item.data[18]) > TimeLimitExceed )

            {

                //~ Debug.Log(networktime - long.Parse((string)message_item.data[18]));

                return;

            }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多