你好,现在你是一名processing的专家,擅长用processing实现以下美丽的视觉效果和颇具趣味的鼠标交互。现在请你参考下面的动量定理的代码: import…
- User
你好,现在你是一名processing的专家,擅长用processing实现以下美丽的视觉效果和颇具趣味的鼠标交互。现在请你参考下面的动量定理的代码: import controlP5.*; //<>//
ControlP5 cp5;
boolean iLinked = false; PVector finger_velocity;
// Variables related to moving box, 1 stand for lefg box, 2 stand for right box PVector center1, velocity1; PVector center2, velocity2; float boxHeight = 50; float speed = 3.5; float epsilon = 0; float m1 = 100; float m2 = 100;
void setup() { size(1200, 600); cp5 = new ControlP5(this);
rectMode(CENTER);
// add a horizonal slider for epsilon cp5.addSlider("slider") .setPosition(20, 20) .setSize(200, 20) .setRange(0, 1) .setValue(0.5) ;
// reposition the Label for controller 'slider' cp5.getController("slider").getValueLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0); cp5.getController("slider").getCaptionLabel().align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0); cp5.getController("slider").getCaptionLabel().setColor(#AAAAAA); cp5.getController("slider").getCaptionLabel().setText("Epsilon"); cp5.getController("slider").getValueLabel().setColor(#AAAAAA);
// add a vertical slider for masses of 2 boxes cp5.addSlider("Mass_Of_Left_Box") .setPosition(width-180, 20) .setSize(10, 100) .setRange(50, 150) .setValue(100) ; cp5.getController("Mass_Of_Left_Box").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(10); cp5.getController("Mass_Of_Left_Box").getCaptionLabel().setColor(#AAAAAA); cp5.getController("Mass_Of_Left_Box").getCaptionLabel().setText("m1"); cp5.getController("Mass_Of_Left_Box").getValueLabel().setColor(#AAAAAA);
cp5.addSlider("Mass_Of_Right_Box") .setPosition(width-80, 20) .setSize(10, 100) .setRange(50, 150) .setValue(100) ; cp5.getController("Mass_Of_Right_Box").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(10); cp5.getController("Mass_Of_Right_Box").getCaptionLabel().setColor(#AAAAAA); cp5.getController("Mass_Of_Right_Box").getCaptionLabel().setText("m2"); cp5.getController("Mass_Of_Right_Box").getValueLabel().setColor(#AAAAAA);
center1 = new PVector(m1/2, 3height/4-boxHeight/2); center2 = new PVector(3width/4-m1/2, 3*height/4-boxHeight/2);
velocity1 = new PVector(0, 0); velocity2 = new PVector(0, 0); }
void draw() { // draw background background(#F0FFF0);
// draw base fill(#6C7B8B); noStroke(); rect(width/2, 7*height/8, width, height/4);
// draw two boxes m1 = cp5.getController("Mass_Of_Left_Box").getValue();//mass of Left box m2 = cp5.getController("Mass_Of_Right_Box").getValue();//mass of Right box
//move Left Box center1.add(velocity1); center2.add(velocity2);
noStroke(); fill(199, 97, 20); rect( center2.x, center2.y, m2, boxHeight); rect( center1.x, center1.y, m1, boxHeight);
if (checkCollisionsBetween2Boxes()) { //get epsilon epsilon = cp5.getController("slider").getValue();
- Assistant
你好,根据你提供的动量守恒定律的Processing代码示例,这段代码描述了一个仿真,在仿真中,两个横向移动的盒子可以在边界或彼此之间反弹,并应用了一个控制弹性碰撞系数(ε)和两个盒子质量(m1和m2)的GUI控件。其中,重置功能('r'键)将仿真重置到初始状态,并且鼠标单击在触发区域(mouseY > 120)时可以为左边的盒子增加水平速度。
以下是完成的Processing代码,使用了ControlP5库对于控件的控制,并增加了一些鼠标交互逻辑,使用户可以通过鼠标单击增加左侧盒子的速度。
注意,以下代码假设你已经正确安装了ControlP5库,并为其正确设置了classpath。如果你遇到问题,请确保在Processing的“Import Library”菜单中包含了ControlP5库。
java// 参考代码已经包含了以下基础代码 // 你可以在此基础上进行自定义修改和添加趣味交互特性 // 例如,为了提高互动性,可以增加鼠标拖拽盒子的功能,这需要修改mousePressed()函数并新增mouseDragged()函数实现 void mouseDragged() { // 检查左盒子范围内的鼠标拖拽 if (mouseX > center1.x - m1/2 && mouseX < center1.x + m1/2 && mouseY > center1.y - boxHeight/2 && mouseY < center1.y + boxHeight/2) { center1.x = mouseX; // 设置左盒子中心为鼠标的X坐标 } // 类似地,也可以增加右盒子拖拽: if (mouseX > center2.x - m2/2 && mouseX < center2.x + m2/2 && mouseY > center2.y - boxHeight/2 && mouseY < center2.y + boxHeight/2) { center2.x = mouseX; // 设置右盒子中心为鼠标的X坐标 } }