How to create a sine wave in processing?

我只是一个虾纸丫 提交于 2019-12-12 01:52:51

问题


I would like to create a sine wave using vectors (as I am using box2d).

So far I have (in void draw())

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

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

where bridge is a chain of particles. However, this makes all the particles move back and forth at the same time whereas I would like to move like a sine wave so that each particle moves just slightly after the previous one.


回答1:


You need to add some sort of offset between each of the particles inside your loop.

Example:

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;


来源:https://stackoverflow.com/questions/43300125/how-to-create-a-sine-wave-in-processing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!