Jacob Cafiero
Jacob Cafiero

Reputation: 145

How can I properly generate a parabola

I am working on a vanilla JavaScript game and I am using an array for stored heights on a y axis. My issue is that as a part of my height generation I want to create a biome with hills. This requires me to use parabolas so that I can properly generate the curves. Each X unit is fixed like a graph, but the y coordinates are subject to change.

To my knowledge it seems I have written it out properly. I used plenty of references before coming here.

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
    }
  }

This should create varying sizes of repeated parabolas. The data is visually transferable because I have a function that uses the HTML5 canvas to stroke() the coordinate values. The way it is currently behaving makes absolutely no sense.

Upvotes: 1

Views: 427

Answers (1)

Zach Sadler
Zach Sadler

Reputation: 580

a^b is bitwise XOR; not exponentiation in Javascript. You're looking for Math.pow(a,b).

Upvotes: 3

Related Questions