0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ angle is relative to elapsed_time not to 'deltatime' so try : player_x = Math.sin(angle); (instead of +=) \$\endgroup\$ – dnk drone.vs.drones Jul 27 '15 at 11:04
  • \$\begingroup\$ tried player_x = Math.sin(angle); and player doesn't move at all \$\endgroup\$ – Droid Jul 27 '15 at 11:12
  • \$\begingroup\$ put some debug log. Ispect angle0, T, angle \$\endgroup\$ – dnk drone.vs.drones Jul 27 '15 at 11:21
  • \$\begingroup\$ log added, forgot about angle0, but it's around 1.1(to degrees(60-65)) and rope length is 400+ \$\endgroup\$ – Droid Jul 27 '15 at 11:32
  • \$\begingroup\$ 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) \$\endgroup\$ – dnk drone.vs.drones Jul 27 '15 at 12:23
2
\$\begingroup\$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.