Swing along the tangent.

Hello,
I am am stuck with my java code. Need help. I have been trying to create a swing movement for an animated java ball. I have managed to get the ball moving in a circular manner. With this:

 theta = theta + Math.toRadians(10); 
x = a + r*Math.cos(theta);
y = b + r*Math.sin(theta) ;

This just sets the ball in circular motion. I am wanting to swing the ball from particular point in a motion of a tangent to the circle as given in the image figure below. Could anyone please help me with this?

You can find the slope of that tangent line by using the derivative of a circle: dy/dx = -x/y. You plug in (ballX - circleCenterX) for x, and (ballY - circleCenterY) for y to find the slope(m). You then set the deltaX and deltaY values for the ball using some trig: deltaX = speed/sqrt(m^2 + 1), and deltaY = deltaX * m (You will sometimes have to negate the direction based on whether you are above or below the center of the circle, and for clockwise/counter-clockwise rotation).

Ugh ok so look at a sine curve
BOINK:

http://upload.wikimedia.org/wikipedia/commons/0/02/Simple_sine_wave.svg

have a single value that is equal to the current tick and then use this to calculate how far to rotate the ball each time ie movement = Math.sin(Math.toRadians(tick_count)); this will generate a nice curve value that will swing , simply apply the rotation to the end point about the central point , this will make the swing go up , if you want to start your swing from the central position where velocity is at its maximum then perform Math.cos(Math.toRadians(tick_count));. If you are wondering why its because sin(0) is 0 hence starting on the lowest velocity and cos(0) is 1.

Hope this helped

That was quite close to satisfaction although quite complicated for me. Anyway, I think that solved my problem. Thank you for that wonderful explaination @Icass @Longarmx :slight_smile: