如何做到这一点的坐标系统运行更有效率? [英] How to do this coordinate system operation more efficiently?

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

问题描述

我正在做一个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...

虽然这是一个三维坐标系统,高度可忽略(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...

  • 点(0,0)是玩家的位置。
  • 在点(x,y)是摄像机的位置。
  • 照相机(DX,DY)单元远离播放器(和因为玩家为(0,0),它也是(X,Y)的单位距离,虽然这是一个位置矢量,而不是一个翻译1 )

问题:我如何得到一分(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)?

可视化:

通过这样做,我应该有一个2D矢量(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.

我知道如何做到这一点,但在很昂贵和低效率的方式,使用Pythagora定理,平方根,以及所有那些外的所述-问题的工具(在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;

必须有更好的东西!

There must be something better!

有关的参考,我正在做游戏在Javascript中,使用Three.js引擎。

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.

不优化prematurely。有没有办法这个东西是在你的应用程序的瓶颈。

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

请记住,即使这些计算发生在每个渲染(),他们仍然只发生一次,每隔数毫秒 - 17MS假设60 FPS,这是的很多的。 Math.sin() / Math.cos() / 的Math.sqrt()大量高效,和许多其他的计算发生在每个渲染()的要复杂得多。

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天全站免登陆