How to apply gravity to bouncing balls in javascript -

后端 未结 1 1155
眼角桃花
眼角桃花 2020-12-30 16:26

I want to move from having a ball bounce back and forth around a canvas to having some gravity and eventually dropping. I know i need to change the velocity when it reaches

相关标签:
1条回答
  • 2020-12-30 16:55

    You were very close, think of the gravity as a constant downwards velocity increment, so in each step you need to add that to your vy calculation.

    "I know i need to change the velocity when it reaches the bottom"`

    That is not true because gravity affects objects ALL the time. When you touch the bottom, things like material dampening and surface friction can happen.

    var canvas,
      ctx,
      cx = 100,
      cy = 100,
      vx = 2,
      vy = 5,
      radius = 5,
      gravity = 0.2,
      damping = 0.9,
      traction = 0.8,
      paused = false;
      ;
    
    function init() {
    
      canvas = document.getElementById("gameCanvas");
      ctx = canvas.getContext("2d");
      
      canvas.width = 300;
      canvas.height = 150;
    
      circle();
    }
    
    function circle() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      if (!paused)
        requestAnimationFrame(circle);
    
      if (cx + radius >= canvas.width) {
        vx = -vx * damping;
        cx = canvas.width - radius;
      } else if (cx - radius <= 0) {
        vx = -vx * damping;
        cx = radius;
      }
      if (cy + radius >= canvas.height) {
        vy = -vy * damping;
        cy = canvas.height - radius;
        // traction here
        vx *= traction;
      } else if (cy - radius <= 0) {
        vy = -vy * damping;
        cy = radius;
      }
    
      vy += gravity; // <--- this is it
    
      cx += vx;
      cy += vy;
    
      ctx.beginPath();
      ctx.arc(cx, cy, radius, 0, 2 * Math.PI, false);
      ctx.fillStyle = 'green';
      ctx.fill();
    }
    
    init();
    
    // fancy/irrelevant mouse grab'n'throw stuff below
    canvas.addEventListener('mousedown', handleMouseDown);
    canvas.addEventListener('mouseup', handleMouseUp);
    
    function handleMouseDown(e) {
      cx = e.pageX - canvas.offsetLeft;
      cy = e.pageY - canvas.offsetTop;
      vx = vy = 0;
      paused = true;
    }
    
    function handleMouseUp(e) {
      vx = e.pageX - canvas.offsetLeft - cx;
      vy = e.pageY - canvas.offsetTop - cy;
      paused = false;
      circle();
    }
    canvas {border: 1px solid black; cursor: crosshair;}
    p {margin: 0;}
    <canvas id="gameCanvas"></canvas>
    <p>Throw the ball by holding and releasing the left mouse button on the canvas (reverse slingshot)</p>

    0 讨论(0)
提交回复
热议问题