NCF参数化建筑论坛

标题: 新手Processing代码求教 [打印本页]

作者: xujieflank    时间: 2014-6-11 02:51
标题: 新手Processing代码求教
有几年没碰语言了,重拾起来倍感吃力,跟着教材模拟小球收重力和风力两个力影响的情况,写出来不知道问题出在哪里。。有很多基本的概念也半懂不懂。。向论坛里求解,什么是cannot make a static reference to the non-static method applyForce

class Mover{
    PVector location;
    PVector velocity;
    PVector acceleration;
    float mass;
   
    Mover(){
      mass=1;
      location=new PVector( 30,30);
      velocity=new PVector (0,0);
      acceleration= new PVector(0,0);
    }
   
    void applyForce(PVector force){
      PVector f = PVector.div(force,mass);
      acceleration.add(f);
    }
    void update(){
      velocity.add(acceleration);
      location.add(velocity);
      acceleration.mult(0);
    }
    void display(){
      stroke(0);
      fill(175);
      ellipse(location.x,location.y,mass*16,mass*16);
    }
    void checkedge(){
      if (location.x>width){
        location.x=width;
        velocity.x*=-1;
      }else if (location.x < 0) {
        velocity.x *= -1;
        location.x = 0;
      }if (location.y > height) {
        velocity.y *= -1;
        location.y = height;
      }
    }
}
   
void setup(){
  size(200,500);
  background(255);
  
}

void draw(){
  PVector wind= new PVector (0.01,0);
  PVector gravity= new PVector (0,0.1);
  Mover.applyForce(wind);
  Mover.applyForce(gravity);
  Mover.update();
  Mover.display();
  Mover.checkedge();
}



作者: jasonroc    时间: 2014-6-11 11:09
不懂java的飘过~期待高手答疑~ 另外很想知道,楼主学processing的原因是啥呢?是爱好还是觉得掌握了对设计有帮助?还是其他的原因呢?最近看好多人都在学啊,正在犹豫呢~
作者: 月之眼    时间: 2014-6-11 12:44
请加40391970这个群。processing的版主会给你解答的
作者: xujieflank    时间: 2014-6-13 09:41
jasonroc 发表于 2014-6-11 11:09
不懂java的飘过~期待高手答疑~ 另外很想知道,楼主学processing的原因是啥呢?是爱好还是觉得掌握了对设计有 ...

嗯,我只是对交互设计感兴趣所以在开始看些散的知识~
作者: xujieflank    时间: 2014-6-13 09:42
月之眼 发表于 2014-6-11 12:44
请加40391970这个群。processing的版主会给你解答的

好的,谢谢已加
作者: staychild    时间: 2014-6-25 09:59
我也在尝试在processing中模拟风场
请问lz看的什么教材?
作者: staychild    时间: 2014-6-25 10:01
我也在尝试在processing中模拟风场
请问lz看的什么教材?
作者: staychild    时间: 2014-6-25 10:02
我也在尝试在processing中模拟风场
请问lz看的什么教材?
作者: 野行    时间: 2014-8-10 16:14
你混淆了class 和Object的概念。
你的Mover是一个class,你需要在这个class下定义一个变量。

修改过的代码:
class ClassNameMover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;

  ClassNameMover() {
    mass=1;
    location=new PVector( 30, 30);
    velocity=new PVector (0, 0);
    acceleration= new PVector(0, 0);
  }

  void applyForce(PVector force) {
    PVector f = PVector.div(force, mass);
    acceleration.add(f);
  }
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);
  }
  void display() {
    stroke(0);
    fill(175);
    ellipse(location.x, location.y, mass*16, mass*16);
  }
  void checkedge() {
    if (location.x>width) {
      location.x=width;
      velocity.x*=-1;
    } else if (location.x < 0) {
      velocity.x *= -1;
      location.x = 0;
    }
    if (location.y > height) {
      velocity.y *= -1;
      location.y = height;
    }
  }
}


PVector wind= new PVector (0.01, 0);
PVector gravity= new PVector (0, 0.1);
ClassNameMover Mover;


void setup() {
  size(200, 500);
  background(255);
  Mover=new ClassNameMover();
}

void draw() {

  Mover.applyForce(wind);
  Mover.applyForce(gravity);
  Mover.update();
  Mover.display();
  Mover.checkedge();
}


我这里图省事把你的class名称改成了 ClassNameMover,定义的object变量沿用了你之前的Mover;
作者: 小浣熊    时间: 2014-9-2 07:20
好东西 我是小白一个 对processing 很感兴趣,另外 有哪位大神 能提供私人帮助吗 有偿的 十分紧急 谢谢!




欢迎光临 NCF参数化建筑论坛 (http://bbs.ncf-china.com/) Powered by Discuz! X3.2