gpt4 book ai didi

javascript - 球弹跳问题

转载 作者:行者123 更新时间:2023-11-29 10:28:20 24 4
gpt4 key购买 nike

<分区>

“我尝试使用一些基本的运动方程:v^2 = u^2 + 2as,并将它乘以一个变量:bouncing_factor。这给了我一个非常好的动画,但我希望球逐渐减速然后停止。如果 bouncing_factor 为 0,则球会进行奇怪的运动。请解决这两个问题。“

{		var c = document.getElementById("gameCanvas");
var ctx = c.getContext("2d");

var width = 600;
var height = 600;

var x = width / 2;
var y = 100;
var r = 50;
var mass = 1;

var vY = 0;
var a = 0.2;

var ground = height - r;
var bouncing_factor = 1; // keep this <1 & >0

function draw() {
ctx.clearRect(0, 0, width, height);

if (y < ground) {
// gives acceleration
vY += a;
y += vY;
} else {
vY = -Math.sqrt (vY * vY + 2 * a * 500) * bouncing_factor;
y = (ground - 1);
}

// ball
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(x, y, r, 0, 2*Math.PI);
ctx.fill();

requestAnimationFrame(draw); // loops this function
}
draw();}
<canvas id='gameCanvas'></canvas>

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com