我正致力于开发一个简单的处理应用程序,只要锤子碰到不动的形状,就会产生声音效果。但是,每当锤子对象与形状对象发生碰撞时,我都难以尝试检测代码,并且已开始采用非专业的解决方法,如在“测试”下评论的代码块中所示。任何有关创建此问题解决方案的帮助都将受到大力赞赏
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);
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);
Hammer h = new Hammer(135, -67, boxSizeY+25, boxSizeX-25, 1);
h.displayHammer();
rect(-25, -15, boxSizeX, boxSizeY);
popMatrix();
b1.displayBall();
//Testing
if(angle < -2.6561418 && angle > -3.043227)
{
background(120);
b1.collide();
}
println(angle);
}
void mousePressed()
{
xOffset = mouseX-bx;
yOffset = mouseY-by;
}
void mouseDragged()
{
bx = mouseX-xOffset;
by = mouseY-yOffset;
angle = atan2(mouseY - 400, mouseX - 400);
}
//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()
{
/* Collision Example
float dx = Hammer.x - x;
float dy = Hammer.y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist)
{
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
*/
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);
}
}
//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);
}
}
答案 0 :(得分:2)
我已经构建了a suite of collision detection functions for Processing可能会有所帮助。
如果您可以简化事物并将对象视为圆圈,则可以使用毕达哥拉斯定理来检查它们的距离。 (根据要求更新为函数。)
// variables for your objects - where are they and how big?
float ballX, ballY;
float ballRadius;
float hammerX, hammerY;
float hammerRadius;
void setup() {
// check for a collision
boolean hit = ballBallCollision(ballX, ballY, ballRadius, hammerX, hammerY, hammerRadius);
if (hit) {
// hit!
}
else {
// not :(
}
}
// a function to check for ball-ball collision
boolean ballBallCollision(float ballX, float ballY, float ballRadius, float hammerX, float hammerY, float hammerRadius) {
// calculate distance between the objects using the Pythagorean Theorem
float xDist = hammerX - ballX;
float xDist = hammerY - ballY;
float dist = sqrt( (xDist*xDist) + (yDist*yDist) );
if (dist < ballRadius + hammerRadius) {
return true;
}
return false;
}