简体   繁体   English

如何将相机的位置合并到此算法中以计算精灵在屏幕上的位置?

[英]How can I incorporate the camera's position into this algorithm to calculate a sprite's position on the screen?

I'm working on a 2D rendering system in 3D space, for practice. 我正在研究3D空间中的2D渲染系统。 The sprites are 2D, but they're rendered in 3D space. 精灵是2D的,但它们是在3D空间中渲染的。 The "camera" can move in 3D space and turn 360 degrees horizontally. “相机”可以在3D空间中移动并水平旋转360度。 I'm having trouble figuring out the right formula to calculate, based on the position/rotation of the camera and the position of the assets, where they should exist on the screen. 我在根据相机的位置/旋转度以及资产在屏幕上应存在的位置,找出正确的公式进行计算时遇到了麻烦。

What I have is like this: 我所拥有的是这样的:

chunk.assets.forEach(asset => {
    let x = Math.round(
      asset.coords.x * Math.cos(angle) - asset.coords.y * Math.sin(angle)
    );
    let y = Math.round(
      asset.coords.y * Math.cos(angle) + asset.coords.x * Math.sin(angle)
    );
    if (!depthMap[y]) {
      depthMap[y] = [];
    }
    depthMap[y].push(asset);
});

But this does not take into account the camera's (player's) position (stored at player.coords.x, player.coords.y ), only the angle the camera/player is facing ( angle ). 但这不考虑摄像机(玩家)的位置(存储在player.coords.x, player.coords.y ),仅考虑摄像机/玩家面对的angleangle )。 So right now the camera can't move. 因此,现在相机无法移动。 Note: Depth map is just storing the assets in order so the renderer knows which order to render the sprites so things appear in the right order based on which is closer to the player/camera. 注意:深度图只是按顺序存储资产,因此渲染器知道渲染精灵的顺序,以便根据哪个更接近播放器/相机,事物以正确的顺序出现。

How can I incorporate the camera's position into this algorithm? 如何将相机的位置纳入此算法?

Some assumptions: 一些假设:

If the camera and the sprite are at the same position, the sprite will be rendered at 0|0. 如果相机和子画面位于同一位置,则子画面将渲染为0 | 0。

Wether the camera moves left or the sprite moves right does not matter. 相机向左移动还是精灵向右移动都无关紧要。

From that we can conclude that only the relative position matters, and that can be easily calculated (by subtracting both positions). 由此可以得出结论,只有相对位置很重要,并且可以很容易地计算出(通过减去两个位置)。

 let x = Math.round(
  (asset.coords.x - player.coords.x) * Math.cos(angle) - (asset.coords.y - player coords.y) * Math.sin(angle)
);
let y = Math.round(
  (asset.coords.y - player.coords.y) * Math.cos(angle) + (asset.coords.x - player.coords.x) * Math.sin(angle)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用TWEEN动画相机的位置 - How to use TWEEN to animate the camera's position 如何修复随机振动的精灵,同时使用设置为精灵的camera.position作为中心向鼠标移动? - How can I fix sprite vibrating randomly which is moving towards the mouse while using camera.position set to the sprite as the center? 如何使用javascript在计算机屏幕上移动窗口的位置? - How do I move a window's position on the computer screen with javascript? 如何固定输入的位置,使其始终位于屏幕中央的元素下方,但其大小可以更改? - How do I fix the position of an input so it's always below an element that's centered on the screen but its size can change? ThreeJS:屏幕位置到摄像机位置 - ThreeJS: Screen position to camera position 如果 position 与另一个精灵相同,则停止精灵的移动 - Stopping movement of sprite if it's same position as another sprite 如何将元素放在绝对位置的元素下? - How can I put elements under a absolute position's element? DOM如何获取元素在“队列”中的位置 - DOM How can I get element's position in “queue” 如何放大和正确 position 来自精灵的图像? - How can I enlarge & correctly position an image from a sprite? 如何并排放置两个下拉菜单? - How can I position two dropdown's side by side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM