【问题标题】:Java: Setting cap on how far mousedrag function can move objectJava:设置鼠标拖动功能可以移动对象的距离
【发布时间】:2014-12-15 13:56:59
【问题描述】:

我目前有一个锤子,它通过鼠标拖动功能在枢轴上旋转,只要它与范围内的两个球对象之一接触,就会触发声音效果。但是,我想让锤子在碰到这些圆圈之一时无法移过接触点 - 但我无法做到这一点。任何帮助将不胜感激。

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

float bx;
float by;
int boxSizeX = 160;
int boxSizeY = 30;
boolean overBox = true;
boolean locked = false;
float xOffset = 0.0; 
float yOffset = 0.0; 
float angle = 4.70;

BeatBall b1 = new BeatBall(210,425,60, 1);
BeatBall b2 = new BeatBall(570,395,60, 2);
Hammer h = new Hammer(135, -67,  boxSizeY+25, boxSizeX-25, 1);

void setup()
{
  size(800, 600);
  smooth();
  frameRate(120);
  bx = width/2.0;
  by = height/2.0;

    oscP5 = new OscP5(this,12001);

  /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. usage see below. for testing purposes the listening port
   * and the port of the remote location address are the same, hence you will
   * send messages back to this sketch.
   */
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
}

void draw()
{ 
  background(0);

  pushMatrix();
  translate(400, 425);
  rotate(angle);
  fill(222,223,255);
  h.displayHammer();
  rect(-25, -15, boxSizeX, boxSizeY);
  popMatrix();

  b1.displayBall();

  b2.displaySquare();


   //Testing
  if(angle < -2.6075916 && angle > -2.613132)
   {
      locked = true;
      b1.collide();

   }
}



void mousePressed() 
{
  xOffset = mouseX-bx; 
  yOffset = mouseY-by; 
}

void mouseDragged()
{
    bx = mouseX-xOffset; 
    by = mouseY-yOffset;  
   angle = atan2(mouseY - 400, mouseX - 400);
   println(angle);
}

//BEATBALL CLASS

class BeatBall {
  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;

  BeatBall(float xin, float yin, float din, int idin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
  } 

  void collide() 
  {
    /*
    // variables for your objects - where are they and how big?
    float ballX, ballY;
    float ballRadius;
    float hammerX, hammerY;
    float hammerRadius;

    // calculate distance between the objects using the Pythagorean Theorem
    float xDist = hammerX - ballX;
    float yDist = hammerY - ballY;
    float dist = sqrt( (xDist*xDist) + (yDist*yDist) );

    if (dist < ballRadius + hammerRadius)
    {
      // collision!
      background(120);
    }
    else
    {
  // no collision!
    }
    */

      OscMessage myMessage = new OscMessage("/bubble");
      print(diameter + " ");
      myMessage.add( 1/(diameter*diameter) * 1000000); /* add an int to the osc message */

      /* send the message */
     oscP5.send(myMessage, myRemoteLocation);  
    // }
  }

  void displayBall() 
  {
    fill(191,89,0);
    ellipse(x, y, diameter, diameter);   
  }

  void displaySquare() 
  {
    fill(191,89,0);
    rect(x, y, diameter, diameter);   
  }

}

//HAMMER CLASS

class Hammer {
  float x, y;
  float sizeX, sizeY;
  float vx = 0;
  float vy = 0;
  int id;

  Hammer(float xin, float yin, float sxin, float syin, int idin) {
    x = xin;
    y = yin;
    sizeX = sxin;
    sizeY = syin;
    id = idin;
  } 

   void displayHammer() 
  {
    fill(222,223,255);
    rect(x, y, sizeX, sizeY); 
  }


}

【问题讨论】:

    标签: java animation processing draggable


    【解决方案1】:

    通过使用处理api提供的constrain()方法自己找到了解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多