Reputation: 77
I'm working on developing a simple Processing application that creates a sound effect whenever a hammer hits an non-moving shape. However, I'm having difficulties attempting to have the code detect whenever the hammer object collides with the shape object, and have begun resorting to unprofessional workarounds as seen in the code block commented under 'testing'. Any help on creating a solution to this problem would be massively appreciated
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);
}
}
Upvotes: 1
Views: 236
Reputation: 1600
I've built a suite of collision detection functions for Processing that might help.
If you can simplify things and think of the objects as circles, you can use the Pythagorean Theorem to check their distance. (Updated to a function, as requested.)
// 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;
}
Upvotes: 2