我正在开发一个简单的处理应用程序,每当锤子击中一个不动的形状时,它就会产生声音效果。但是,当锤子对象与shape对象发生冲突时,我在尝试让代码检测时遇到了困难,并且已经开始求助于不专业的解决方法,正如在“测试”下注释的代码块中所看到的那样。如果能为这个问题找到解决方案,我们将不胜感激。
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);
}
}发布于 2014-12-14 22:54:19
我已经建立了可能有用的一套用于处理的碰撞检测功能。
如果你能简化事物,把物体看作圆圈,你就可以用毕达哥拉斯定理来检查它们的距离。(按要求更新为函数。)
// 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;
}https://stackoverflow.com/questions/27471063
复制相似问题