We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Collision syntax
Page Index Toggle Pages: 1
Collision syntax (Read 376 times)
Collision syntax
Mar 30th, 2006, 12:40pm
 
I am now looking to create a cascade effect of balls hitting off one another, this is the final piece of my program, but i am looking for an adjustment to my collision detection method below so that when a collision is detected the ball moves from a stationary position to go somewhere else depending on how much speed there is hitting the ball and the ball emitting the force moves slower and in the direction it should when hitting another ball
Code:

void collide()
{
ballHit = 0;
correctBall = false;

for (int i = 0; i <= 15; i++)
{
for (int j = 0; j <= 15; j++)
{
if ( i != j )
{
distX = b[j].getXPos() - b[i].getXPos();
distY = b[j].getYPos() - b[i].getYPos();
d = dist(b[j].getXPos(), b[j].getYPos(), b[i].getXPos(), b[i].getYPos());

if ( d < 30)
{
if (i == 15)
{
ballHit++;
//doesnt need to be used in this problem
//correctBall = checkHit(ballHit, j);
theta = atan2(distY, distX);
//set the direction of the X and Y axis depending on where the ball was hit (theta)
b[i].directionX = -1 * cos(theta);
b[i].directionY = -1 * sin(theta);
//my attempts at the problem
//b[j].setSpeed(b[i].getSpeed() / 2);
//b[i].setSpeed(b[i].getSpeed() / 2);
}
}
}
}
}
}
Re: Collision syntax
Reply #1 - Mar 30th, 2006, 8:39pm
 
A revision of the code here, I have the balls moving after they are hit but they keep overlapping each other now and never slow down (I have a getSpeed = getSpeed -2 in the main method when the speed isnt 0)

Code:

void collide()
{
ballHit = 0;
correctBall = false;

for (int i = 0; i <= 15; i++)
{
for (int j = 0; j <= 15; j++)
{
if ( i != j )
{
distX = b[j].getXPos() - b[i].getXPos();
distY = b[j].getYPos() - b[i].getYPos();
d = dist(b[j].getXPos(), b[j].getYPos(), b[i].getXPos(), b[i].getYPos());

if ( d < 30)
{
if (i == 15)
{
ballHit++;
correctBall = checkHit(ballHit, j);
}
theta = atan2(distY, distX);
if ((b[i].getSpeedX() > b[j].getSpeedX()) || (b[i].getSpeedY() > b[j].getSpeedY()))
{
b[i].setDirectionX(-1 * cos(theta));
b[i].setDirectionY(-1 * sin(theta));
}
else
{
b[j].setDirectionX(-1 * cos(theta));
b[j].setDirectionY(-1 * sin(theta));
}
b[i].setSpeedX ((b[i].getSpeedX() + b[j].getSpeedX()) / 2);
b[i].setSpeedY ((b[i].getSpeedY() + b[j].getSpeedX()) / 2);
}
}
}
}
}
Page Index Toggle Pages: 1