如何更高效地进行这个坐标系操作? [英] How to do this coordinate system operation more efficiently?

查看:13
本文介绍了如何更高效地进行这个坐标系操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 3D 游戏,玩家的背部应该始终面向摄像机,并且他应该朝那个方向移动.我还没有来到背对相机"部分,但我相信一旦我弄清楚如何将玩家移动到正确的方向就会很简单......

I'm making a 3D game, where the player's back should always be facing the camera and he should move in that direction. I didn't come to the "back facing the camera" part yet, but I believe that it will be simple once I figure out how to move the player in the right direction...

虽然它是一个 3D 坐标系,但高度可以忽略(z 轴),因为无论相机有多高,玩家都应该始终以相同的速度前进(相机系统的功能很像游戏魔兽世界).

Though it is a 3D coordinate system, height can be ignored (z-axis) because no matter how high the camera is, the player should always be going in the same speed (the camera system is planned to function much like in the game World of Warcraft).

现在,我已经总结了我的问题......

Now, I have summarized my problem to this...

  • Point (0, 0) 是玩家的位置.
  • Point (x, y) 是相机的位置.
  • 相机距离玩家 (dx, dy) 个单位(并且因为玩家在 (0, 0) 处,所以它也距离 (x, y) 个单位,尽管这是一个位置向量,而不是平移向量))

问题:如何在这个 2D 空间中得到一个点 (a, b),该点位于圆 r = 1 上,但与 (0, 0) 和 (x, y) 在同一条线上?

Problem: how do I get a point (a, b) in this 2D space that lies on a circle r = 1 but is on the same line as (0, 0) and (x, y)?

可视化:

通过这样做,我应该有一个二维向量 (a, b),当乘以 -30 时,它会作为玩家的速度.

By doing this, I should have a 2D vector (a, b), which would, when multiplied by -30, act as the speed for the player.

我知道如何做到这一点,但以一种非常昂贵且低效的方式,使用毕达哥拉定理、平方根和所有那些毫无疑问的工具(在 Javascript 中工作).

I know how to do this, but in a very expensive and inefficient way, using the Pythagora's theorem, square roots, and all those out-of-the-question tools (working in Javascript).

基本上是这样的:

c = sqrt(dx*dx + dy*dy); //Get the length of the line
rat = 1/c; //How many times is the desired length (1) bigger than the actual length

a = x*rat;
b = y*rat;

一定有更好的!

作为参考,我正在使用 Three.js 引擎在 Javascript 中制作游戏.

For reference, I'm making the game in Javascript, using the Three.js engine.

推荐答案

这里没有什么可以提高效率的,这些计算是 3D 场景的标准内容.

There is nothing to make more efficient here, these calculations are standard stuff for 3D scenes.

不要过早优化.这些东西不可能成为您应用的瓶颈.

Don't optimize prematurely. There is no way this stuff is a bottleneck in your app.

请记住,即使这些计算发生在每个 render() 上,它们仍然每几毫秒只发生一次 - 假设 60 FPS 为 17 毫秒,这是很多.Math.sin()/Math.cos()/Math.sqrt() 效率很高,并且每个都进行了许多其他计算render() 那些 更复杂.

Remember, even if these calculations happen on each render(), they still only happen once every several milliseconds - 17ms assuming 60 FPS, which is a lot. Math.sin() / Math.cos() / Math.sqrt() are plenty efficient, and lots of other calculations happen on each render() that are much more complex.

你现在拥有的一切都会很好.

You'll be just fine with what you have now.

这篇关于如何更高效地进行这个坐标系操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆