3

我正在尝试查看是否可以使用 oscP5 将处理中制作的草图链接到蚱蜢。该草图是该编码训练视频前半部分中概述的点球体:

https://www.youtube.com/watch?v=RkuBWEkBrZA

在我开始将其与 oscP5 链接之前的代码似乎工作正常:

import peasy.*;

PeasyCam cam;

PVector[][] globe;
int total = 20;

void setup() {
  size(600, 600, P3D);
  cam = new PeasyCam(this, 500);

  globe = new PVector[total+1][total+1];
}

void draw() {
  background(0);
  fill(255);
  lights();

  float r = 200;

  for (int i = 0; i < total+1; i++) {
    float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
      float lon = map(j, 0, total, 0, TWO_PI);
      float x = r * sin(lat) * cos(lon);
      float y = r * sin(lat) * sin(lon);
      float z = r * cos(lat);   
      globe[i][j] = new PVector(x, y, z);        
    }
  }
  noFill();   
  for (int i = 0; i < total; i++) {
    beginShape(TRIANGLE_STRIP); 
    for (int j = 0; j < total+1; j++) {
      PVector v1 = globe[i][j];
      stroke(255);
      strokeWeight(2);
      vertex(v1.x, v1.y, v1.z);   

      PVector v2 = globe[i+1][j];
      vertex(v2.x, v2.y, v2.z); 
    }
    endShape();
  }
}

但是,当我尝试实现 oscP5 时,它并不能很好地工作。我的蚱蜢文件正在接收草图,但是点不正确,所以我必须对我发送的数据有问题,但我似乎无法弄清楚我应该发送什么。该代码采用指定的半径、经度和纬度的值并将其转换为 x、y、z 坐标,我试图让 oscP5 发送这些 x、y、z 坐标。

有没有人有任何想法?到目前为止的代码如下。

//import necessary libraries
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

//import camera
import peasy.*;

PeasyCam cam;

// message to send
String message;

PVector[][] sphere;
int total = 20;

float lat;
float lon;

void setup() {
  size(600, 600, P3D);
  cam = new PeasyCam(this, 500);

  sphere = new PVector[total+1][total+1];

//send message from this port
  oscP5 = new OscP5(this,12000);      
//send message to this port
  myRemoteLocation = new NetAddress("127.0.0.1",12001);
}

void draw() {
  background(0);
  fill(255);
  lights();

  float r = 200;

  for (int i = 0; i < total+1; i++) {
    float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
      float lon = map(j, 0, total, 0, TWO_PI);
      float x = r * sin(lat) * cos(lon);
      float y = r * sin(lat) * sin(lon);
      float z = r * cos(lat);   
      sphere[i][j] = new PVector(x, y, z);        
    }
  }
  noFill();    
  for (int i = 0; i < total; i++) {
    beginShape(TRIANGLE_STRIP); 
    for (int j = 0; j < total+1; j++) {
      PVector v1 = sphere[i][j];
      stroke(255);
      strokeWeight(2);
      vertex(v1.x, v1.y, v1.z);   

      PVector v2 = sphere[i+1][j];
      vertex(v2.x, v2.y, v2.z); 
    }
    endShape();
  }

// osc message
  OscMessage myMessage = new OscMessage("/hello world");


for (int j = 0; j < total+1; j++) {
//message to send 
message  = "x = " + String.valueOf(r * sin(lat) * cos(lon)) + "; " + 
           "y = " + String.valueOf(r * sin(lat) * sin(lon))+ "; " +
           "z = " + String.valueOf(r * cos(lat))+ "; " ;

    myMessage.add(String.valueOf(r * sin(lat) * cos(lon)));
    myMessage.add(String.valueOf(r * sin(lat) * sin(lon))); 
    myMessage.add(String.valueOf(r * cos(lat))); 
  }

//print message
  println(message);
//send message
  oscP5.send(myMessage, myRemoteLocation);
}

我相信错误出现在这部分代码中

for (int j = 0; j < total+1; j++) {
//message to send 
message  = "x = " + String.valueOf(r * sin(lat) * cos(lon)) + "; " + 
           "y = " + String.valueOf(r * sin(lat) * sin(lon))+ "; " +
           "z = " + String.valueOf(r * cos(lat))+ "; " ;

    myMessage.add(String.valueOf(r * sin(lat) * cos(lon)));
    myMessage.add(String.valueOf(r * sin(lat) * sin(lon))); 
    myMessage.add(String.valueOf(r * cos(lat))); 
  }

任何帮助都会很棒。

4

1 回答 1

2

全局变量的值float lat;永远不会float lon;设置,因为在嵌套for循环中,声明了 2 个新的局部变量(在循环块范围内):

for (int i = 0; i < total+1; i++) {
float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
        float lon = map(j, 0, total, 0, TWO_PI);

        // [...]
    }
}

注意,float lat = ...声明一个新变量,lat = ...将是对现有变量的赋值lat

但是使用全局变量而不是设置新变量不会改变任何东西,因为当消息发送时,变量latlon将具有嵌套循环的最后一次迭代的值。

您必须实现 2 个新循环,并且必须重新计算 和 的latlon。循环之后的 print 语句没有任何意义,因为它只会打印最后一个坐标。

代码应该看起来像这样:

// osc message
OscMessage myMessage = new OscMessage("/hello world");

for (int i = 0; i < total+1; i++) {
    float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
        float lon = map(j, 0, total, 0, TWO_PI);

        float x = r * sin(lat) * cos(lon);
        float y = r * sin(lat) * sin(lon);
        float z = r * cos(lat);
        println("(", i, ",", j, ") : x =" , x, "y = ", y, "z = ", z);

        myMessage.add(String.valueOf(x));
        myMessage.add(String.valueOf(y)); 
        myMessage.add(String.valueOf(z)); 
    }
}

//send message
oscP5.send(myMessage, myRemoteLocation);
于 2018-12-31T11:05:09.430 回答