我目前有一个锤子,可以在鼠标拖动功能的枢轴上旋转,每当它接触到其范围内的两个球对象中的一个时,就会触发声音效果。然而,我希望当锤子接触到这些圆圈中的一个时,它不能移动通过接触点-但我在实现这一点上遇到了麻烦。任何帮助都将不胜感激。
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);
}
}发布于 2014-12-16 03:32:34
通过使用processing api提供的constrain()方法,我自己找到了一个解决方案。
https://stackoverflow.com/questions/27485690
复制相似问题