Javascript 如何让球挂在绳子上?
我正在尝试使用P5.js库制作一个简单的游戏,其中一个球用绳子固定在一个大头针上,当球落下时,它不能超过绳子的长度。 球必须落下,直到它与销的距离等于绳子的长度。我怎么做?我只需要它和y轴一起工作 下面是一些代码: var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; 这一点。piny=高度/2; 这个球直径=50; 此值为0.x=宽度/2; 此.bally=高度/2+200; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ropeWidth/2; this.ropey=this.piny; this.updatePin=函数{ } this.updateBall=函数{ this.ballSpeed=this.ballSpeed+1; this.bally=this.bally+this.ballSpeed; } this.updateRope=函数{ } this.show=函数{ ellipsethis.pinx,this.piny,this.pinDiameter; 填充255; 仰泳; ellipsethis.ballx,this.bally,this.ballDiameter; 填充255; 仰泳; recthis.ropex,this.ropey,this.ropeWidth,this.ropeHeight; 填充255; 仰泳; } } 功能设置{ CreateCanvas600600; 化合物=新化合物; } 函数图{ 背景0; 复方阿替平 composite.updateBall composite.updateRope 复合表演 } 希望能有帮助 var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; 这一点。piny=高度/2; 这个球直径=50; this.ballx=this.pinx; this.bally=this.piny+100; 此值为0.ballSpeed=0; 这个重力=0.5; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ropeWidth/2; this.ropey=this.piny; this.onPin=函数x,y{ 设dx=x—this.pinx; 让dy=y-这个.piny; 设dist=Math.sqrtdx*dx,dy*dy 返回距离此。piny+200{ this.bally=this.piny+200; 此值为0.ballSpeed=0; } } this.updateRope=函数{ 如果是这个,拖动{ this.ropey=this.piny; this.ropehight=this.ropehight计算; } } this.show=函数{ ellipsethis.pinx,this.piny,this.pinDiameter; 填充255; 仰泳; ellipsethis.ballx,this.bally,this.ballDiameter; 填充255; 仰泳; recthis.ropex,this.ropey,this.ropeWidth,this.ropeHeight; 填充255; 仰泳; } } 功能鼠标按下{ 如果是component.onPinmouseX,mouseY 复合捕获; } 功能鼠标{ 复合液滴; } 功能设置{ CreateCanvas600600; 化合物=新化合物; } 函数图{ 背景0; 复方阿替平 composite.updateBall composite.updateRope 复合表演 }Javascript 如何让球挂在绳子上?,javascript,canvas,p5.js,Javascript,Canvas,P5.js,我正在尝试使用P5.js库制作一个简单的游戏,其中一个球用绳子固定在一个大头针上,当球落下时,它不能超过绳子的长度。 球必须落下,直到它与销的距离等于绳子的长度。我怎么做?我只需要它和y轴一起工作 下面是一些代码: var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; 这一点。piny=高度/2; 这个球直径=50; 此值为0.x=宽度/2; 此.bally=高度/2+200; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ro
从OP代码开始,这里有一个示例,它允许球沿着绳子向下滑动,就像绳子穿过球一样,并且在末端有一个结 var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; this.piny=this.pinDiameter; 这个球直径=50; 此值为0.x=宽度/2; this.bally=this.ballDiameter+this.pinDiameter*2; 这是1.ballSpeed=1; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ropeWidth/2; this.ropey=this.piny; this.updatePin=函数{ } this.updateBall=函数{ 如果this.bally