# android canvas rope swing

I'm creating the code, where a player swinging on a rope

for the moment I got this

double angle = Math.atan2(player_y - center_y, player_x - center_x) - Math.atan2(end_y - center_y, end_x - center_x);
if (direction == 'right') {
player_x += 10;
player_y += 10 * angle;
} else {
player_x -= 10;
player_y -= 10 * angle;
}


I know that I have to add gravity, speed, etc... but I'm not very good in physics. The question is How do I make rope swinging more realistic?

I don't use any engines or frameworks, only native canvas.

EDIT

Tried this code.

onStart...
angle0 = Math.atan2(player_y - center_y, player_x - center_x) - Math.atan2(end_y - center_y, end_x - center_x);
length = Math.hypot(center_x - player_x, center_y - player_y);
swing_start = System.currentTimeMillis();
T = 2 * Math.PI * Math.sqrt(length / 9.8f);

onDraw...
elapsed_time = (System.currentTimeMillis() - swing_start) / 1000;

#with this player makes a very slow circle with the wrong pivot point
player_x += Math.sin((2 * Math.PI / T) * elapsed_time);
player_y += Math.cos((2 * Math.PI / T) * elapsed_time);

#with this it makes backwards circle
double angle = angle0 * Math.cos((2 * Math.PI / T) * elapsed_time);
player_x += Math.sin(angle)
player_y += Math.cos(angle);


Where am I wrong?

onDraw called every 25ms

Log... start - 15:31:22.411

﹕ T: 39.83959786319781 angle: 1.1898202456306484
+15 the same
﹕ T: 39.83959786319781 angle: 1.175053634797472
+18
﹕ T: 39.83959786319781 angle: 1.13112033295045
+18
﹕ T: 39.83959786319781 angle: 1.059110834176187
+17
﹕ T: 39.83959786319781 angle: 0.9608125282061935
+17
﹕ T: 39.83959786319781 angle: 0.9608125282061935
+17
﹕ T: 39.83959786319781 angle: 0.6957011399544965
+17


end - 15:31:29.361

• angle is relative to elapsed_time not to 'deltatime' so try : player_x = Math.sin(angle); (instead of +=) – dnk drone.vs.drones Jul 27 '15 at 11:04
• tried player_x = Math.sin(angle); and player doesn't move at all – Droid Jul 27 '15 at 11:12
• put some debug log. Ispect angle0, T, angle – dnk drone.vs.drones Jul 27 '15 at 11:21
• log added, forgot about angle0, but it's around 1.1(to degrees(60-65)) and rope length is 400+ – Droid Jul 27 '15 at 11:32
• I don't remind all the trigonometry but I expect something like player_x = Math.sin(angle) * length; (keepeng the origin in 0,0) (sin and cos returns a -1..1 value) – dnk drone.vs.drones Jul 27 '15 at 12:23

You can use pendulum physic to approximate your rope swing.

Calculate the fluctuation period (this is true for small fluctuations)

T = 2*PI*SQRT(L/g)


where:

PI = 3.14


g = 9.8 (if I remember) the earth gravity acceleration L : the rope length, or better the length from rotation fulcrum to the character hands.

Then you get the fluctuation angle as function of time t as :

angle(t) = angle0 * COS((2*PI/T)*t)


where angle0 is the starting amptitude

• thank you very much, going to try it, but what small "t" is? – Droid Jul 27 '15 at 9:51
• t is the time (in seconds) from the start of your simulation. Or from the start of rope swing – dnk drone.vs.drones Jul 27 '15 at 10:07