We are about to switch to a new forum software. Until then we have removed the registration on this forum.
First of all, sorry if this not the right place to ask a question about math, but i didn't find any other place to put it.
as you can see in the code bellow, i have two circles, a white and a black one. What i want is for the black one to go in the white one's point. I know that this is a very complicated way to do it, but i this it should still work.
What actually happens is the black circle will go around the white one and, sometimes it will stop in the right place and other times it will disappear out of the screen.
float whiteX = random(100, 500);
float whiteY = random(100, 500);
float blackX = random(100, 500);
float blackY = random(100, 500);
float distanceX;
float distanceY;
float distance;
void setup()
{
size(600, 600);
frameRate(1);
}
void draw()
{
background(128);
WhiteCircle(whiteX, whiteY);
BlackCircle(blackX, blackY);
if(whiteX != blackX && whiteY != blackY)
{
Distance();
float angle = Angle();
// calculating the next position
if(blackX < blackY)
blackX += sin(angle)*distance;
else
blackX -= sin(angle)*distance;
if(whiteX < whiteY)
blackY += cos(angle)*distance;
else
blackY -= cos(angle)*distance;
}
}
void WhiteCircle(float X, float Y)
{
int radious = 50;
fill(256);
ellipse(X, Y, radious, radious);
}
void BlackCircle(float X, float Y)
{
int radious = 50;
fill(0);
ellipse(X, Y, radious, radious);
}
void Distance()
{
// calculating the distance between the two circles
distanceX = blackX - whiteX;
distanceY = blackY - whiteY;
distance = sqrt(sq(distanceX) + sq(distanceY));
}
float Angle()
{
// calculating the angle of the two circles
float angle;
angle = distanceY/distance;
angle = asin(angle);
angle = degrees(angle);
//print(angle);
return angle;
}
Answers
There are at least two problems.
You calculate the asin of a variable so you get an angle. Then you convert it to degrees then take the sin of that value. But the sin function works with radians! Remove line 67 at the least.
You calculate the asin in Angle() then calculate the sin of that value... that's not the best approach.
You don't need to use sin and cos at all actually, there's a way which just uses the x and y coordinates. Look at this post:
http://forum.processing.org/two/discussion/comment/35290/#Comment_35290
Thanks for the reply, i thought of the fact that maybe the sin and cos functions use radians, but when i removed the line that converts to degrees, the black circle disappeared from the screen on the second frame.
I get what you' re sayng about the Angle() function, but i prefer to get the actuall angle out of it.
Anyway, i'll try your suggestion, it seems promising.
When I did that, the black circle jumped straight to the white circle's position. Is that what you wanted?
Yes, why am i getting a different result? I just did it again a few times, but it only jumbed in the white circle once, and it was in the third frame, not the second.
are you sure you didn't change anything else?
Just added these two lines at the end of draw() but apart from that...
Well, i doesn' t work for me, but i' ll try your suggestion about using vectors, it seems easier to work with. Thanks alot.
Definately. Literally the only thing you need to do is subtract the coordinates from each other (whiteX - blackX and whiteY - blackY) and you get the difference vector (distanceX and distanceY in your code). This you can add to the black coordinates and you end up at white's position... that's pretty trivial of course. If you then want the black circle to take more frames to reach white you could add only a fraction of the difference vector each frame (like
0.2*distanceX
and0.2*distanceY
). If you calculate the difference vector on each frame, the black circle will slow down as it reaches white, but if you calculate it only once it will go straight to white (your preference).