如何在处理中创建正弦波? [英] How to create a sine wave in processing?

查看:116
本文介绍了如何在处理中创建正弦波?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用矢量创建正弦波(就像我使用的 box2d )。

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

所以我到目前为止(在 void draw()中)

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);
}

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

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.

推荐答案

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

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

示例:

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;

这篇关于如何在处理中创建正弦波?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆