我怎样才能正确生成抛物线

问题描述 投票:0回答:1

我正在开发一个vanilla JavaScript游戏,我正在使用一个数组来存储y轴上的高度。我的问题是,作为我身高的一部分,我想创建一个带有山丘的生物群系。这需要我使用抛物线,这样我才能正确生成曲线。每个X单位都固定为图形,但y坐标可能会发生变化。

据我所知,似乎我已经写好了。来这里之前我使用了大量的参考资料。

function constructMountains(spot, last, gradient) {
  var randomHeight = (Math.random() * 1.5)+0.3;//creats a random intensity for the parabola
  var lengthOfHill = Math.floor(Math.random() * 35)+4;// creates a random width for parabola
  var parabolaVertexY = (-randomHeight*((q-lengthOfHill/2)-(lengthOfHill/2))^2)+last;//gets max height of curve
  for (var q = 0; q <= lengthOfHill; q++) {
    test[q+spot] = (-randomHeight*((q-lengthOfHill/2)-(lengthOfHill/2))^2)+parabolaVertexY;//vertex is half way through so half the length is the iterator's offset
  }
  return spot+lengthOfHill;//returns place in the main loop
}

  var biome = 1
  for (x=0; x<blockAmount + 101 + 1000; x++) {
    //set up this way for testing
    if (x == 0) {
      x = constructMountains(x, 45, toggleGradient);//toggleGradient will be used to flip curve, not implemented yet
    }
  }

这应该创造不同大小的重复抛物线。数据是可视转换的,因为我有一个使用HTML5画布来描边()坐标值的函数。它目前的行为方式完全没有意义。

javascript calculator algebra procedural-generation
1个回答
2
投票

a^b是按位异或;在Javascript中没有取幂。你在找Math.pow(a,b)

© www.soinside.com 2019 - 2024. All rights reserved.