简体   繁体   English

透视投影时反转z轴

[英]Invert z-axis, when projecting in perspective

I am creating a rhythm game. 我正在创建一个节奏游戏。 But I am not using metronome to calculate¬ the positions of the notes on the screen (game interface). 但是我没有使用节拍器来计算音符在屏幕上的位置(游戏界面)。 What I have is a file with notes map that is very similar to a subtitles file. 我所拥有的是带有注释图的文件,该文件与字幕文件非常相似。

The problem is that when I project the notes in perspective, the notes are not shown correctly. 问题是当我透视投影笔记时,笔记无法正确显示。 It seems that I have inverted the Z axis and I can not change it. 似乎我已经反转了Z轴,无法更改它。 I would like to know how to change the axis correctly because. 我想知道如何正确更改轴,因为。 Or if someone can help me with other solution, I would appreciate. 或者,如果有人可以帮助我提供其他解决方案,我将不胜感激。 I tried different things but I am not capable to make appear the notes correctly. 我尝试了不同的方法,但无法正确显示笔记。

Here is the fiddle , and the function where I execute the calculation of the perspective. 这是小提琴 ,以及执行透视图计算的函数。

function updateNotes() {
    currentPosition = (sound.seek() * 1000);

    notes.forEach(function(note, index) {
    var notePosition = (currentPosition * noteSpeed) - ((note.position * noteSpeed) - deadLine);

    if (notePosition > offScreenY && notePosition < height) {
    var ball = new Ball3d(5, '#000000');
    var scale = fov / (fov + notePosition);
    console.log(notePosition);
    ball.x = halfWidth;
    ball.y = halfHeight + notePosition * scale;
    ball.scaleX = ball.scaleY = scale;
    ball.draw(context);
    balls.push(ball);
  } else {
    // elimino la nota
    balls.splice(index, 1);
  }
  });
}

Thanks in advance. 提前致谢。

The z-axis isn't inverted, the notes are coming from top to bottom, but they seem like they are going further away, as they scale down, so you have to adjust your scale-assignment. Z轴没有倒置,音符从上到下排列,但是随着它们的缩小,它们似乎在走得更远,因此您必须调整音阶分配。 When using var scale = (fov + notePosition) / fov; 当使用var scale = (fov + notePosition) / fov; it looks more like they are coming towards the user, although you'd have to adjust your formula a bit more to give it a better look. 尽管您必须稍微调整一下公式以使其外观更好,但看起来它们似乎更接近用户。

  // init variables var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), width = canvas.width = window.innerWidth, height = canvas.height = window.innerHeight, halfWidth = width / 2, halfHeight = height / 2, deadLine = height - 100, fov = 250, offScreenY = -100, currentPosition = 0, balls = [], noteSpeed = 0.3, animId; var sound = new Howl({ src: ['https://rawcdn.githack.com/noeszc/atwood/master/assets/sounds/mario.ogg'], onload: function(){ setup(); }, onend : function(){ stop(); } }); function setup(){ sound.play(); sound.mute(); drawDeadLine(); gameLoop(); } function gameLoop() { animId = requestAnimationFrame(gameLoop, canvas); context.clearRect(0, 0, width, height); updateNotes(); drawDeadLine(); } function updateNotes() { currentPosition = (sound.seek() * 1000); notes.forEach(function(note, index) { var notePosition = (currentPosition * noteSpeed) - ((note.position * noteSpeed) - deadLine); if (notePosition > offScreenY && notePosition < height) { var ball = new Ball3d(5, '#000000'); var scale = (fov + notePosition)/fov; ball.x = halfWidth; ball.y = halfHeight + notePosition * scale; ball.scaleX = ball.scaleY = scale; ball.draw(context); balls.push(ball); } else { // elimino la nota balls.splice(index, 1); } }); } // dibuja la linea base donde deben llegar las notas al ritmo function drawDeadLine() { context.beginPath(); context.moveTo(0, deadLine); context.lineTo(width, deadLine); context.stroke(); } function stop() { context.clearRect(0, 0, width, height); cancelAnimationFrame(animId); drawDeadLine(); console.log('======= end music ========='); console.log(balls); } 
 body { margin: 0; } canvas { display: block; } 
 <script src="https://rawcdn.githack.com/noeszc/atwood/master/scripts/ball3D.js"></script> <script src="https://rawcdn.githack.com/noeszc/atwood/master/scripts/notes.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.0.2/howler.min.js"></script> <canvas id="canvas"></canvas> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM