Javascript 如何在P5js库中获取鼠标拖动对象的速度

Javascript 如何在P5js库中获取鼠标拖动对象的速度,javascript,canvas,processing,p5.js,Javascript,Canvas,Processing,P5.js,我正在尝试使用P5.js库制作一个脚本,让用户拖放受重力影响的球。当用户落下球时,我需要测量它的速度。 有了这些信息,我就能以两种不同的方式行事: 如果球没有速度落下,它就会直线下落 受重力的影响 如果球在水平移动时落下,那么它需要遵循该轨迹,当然会受到重力的影响 如何获得球的瞬时速度?有人能帮我吗 代码是: function Ball() { this.diameter = 50; this.v_speed = 0; this.gravity = 0.2; t

我正在尝试使用P5.js库制作一个脚本,让用户拖放受重力影响的球。当用户落下球时,我需要测量它的速度。 有了这些信息,我就能以两种不同的方式行事:

  • 如果球没有速度落下,它就会直线下落 受重力的影响
  • 如果球在水平移动时落下,那么它需要遵循该轨迹,当然会受到重力的影响
  • 如何获得球的瞬时速度?有人能帮我吗

    代码是:

    function Ball() {
    
        this.diameter = 50;
        this.v_speed = 0;
        this.gravity = 0.2;
        this.starty = height/2 - 100;
        this.ypos = this.starty;
        this.xpos = width/2;
    
        this.update = function(){
    
            this.v_speed = this.v_speed + this.gravity; 
            this.ypos    = this.ypos + this.v_speed;
    
            if (this.ypos >= height-this.diameter/2){
    
                this.v_speed *= -1.0; // change direction
                this.v_speed = this.v_speed*0.9; 
    
                if ( Math.abs(this.v_speed) < 0.5 ) {
                    this.ypos = this.starty;
                }
            }
        }
    
        this.show = function(){
            ellipse(this.xpos, this.ypos, this.diameter);
            fill(255);
        }
    }
    
    函数球(){
    这个直径=50;
    该速度=0;
    这是重力=0.2;
    此.starty=高度/2-100;
    this.ypos=this.starty;
    这是1.xpos=宽度/2;
    this.update=函数(){
    这个速度=这个速度+这个重力;
    this.ypos=this.ypos+this.v_速度;
    如果(this.ypos>=高度this.diameter/2){
    该速度为*=-1.0;//改变方向
    此.v_速度=此.v_速度*0.9;
    if(数学绝对值(此速度)<0.5){
    this.ypos=this.starty;
    }
    }
    }
    this.show=函数(){
    椭圆(this.xpos,this.ypos,this.diameter);
    填充(255);
    }
    }
    
    编写一个函数,检查球上是否有XY坐标:

    this.onBall=函数(x,y){
    设dx=x-this.xpos;
    设dy=y-this.ypos;
    设dist=Math.sqrt(dx*dx,dy*dy)
    return dist=this.maxY){
    this.ypos=this.maxY;
    该速度为*=-1.0;//改变方向
    此.v_速度=此.v_速度*0.9;
    }
    //如果允许投球,则必须跳过以下步骤
    //直达天空(顶部屏幕外)
    
    如果(this.ypos)是Hi的可能副本,谢谢你的回答!为什么垂直速度没有发挥应有的作用?水平速度很好,但垂直速度在你尝试垂直击球时效果不佳
    // calculate gravity
    
    this.v_speed  = this.v_speed + this.gravity; 
    this.ypos = this.ypos + this.v_speed;
    
    if (this.ypos >= this.maxY){
        this.ypos = this.maxY;
        this.v_speed *= -1.0; // change direction
        this.v_speed = this.v_speed*0.9; 
    }
    
    // the following has to be skipped if the ball is allowed to be thrown
    // up to the sky (out of the screen at the top)
    if (this.ypos <= this.minY){
        this.ypos = this.minY;
        this.v_speed *= -1.0;
        this.v_speed = this.v_speed*0.9;
    }
    
    // calculate side movement
    
    this.xpos = this.xpos + this.v_speed_x;
    if ( this.xpos <= this.minX){
        this.xpos = this.minX;
        this.v_speed_x *= -1;
    }
    if ( this.xpos >= this.maxX){
        this.xpos = this.maxX;
        this.v_speed_x *= -1;
    }
    this.v_speed_x = this.v_speed_x * 0.99;