使用Processing和box2d创建单波
我在box2d世界中有一系列物体。这些机构被链接在一起。我想在按下鼠标时沿着身体创建一个正弦波。我只希望这个波发生一次,它应该以相同的幅度沿着身体的长度继续,直到它结束,然后停止,直到再次按下鼠标。使用Processing和box2d创建单波
目前,我有这样的:
float angle = 0.0;
float scalar = 1.2;
float speed = 0.01;
void mousePressed() {
for (int j = 0; j < 91; j++) {
float x = sin(j+angle)*scalar;
float y = 0;
Vec2 mov2 = new Vec2(x,y);
bridge.particles.get(j).body.setLinearVelocity(mov2);
angle+=speed;
}
}
然而,这将导致机构成为一个连续波刚刚向外扩展如下(只是在一瞬间尝试这种对左侧股):
如何创建一个向下移动的单波形?
使用从@dfour修改后的代码,我用:
void mousePressed() {
int frequency = 10; // sine frequency (larger for longer wave)
double fullCircle = Math.toRadians(180); // get 1 full iteration of a circle in radians;
float x=0;
float y=0;
for(int i = 0; i < 100 ; i++){
if(i > fullCircle*frequency){
// after first wave so output 0
//System.out.println(0);
}else{
// part of first sinewave so output wave value
x =(float)Math.sin(i/frequency);
Vec2 mov2 = new Vec2(x,y);
print(" x: "+x);
System.out.println(Math.sin(i/frequency));
bridge.particles.get(i).body.setLinearVelocity(mov2);
}
}
}
来自正弦波的单波只需要循环,一旦第一波完成输出0;
int frequency = 10; // sine frequency (larger for longer wave)
double fullCircle = Math.toRadians(360); // get 1 full iteration of a circle in radians;
for(double i = 0; i < 75 ; i++){
if(i > fullCircle*frequency){
// after first wave so output 0
System.out.println(0);
}else{
// part of first sinewave so output wave value
System.out.println(Math.sin(i/frequency));
}
}
编辑:
我已经使用LibGdx框架,所有的工作测试这一点。若要将此你的代码,你将需要添加一个计时器字段存储时间:
private float sineTimer = 50f; //initially 50f as 0 would start wave
private final int PARTICLES = 40; // the amount of particles in your bridge
然后在您的clickMethod补充:
sineTimer = -35f; // resets timer
现在,在你的主循环地址:
sineTimer += Gdx.graphics.getDeltaTime() * 10; // increment time since last frame
int frequency = 3; // sine frequency (larger for longer wave)
float fullCircle = (float) Math.toRadians(360); // get 1 full iteration of a circle in radians;
// loop through all particles
for(int i = 0; i < PARTICLES ; i++){
float offset = i; // set base offset
offset+=sineTimer; // add timer value to offset
// if offset is lower than 0 or past first sine wave set particle to default place
if(offset > fullCircle*frequency || offset < 0){
bridgeParticles.get(i).setTransform(32,i, 0);
}else{ // else apply sine position (I used x*3 here to amplifiy sine on x axis)
float x =(float)Math.sin(offset/frequency);
bridgeParticles.get(i).setTransform((x *3) + 32, i, 0);
}
}
修改代码处理环境:
private float sineTimer = 50f; //initially 50f as 0 would start wave
private final int PARTICLES = 40; // the amount of particles in your bridge
void draw(){
sineTimer += 0.5; // increment time since last frame
int frequency = 23; // sine frequency (larger for longer wave)
float fullCircle = (float) Math.toRadians(180);
for(int i = 0; i < PARTICLES ; i++){
float offset = i; // set base offset
offset+=sineTimer; // add timer value to offset
if(offset > fullCircle*frequency || offset < 0){
bridge.particles.get(i).body.setTransform(box2d.coordPixelsToWorld(200,i*10), 0);
}else{
float x =(float)Math.sin(offset/frequency);
bridge.particles.get(i).body.setTransform(box2d.coordPixelsToWorld((x *125) +200, i*10), 0);
}
}
}
void mousePressed() {
sineTimer = -35f; // resets timer
}
谢谢@dfour ...请参阅修改后的代码。我所遇到的最大问题之一是浪潮不会沿着机构链向下移动 –
@SebastianZeki我已经添加了一个在LibGdx框架中工作的完整示例,希望您能够将其转换为您的需求。 :) – dfour
谢谢@dfour。我已经添加了处理版本。此时此波正由下而上传递。我怎样才能改变它的方向,使其从上到下运行? –
只是要清楚:[Processing!= Java](https://meta.stackoverflow.com/questions/321127/processing-java) – Pshemo
当然这就是为什么两个标签都在。但问题可能也是一个java一个 –
如果你想自己控制身体,是否有你使用box2d的原因?如果你知道你想要的身体是什么,为什么不把它们绘制在那里而不是依靠物理引擎? –