开启左侧

通过JS实现对世界指定坐标渲染模型与坐标系变换

[复制链接]
Harlotte 作者认证 2024-5-9 20:15:06

还没有账号?赶快去注册吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 Harlotte 于 2024-5-9 20:27 编辑

如下,我完成了以下几个函数用于实现转换坐标系,实测可用。

  1. var test1 = new Vector3f(-4260,-41,4667);
  2. function render(ctx, state, train) {
  3.     let iii = getTrainLocalPositionFromWorldPosition(test1,train.lastCarPosition[0],train.lastCarRotation[0]);
  4.     let rrr = vectMirrorFlip(train.lastCarRotation[0]);
  5.     matxuan = new Matrices();
  6.     matxuan.translate(iii.x(),iii.y(),iii.z());
  7.     matxuan.rotateX(rrr.x());
  8.     matxuan.rotateY(rrr.y());
  9.     matxuan.rotateZ(rrr.z());
  10.     ctx.drawCarModel(models["yan1"], 0, matxuan);
  11. }
  12. function getWorldPositionFromTrainLocalPosition(localPosition , wordpost , wordrotate){//转换列车局部坐标到世界坐标
  13.     let mat = new Matrix4f();
  14.     mat.rotateX(wordrotate.x());
  15.     mat.rotateY(wordrotate.y());
  16.     mat.rotateZ(wordrotate.z());
  17.     mat.translate(localPosition.x(),localPosition.y(),localPosition.z());
  18.     return vectadd(wordpost,mat.getTranslationPart())
  19. }
  20. function getTrainLocalPositionFromWorldPosition(localPosition , wordpost , wordrotate){//转换世界坐标到列车局部坐标
  21.     let mat = new Matrix4f();
  22.     mat.rotateX(-wordrotate.x());
  23.     mat.rotateY(-wordrotate.y());
  24.     mat.rotateZ(-wordrotate.z());
  25.     mat.translate(localPosition.x()-wordpost.x(),localPosition.y()-wordpost.y(),localPosition.z()-wordpost.z());
  26.     return mat.getTranslationPart();
  27. }
  28. function vectMirrorFlip(vect){//镜像翻转
  29.     return new Vector3f(-vect.x(),-vect.y(),-vect.z());
  30. }
  31. function vectadd(v1,v2){//向量相加
  32.     return new Vector3f(v1.x()+v2.x(),v1.y()+v2.y(),v1.z()+v2.z());
  33. }
复制代码
第一段:声明一个世界坐标与一个简单的应用方式
第二段:一个将列车坐标系转为世界坐标系的函数
第三段:一个将世界坐标系转为列车坐标系的函数
第四段:一个辅助函数,将vector3f的向量反转
第五段:一个辅助函数,将vector3f的向量相加

但现在仍存在一些问题,如通过示例方法渲染的模型在列车运行时会存在一定抖动,这与render函数内的逻辑复杂程度有关,延迟会使获取的坐标、旋转有差异。可能使其运行在一个新的js函数会有改进。但其仅仅是会有抖动,幅度不超过0.2m?

希望各位可以将此玩出花来!
令附:欢迎入群来玩我的追加包群836291719

2024年5月9日

Aphrodite

ShentongMetro 作者认证 2024-5-10 16:41:44
我倒觉得可以setTimeout(()=>{while(true){;}},1)之类的开个异步解决,但这样又要遇到著名的锁问题了(js可没synchonorous关键字用)

MTRSHM成员,又叫XHG78999
私信一概不看,有事QQ联系
楼主 Harlotte 作者认证 2024-5-10 18:21:58
ShentongMetro 发表于 2024-5-10 16:41
我倒觉得可以setTimeout(()=>{while(true){;}},1)之类的开个异步解决,但这样又要遇到著名的锁问题了(js可 ...

zbx说那个获取坐标的因为运行次数不是和帧率同步的所以会有偏差(哭
ShentongMetro 作者认证 2024-5-11 18:05:15
Harlotte 发表于 2024-5-10 18:21
zbx说那个获取坐标的因为运行次数不是和帧率同步的所以会有偏差(哭

这样啊
(想到复杂的解决方案:根据列车方向和瞬时速度预估列车位置然后相对

MTRSHM成员,又叫XHG78999
私信一概不看,有事QQ联系
楼主 Harlotte 作者认证 2024-5-11 21:51:36
ShentongMetro 发表于 2024-5-11 18:05
这样啊
(想到复杂的解决方案:根据列车方向和瞬时速度预估列车位置然后相对
...

好好好好好好好好好
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表