如何在处理中创建正弦波?

How to create a sine wave in processing?

我想使用矢量创建一个正弦波(因为我正在使用 box2d)。

到目前为止我有(在void draw()

Vec2 mov2 = new Vec2(sin(angle)*scalar,0);

for (int j = 0; j <= 10; j++) {
   bridge.particles.get(j).body.setLinearVelocity(mov2);
}

其中 bridgeparticles 的链。但是,这会使所有粒子同时来回移动,而我想像正弦波一样移动,以便每个粒子在前一个粒子之后稍微移动。

您需要在循环内的每个粒子之间添加某种偏移量。

示例:

for( int i=0; i < 360; i++ ){   
         float x = 1 + i;
         float y = (float)(Math.sin( Math.toRadians(i+currentOffset)));
         bridge.particles.get(j).setTransform(x, y, 0);
 }
currentOffset+=1;