我是靠谱客的博主 勤奋乌冬面,这篇文章主要介绍游戏数据传输帧同步中,自定义浮点(float)、二维向量(vector2)、三维向量(vecter3)、四元数(Quaternion)的数据类型的实现,现在分享给大家,希望可以做个参考。

由于帧同步需要各客户端的浮点数不能出现误差,所以需要使用经过处理过的浮点数。
不只是浮点数本身,所有基于浮点数实现的数据类型都要经过处理,包括Vector2、Vector3、Quaternion等等

由csharp代码实现,直接拖入unity可直接使用

自定义浮点数类型:mFloat

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 添加该标签可以保证该数据类型可以被json等工具正常序列化 [Serializable] class mFloat{ // 存储中会使用的真实值 public int realValue = 0; // 默认处理的位数 public int scale = 1000; // 程序中正常调用需使用的值 public float value{ set{ // 整存 realValue = (int)(value * (float)scale); } get{ // 零取 return (float)realValue / (float)scale; } } // 初始化赋值 public mFloat(float f){ this.value = f; } // 符号重载 public static mFloat operator+ (mFloat a, mFloat b){ return new mFloat(a.value + b.value); } public static mFloat operator- (mFloat a, mFloat b){ return new mFloat(a.value - b.value); } public static mFloat operator* (mFloat a, mFloat b){ return new mFloat( a.value * b.value); } public static mFloat operator/ (mFloat a, mFloat b){ return new mFloat(a.value / b.value); } }

自定义二维向量数据类型:mVector2

基于自定义浮点数据mFloat

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[Serializable] class mVector2 { public mFloat x; public mFloat y; public mVector2(){ this.x = new mFloat(0f); this.y = new mFloat(0f); } public mVector2(float x, float y){ this.x = new mFloat(x); this.y = new mFloat(y); } public void Set(float x, float y){ this.x = new mFloat(x); this.y = new mFloat(y); } // vector2处理 public void Set(Vector2 a){ this.x = new mFloat(a.x); this.y = new mFloat(a.y); } // vector2还原 public Vector2 Get(){ return new Vector2(this.x.value, this.y.value); } public static mVector2 operator+ (mVector2 a, mVector2 b){ return new mVector2(a.x.value + b.x.value, a.y.value + b.y.value); } public static mVector2 operator- (mVector2 a, mVector2 b){ return new mVector2(a.x.value - b.x.value, a.y.value - b.y.value); } public static mVector2 operator* (mVector2 a, mVector2 b){ return new mVector2( a.x.value * b.x.value, a.y.value * b.y.value); } public static mVector2 operator/ (mVector2 a, mVector2 b){ return new mVector2(a.x.value / b.x.value, a.y.value / b.y.value); } }

自定义三维向量数据类型:mVector3

基于自定义浮点数据mFloat

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[Serializable] class mVector3 { public mFloat x; public mFloat y; public mFloat z; public mVector3(){ this.x = new mFloat(0f); this.y = new mFloat(0f); this.z = new mFloat(0f); } public mVector3(float x, float y, float z){ this.x = new mFloat(x); this.y = new mFloat(y); this.z = new mFloat(z); } public void Set(float x, float y, float z){ this.x = new mFloat(x); this.y = new mFloat(y); this.z = new mFloat(z); } public void Set(Vector3 a){ this.x = new mFloat(a.x); this.y = new mFloat(a.y); this.z = new mFloat(a.z); } public Vector3 Get(){ return new Vector3(this.x.value, this.y.value, this.z.value); } public static mVector3 operator+ (mVector3 a, mVector3 b){ return new mVector3(a.x.value + b.x.value, a.y.value + b.y.value, a.z.value + b.z.value); } public static mVector3 operator- (mVector3 a, mVector3 b){ return new mVector3(a.x.value - b.x.value, a.y.value - b.y.value, a.z.value - b.z.value); } public static mVector3 operator* (mVector3 a, mVector3 b){ return new mVector3(a.x.value * b.x.value, a.y.value * b.y.value, a.z.value * b.z.value); } public static mVector3 operator/ (mVector3 a, mVector3 b){ return new mVector3(a.x.value / b.x.value, a.y.value / b.y.value, a.z.value / b.z.value); } }

自定义四元数据类型:mQuaternion

基于自定义浮点数据mFloat

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[Serializable] class mQuaternion { public mFloat x; public mFloat y; public mFloat z; public mFloat w; public mQuaternion(){ this.x = new mFloat(0f); this.y = new mFloat(0f); this.z = new mFloat(0f); this.w = new mFloat(0f); } public mQuaternion(float x, float y, float z, float w){ this.x = new mFloat(x); this.y = new mFloat(y); this.z = new mFloat(z); this.z = new mFloat(w); } public void Set(float x, float y, float z, float w){ this.x = new mFloat(x); this.y = new mFloat(y); this.z = new mFloat(z); this.z = new mFloat(w); } public void Set(Quaternion a){ this.x = new mFloat(a.x); this.y = new mFloat(a.y); this.z = new mFloat(a.z); this.z = new mFloat(a.w); } public Quaternion Get(){ return new Quaternion(this.x.value, this.y.value, this.z.value, this.w.value); } public static mQuaternion operator+ (mQuaternion a, mQuaternion b){ return new mQuaternion(a.x.value + b.x.value, a.x.value + b.x.value, a.z.value + b.z.value, a.w.value + b.w.value); } public static mQuaternion operator- (mQuaternion a, mQuaternion b){ return new mQuaternion(a.x.value - b.x.value, a.y.value - b.y.value, a.z.value - b.z.value, a.w.value + b.w.value); } public static mQuaternion operator* (mQuaternion a, mQuaternion b){ return new mQuaternion(a.x.value * b.x.value, a.y.value * b.y.value, a.z.value * b.z.value, a.w.value + b.w.value); } public static mQuaternion operator/ (mQuaternion a, mQuaternion b){ return new mQuaternion(a.x.value / b.x.value, a.y.value / b.y.value, a.z.value / b.z.value, a.w.value + b.w.value); } }

最后

以上就是勤奋乌冬面最近收集整理的关于游戏数据传输帧同步中,自定义浮点(float)、二维向量(vector2)、三维向量(vecter3)、四元数(Quaternion)的数据类型的实现的全部内容,更多相关游戏数据传输帧同步中,自定义浮点(float)、二维向量(vector2)、三维向量(vecter3)、四元数(Quaternion)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部