Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建一个对象';s在processing.js中基于速度的移动_Javascript_Processing_Processing.js - Fatal编程技术网

Javascript 创建一个对象';s在processing.js中基于速度的移动

Javascript 创建一个对象';s在processing.js中基于速度的移动,javascript,processing,processing.js,Javascript,Processing,Processing.js,我试图画出翅膀根据速度摆动的鸟。简言之,我的鸟飞得越快,它们的翅膀摆动得就越快 下面是我的完整代码,它不符合我的要求。在我的显示和移动机翼功能中,我所做的是使飞行因子根据速度的大小而变化。然而,当我绘制所有东西时,我发现这一点,因为速度一直在变化(即由绘制函数中的随机加速度力触发),所以飞行因子也是如此,这意味着我的鸟翅膀根本不摆动,看起来很像童车 有什么办法让它工作吗?我希望我的鸟的翅膀看起来像是自然摆动的,只是随着我的鸟加速或减速而越来越慢 提前非常感谢 angleMode = "degre

我试图画出翅膀根据速度摆动的鸟。简言之,我的鸟飞得越快,它们的翅膀摆动得就越快

下面是我的完整代码,它不符合我的要求。在我的
显示和移动机翼
功能中,我所做的是使
飞行因子
根据
速度
的大小而变化。然而,当我
绘制
所有东西时,我发现这一点,因为
速度
一直在变化(即由
绘制
函数中的随机
加速度
力触发),所以
飞行因子
也是如此,这意味着我的鸟翅膀根本不摆动,看起来很像童车

有什么办法让它工作吗?我希望我的鸟的翅膀看起来像是自然摆动的,只是随着我的鸟加速或减速而越来越慢

提前非常感谢

angleMode = "degrees";

var Bird = function(m,x,y){
    this.mass = m;
    this.position = new PVector(x,y);
    this.velocity = new PVector(0,0);
    this.acceleration = new PVector(0,0);
};

Bird.prototype.applyForce = function(force) {
    var f = PVector.div(force, this.mass);
    this.acceleration.add(f);
};

Bird.prototype.update = function() {
    this.velocity.add(this.acceleration);
    this.position.add(this.velocity);
    this.acceleration.mult(0);
    this.velocity.limit(3);
};

Bird.prototype.displayAndMoveWings = function() {
    this.start=-110;
    this.stop = this.start + 110;
    this.flyingFactor=cos(360+frameCount*this.velocity.mag()*10)*20;
    stroke(0, 0, 0);
    strokeWeight(2);
    noFill();
    arc(this.position.x,this.position.y,this.mass,this.mass,this.start-this.flyingFactor,this.stop-this.flyingFactor);
    arc(this.position.x+this.mass,this.position.y,this.mass,this.mass,this.start+this.flyingFactor-70,this.stop+this.flyingFactor-70);  
};

Bird.prototype.checkEdges = function() {

  if (this.position.x > width) {
    this.position.x = 0;
  } else if (this.position.x < 0) {
    this.position.x = width;
  }

  if (this.position.y > height) {
    this.position.y = 0;
  } else if (this.position.y < 0) {
    this.position.y = height;
  }
};

var bird1 = new Bird(15, width/2, height/2);

var draw = function() {
    background(255, 255, 255);
    bird1.update();
    bird1.displayAndMoveWings();
    var randomAcceleration = new PVector(random(-3,3),random(-3,3));
    bird1.checkEdges();
    bird1.applyForce(randomAcceleration);
};
angleMode=“度”;
var Bird=函数(m,x,y){
这个质量=m;
这个位置=新的PVector(x,y);
这个速度=新的PVector(0,0);
该加速度=新的PVector(0,0);
};
Bird.prototype.applyForce=函数(力){
var f=PVector.div(力,该质量);
本.加速度.加(f);
};
Bird.prototype.update=函数(){
这个。速度。加(这个。加速度);
this.position.add(this.velocity);
这个.加速度.mult(0);
这个速度极限(3);
};
Bird.prototype.displayAndMoveWings=函数(){
这个.start=-110;
this.stop=this.start+110;
this.flyingFactor=cos(360+帧数*this.velocity.mag()*10)*20;
冲程(0,0,0);
冲程重量(2);
noFill();
圆弧(this.position.x,this.position.y,this.mass,this.mass,this.start this.flyingFactor,this.stop this.flyingFactor);
弧(此.position.x+此.mass,此.position.y,此.mass,此.mass,此.start+此.flyingFactor-70,此.stop+此.flyingFactor-70);
};
Bird.prototype.checkEdges=函数(){
if(this.position.x>宽度){
这个.position.x=0;
}else if(此位置x<0){
此.position.x=宽度;
}
如果(此.position.y>高度){
此.position.y=0;
}否则如果(此位置y<0){
此.position.y=高度;
}
};
var bird1=新鸟(15,宽/2,高/2);
var draw=function(){
背景(255、255、255);
bird1.update();
bird1.display和movewings();
var randomAcceleration=新PVector(随机(-3,3),随机(-3,3));
bird1.checkEdges();
bird1.施加力(随机加速度);
};

我想我可能找到了一个黑客。。。在使用
display和movewings
函数之前,我创建了一个名为
whatIsVelocity
的新全局变量。然后在
display和movewings
中,我写了以下内容:

if (frameCount % 60 === 0){
    whatIsVelocity = this.velocity.mag();
}
this.flyingFactor=cos(360+frameCount*whatIsVelocity*10)*20;
实际上,这是每60帧(大约2秒)拉一次速度,并将该速度引入
flyingFactor
,这意味着它不会像我前面的代码那样每帧重新计算一次。不是很漂亮,但它达到了我的目的