Java:设置mousedrag函数可以移动对象的距离上限
我现在有一个锤子,它通过mousedrag函数在枢轴上旋转,每当它与范围内的两个球对象中的一个接触时,就会触发音效。然而,我希望当锤子接触到其中一个圆圈时,锤子不能移动超过接触点,但我很难做到这一点。任何帮助都将不胜感激Java:设置mousedrag函数可以移动对象的距离上限,java,animation,processing,draggable,Java,Animation,Processing,Draggable,我现在有一个锤子,它通过mousedrag函数在枢轴上旋转,每当它与范围内的两个球对象中的一个接触时,就会触发音效。然而,我希望当锤子接触到其中一个圆圈时,锤子不能移动超过接触点,但我很难做到这一点。任何帮助都将不胜感激 import oscP5.*; import netP5.*; OscP5 oscP5; NetAddress myRemoteLocation; float bx; float by; int boxSizeX = 160; int boxSizeY = 30; bool
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);
}
}
导入oscP5.*;
导入netP5.*;
OscP5-OscP5;
NetAddress myRemoteLocation;
浮动bx;
飘过;
int-boxSizeX=160;
int-boxSizeY=30;
布尔overBox=true;
布尔锁定=假;
浮动xOffset=0.0;
浮动yOffset=0.0;
浮动角度=4.70;
BeatBall b1=新BeatBall(210425,60,1);
BeatBall b2=新BeatBall(570395,60,2);
锤子h=新锤子(135,-67,boxSizeY+25,boxSizeX-25,1);
无效设置()
{
尺寸(800600);
光滑的();
帧率(120);
bx=宽度/2.0;
by=高度/2.0;
oscP5=新的oscP5(本,12001);
/*myRemoteLocation是一个NetAddress。NetAddress包含2个参数,
*ip地址和端口号。myRemoteLocation用作中的参数
*oscP5.send()向另一台计算机、设备发送osc数据包时,
*应用程序。用法见下文。出于测试目的,侦听端口
*和远程位置地址的端口相同,因此您将
*将消息发送回此草图。
*/
myRemoteLocation=新的NetAddress(“127.0.0.1”,12000);
}
作废提款()
{
背景(0);
pushMatrix();
翻译(400425);
旋转(角度);
填充(2223255);
h、 显示锤();
rect(-25,-15,boxSizeX,boxSizeY);
popMatrix();
b1.displayBall();
b2.displaySquare();
//测试
如果(角度<-2.6075916和角度>-2.613132)
{
锁定=真;
b1.碰撞();
}
}
void mousePressed()
{
xOffset=mouseX bx;
yOffset=鼠标指针;
}
void mouseDragged()
{
bx=鼠标X偏移量;
by=mouseY yOffset;
角度=atan2(mouseY-400,mouseX-400);
println(角度);
}
//甲壳虫类
职业棒球{
浮动x,y;
浮子直径;
浮动vx=0;
浮动vy=0;
int-id;
BeatBall(浮心、浮阴、浮丁、浮丁){
x=xin;
y=阴;
直径=din;
id=idin;
}
无效碰撞()
{
/*
//对象的变量-它们在哪里,有多大?
浮球,浮球;
浮球半径;
浮锤X,锤状;
浮锤半径;
//使用勾股定理计算对象之间的距离
float xDist=hammerX-ballX;
浮球yDist=hammerY-ballY;
float dist=sqrt((xDist*xDist)+(yDist*yDist));
if(距离<球半径+锤半径)
{
//碰撞!
背景(120);
}
其他的
{
//没有碰撞!
}
*/
OscMessage myMessage=新的OscMessage(“/bubble”);
打印(直径+“”);
myMessage.add(1/(直径*直径)*1000000);/*在osc消息中添加一个int*/
/*发送消息*/
oscP5.send(myMessage,myRemoteLocation);
// }
}
void displayBall()
{
填充(191,89,0);
椭圆(x,y,直径,直径);
}
void displaySquare()
{
填充(191,89,0);
rect(x,y,直径,直径);
}
}
//锤子类
类锤{
浮动x,y;
浮子大小,大小;
浮动vx=0;
浮动vy=0;
int-id;
锤子(浮心、浮阴、浮心、浮心、浮心、浮心){
x=xin;
y=阴;
sizeX=sxin;
sizeY=syin;
id=idin;
}
空锤()
{
填充(2223255);
rect(x,y,sizeX,sizeY);
}
}
通过使用处理api提供的Constraint()方法,我自己找到了一个解决方案