ecountering of 2 circles, help with code
in
Programming Questions
•
9 months ago
Hi guys,
im trying to figure out, how i can create 2 equal circles hitting each other in the middle of the frame.
the following code creates 2 circles but after they encounter only one of them is moving, but i want them both mobing away from each other and hit again in the middle...
PVector pos, posA, vel, velA, velB;
int rad;
void setup()
{
size(400, 400);
pos = new PVector (width/4, height/2);
posA = new PVector (width/2, height/2);
vel = new PVector (0, 0);
velA = new PVector (-2, 0);
rad = 20;
//vel = new PVector (.1, 0);
//velA = new PVector (-1, 0);
velB = new PVector (0, 0);
}
void draw()
{
background(150);
if ( PVector.dist(pos, posA)<rad)
{
velB.add(velA);
velA.mult(0);
velA.add(vel);
vel.mult(0);
vel.add(velB);
velB.mult(0);
//momentum is preserved
//work on implementing mass into mix
}
border();
borderA();
pos.add(vel);
posA.add(velA);
ellipse(pos.x, pos.y, rad, rad);
ellipse(posA.x, posA.y, rad, rad);
}
void border()
{
if (pos.x > width)
{
vel.x = -vel.x;
}
if (pos.x < 0)
{
vel.x = -vel.x;
}
if (pos.y > height)
{
vel.y = -vel.y;
}
if (pos.y < 0)
{
vel.y = -vel.y;
}
}
void borderA()
{
if (posA.x > width)
{
velA.x = -velA.x;
}
if (posA.x < 0)
{
velA.x = -velA.x;
}
if (posA.y > height)
{
velA.y = -velA.y;
}
if (posA.y < 0)
{
velA.y = -velA.y;
}
}
may you guys can give me some hints how i can manage, that the circles just hitting in the middle and moving back to the left/right of the frame ?
just like in this picture the seccond row:
http://s7.directupload.net/file/d/3130/u54osy2g_jpg.htm
thanks for help :)
im trying to figure out, how i can create 2 equal circles hitting each other in the middle of the frame.
the following code creates 2 circles but after they encounter only one of them is moving, but i want them both mobing away from each other and hit again in the middle...
PVector pos, posA, vel, velA, velB;
int rad;
void setup()
{
size(400, 400);
pos = new PVector (width/4, height/2);
posA = new PVector (width/2, height/2);
vel = new PVector (0, 0);
velA = new PVector (-2, 0);
rad = 20;
//vel = new PVector (.1, 0);
//velA = new PVector (-1, 0);
velB = new PVector (0, 0);
}
void draw()
{
background(150);
if ( PVector.dist(pos, posA)<rad)
{
velB.add(velA);
velA.mult(0);
velA.add(vel);
vel.mult(0);
vel.add(velB);
velB.mult(0);
//momentum is preserved
//work on implementing mass into mix
}
border();
borderA();
pos.add(vel);
posA.add(velA);
ellipse(pos.x, pos.y, rad, rad);
ellipse(posA.x, posA.y, rad, rad);
}
void border()
{
if (pos.x > width)
{
vel.x = -vel.x;
}
if (pos.x < 0)
{
vel.x = -vel.x;
}
if (pos.y > height)
{
vel.y = -vel.y;
}
if (pos.y < 0)
{
vel.y = -vel.y;
}
}
void borderA()
{
if (posA.x > width)
{
velA.x = -velA.x;
}
if (posA.x < 0)
{
velA.x = -velA.x;
}
if (posA.y > height)
{
velA.y = -velA.y;
}
if (posA.y < 0)
{
velA.y = -velA.y;
}
}
may you guys can give me some hints how i can manage, that the circles just hitting in the middle and moving back to the left/right of the frame ?
just like in this picture the seccond row:
http://s7.directupload.net/file/d/3130/u54osy2g_jpg.htm
thanks for help :)
1