自己的物理引擎:弹跳并没有停止

时间:2017-04-04 11:48:54

标签: javascript html html5-canvas

我创建了一个简单的move()原型,其中包含vyfunction Ball(radius,x,y,vx,vy,color){ this.radius = radius; this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.color = color; this.gravity = 0.6; this.friction = { air: 0.005, bounce: 0.3 }; } Ball.prototype.move = function(){ this.x += this.vx; this.y += this.vy; //Gravity this.vy += this.gravity; //Air Friction this.vx /= 1+this.friction.air; this.vy /= 1+this.friction.air; //Bounce Border if(this.x<this.radius){//Left this.x = this.radius+((this.radius-this.x)/(1+this.friction.bounce)); this.vx /= -(1+this.friction.bounce); } if(this.x>width-this.radius){//Right this.x = (width-this.radius)-((this.x-(width-this.radius))/(1+this.friction.bounce)); this.vx /= -(1+this.friction.bounce); } if(this.y<this.radius){//Top this.y = this.radius+((this.radius-this.y)/(1+this.friction.bounce)); this.vy /= -(1+this.friction.bounce); } if(this.y>height-this.radius){//Bottom this.y = (height-this.radius)-((this.y-(height-this.radius))/(1+this.friction.bounce)); this.vy /= -(1+this.friction.bounce); } }; Ball.prototype.draw = function(){ ctx.beginPath(); ctx.arc(this.x,this.y,this.radius,0,2*Math.PI,false); ctx.fillStyle = this.color; ctx.fill(); }; var ctx, clock, ball width = 300, height = 150; window.onload = function(){ ball = new Ball(20,150,30,4,0,"red"); var canvas = document.getElementById('canvas'); canvas.width = width; canvas.height = height; ctx = canvas.getContext('2d'); clock = setInterval(main,33); }; function main(){ ctx.clearRect(0,0,width,height); ball.draw(); ball.move(); }函数。球应该在地板,墙壁和天花板上反弹。然而,出于某种原因,虽然速度(canvas{ background-color: black; })不断下降,但它并没有停止弹跳......你知道我做错了什么吗?

&#13;
&#13;
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Bouncy Balls</title>
		<link rel="stylesheet" href="style.css">
		<script src="ball.class.js"></script>
		<script src="script.js"></script>
	</head>
	<body>
		<div align="center">
			<canvas id="canvas"></canvas>
		</div>
	</body>
</html>
&#13;
def a():
    print(x)

def b():
    global x 
    x = 1
    a()

b()
&#13;
pd.merge(df1.reset_index(), df2.reset_index(),on='a', how='outer').set_index(['a','b'])

         c   d
a b           
1 1.0  1.0  11
  2.0  3.0  11
2 1.0  2.0  12
3 NaN  NaN  13
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

我认为你的问题是,当你的球反弹时,它仍会在整个嘀嗒声中向下加速,并且因为它会比它应该反弹得更远。

编辑: 因此,这只是y方向的问题。