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

概述

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

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

自定义浮点数类型:mFloat

// 添加该标签可以保证该数据类型可以被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

[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

[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

[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)的数据类型的实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部