如何让球挂在绳子上?

How to make a ball hang from a rope?

我正在尝试使用 P5.js 库制作一个简单的游戏,其中一个球用绳子连接到一个销钉上,当它掉落时不能超过绳子的长度。 球必须下落,直到它与瓶的距离等于绳索的长度。我该怎么做?我只需要它与 y 轴一起工作。

这是一些代码:

var compound;

function Compound(){
    this.pinDiameter = 25;
    this.pinx = width/2;
    this.piny = height/2;

    this.ballDiameter = 50;
    this.ballx = width/2;
    this.bally = height/2 + 200;

    this.ropeWidth = 4;
    this.ropeHeight = 200;
    this.ropex = this.pinx - this.ropeWidth/2;
    this.ropey = this.piny;

    this.updatePin = function(){
    }

    this.updateBall = function(){
        this.ballSpeed  = this.ballSpeed + 1; 
        this.bally = this.bally + this.ballSpeed;
    }

    this.updateRope = function(){
    }

    this.show = function(){
        ellipse(this.pinx, this.piny, this.pinDiameter);
        fill(255);
        noStroke();

        ellipse(this.ballx, this.bally, this.ballDiameter);
        fill(255);
        noStroke();

        rect(this.ropex, this.ropey, this.ropeWidth, this.ropeHeight);
        fill(255);
        noStroke();
    }
}

function setup() {
    createCanvas(600, 600);
    compound = new Compound();
}

function draw() {
    background(0);
    compound.updatePin()
    compound.updateBall()
    compound.updateRope()
    compound.show()
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.min.js"></script>

希望对您有所帮助

var compound;

    function Compound(){
        this.pinDiameter = 25;
        this.pinx = width/2;
        this.piny = height/2;
        
        this.ballDiameter = 50;
        this.ballx = this.pinx;
        this.bally = this.piny + 100;
        this.ballSpeed = 0;
        this.ballGravity = 0.5;
        
        this.ropeWidth = 4;
        this.ropeHeight = 200;
        this.ropex = this.pinx - this.ropeWidth/2;
        this.ropey = this.piny;
        
        this.onPin = function(x, y) {
            let dx = x - this.pinx; 
            let dy = y - this.piny; 
            let dist = Math.sqrt(dx*dx, dy*dy)
            return dist <= this.pinDiameter/2; 
        }
        
        this.ropeheightcalc = function(){
            let dx = this.bally - this.piny; 
            return dx;
        }
        
        this.drag = false;  
        this.catch = function() {
              this.drag = true;
              this.mousex = mouseX;
              this.mousey = mouseY;
        }
    
        this.drop = function() {
              this.drag = false;  
        }
        
        this.updatePin = function(){
            if (this.drag) {
                this.piny = mouseY; 
                this.mousey = mouseY;
            }
        }
        
        this.updateBall = function(){
            this.ballSpeed  = this.ballSpeed + this.ballGravity; 
            this.bally = this.bally + this.ballSpeed;
            
            if(this.bally > this.piny + 200){
                this.bally = this.piny + 200;
                this.ballSpeed = 0;
            }
        }
    
        this.updateRope = function(){
            if (this.drag) {
                this.ropey = this.piny; 
                this.ropeHeight = this.ropeheightcalc();
            }
        }
        
        this.show = function(){
            ellipse(this.pinx, this.piny, this.pinDiameter);
            fill(255);
            noStroke();
            
            ellipse(this.ballx, this.bally, this.ballDiameter);
            fill(255);
            noStroke();
            
            rect(this.ropex, this.ropey, this.ropeWidth, this.ropeHeight);
            fill(255);
            noStroke();
        }
    }
    
    function mousePressed() {
        if ( compound.onPin(mouseX, mouseY))
            compound.catch();
    }
    
    function mouseReleased() {
        compound.drop();
    }
    
    function setup() {
        createCanvas(600, 600);
        compound = new Compound();
    }
    
    function draw() {
        background(0);
        compound.updatePin()
        compound.updateBall()
        compound.updateRope()
        compound.show()
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.min.js"></script>

从这里的 OP 代码开始是一个示例,它允许球沿着绳子滑动,就好像绳子穿过球并在末端打结。

var compound;

function Compound(){
    this.pinDiameter = 25;
    this.pinx = width/2;
    this.piny = this.pinDiameter;

    this.ballDiameter = 50;
    this.ballx = width/2;
    this.bally = this.ballDiameter + this.pinDiameter * 2;
    this.ballSpeed = 1;

    this.ropeWidth = 4;
    this.ropeHeight = 200;
    this.ropex = this.pinx - this.ropeWidth/2;
    this.ropey = this.piny;

    this.updatePin = function(){
    }

    this.updateBall = function(){

    if (this.bally < this.piny + 200){
        this.ballSpeed  = this.ballSpeed + 1; 
        this.bally = this.bally + this.ballSpeed;
    }
    }

    this.updateRope = function(){
    }

    this.show = function(){
        ellipse(this.pinx, this.piny, this.pinDiameter);
        fill(255);
        noStroke();

        ellipse(this.ballx, this.bally, this.ballDiameter);
        fill(255);
        noStroke();

        rect(this.ropex, this.ropey, this.ropeWidth, this.ropeHeight);
        fill(255);
        noStroke();
    }
}

function setup() {
    createCanvas(600, 600);
    compound = new Compound();
 setFrameRate(1);
}

function draw() {
    background(0);
    compound.updatePin()
    compound.updateBall()
    compound.updateRope()
    compound.show()
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>

主要变化是在更新中,我们需要检查球是否脱离绳索。我们还将球的位置初始化为靠近绳索的顶部,这样我们就可以看到它向下滑动。

this.updateBall = function(){
    if (this.bally < this.piny + 200){
        this.ballSpeed  = this.ballSpeed + 1; 
        this.bally = this.bally + this.ballSpeed;
    }
}