test_force_anim.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # @ Author: Giovanni Dalmasso
  2. # @ Create Time: 09-02-2024 17:02:07
  3. # @ Modified by: M. Musy
  4. import numpy as np
  5. from vedo import Arrow2D, Latex, Line, Mesh, Plotter, Text3D, dataurl
  6. # Define the forces and initial angle
  7. FB = 800 # Newton
  8. FA = 750 # Newton
  9. theta = 48 # Initial angle in degrees
  10. # Calculate the initial components of FA and Fx
  11. theta_rad = np.radians(theta)
  12. FA_x = FA * np.sin(theta_rad)
  13. FA_y = FA * np.cos(theta_rad)
  14. Fx = FB * np.cos(np.radians(30)) + FA * np.sin(theta_rad)
  15. # Initialize the text object for Fx with its initial value
  16. arrow_Fx = Arrow2D([0, 0, 0], (Fx, 0, 0), c="green4")
  17. label_Fx = Text3D(f"F_x : {Fx:.0f}~N", pos=(Fx, 0, 0), c="green4", s=80)
  18. # Create arrows for forces
  19. arrow_FA = Arrow2D([0, 0, 0], (FA_x, FA_y, 0), c="red4")
  20. label_FA = Text3D(f"F_A : {FA:.0f}~N", (FA_x, FA_y, 0), c="red4", s=80)
  21. arrow_FB = Arrow2D(
  22. [0, 0, 0],
  23. [FB * np.cos(np.radians(30)), -FB * np.sin(np.radians(30)), 0],
  24. c="blue4",
  25. )
  26. arrow_FB.name = "ArrowFixed"
  27. label_FB = Text3D(
  28. f"F_B : {FB:.0f}~N",
  29. pos=(FB * np.cos(np.radians(30)), -FB * np.sin(np.radians(30)), 0),
  30. c="blue4",
  31. s=80,
  32. )
  33. label_FB.name = "LabelFixed"
  34. # Vertical line to represent the reference direction for theta
  35. vertical_line = Line([0, 0, 0], [0, FA_y, 0], c="black", lw=10)
  36. theta_txt = Latex(r"\vartheta", s=400).pos([0, FA_y / 3, 0])
  37. deg = Latex(r"30^\circ", s=400).pos([400, -300])
  38. gio = Text3D("Giovanni D.", s=50, c="blue4", italic=True).pos(-600, -300, 0)
  39. car = Mesh(dataurl + "porsche.ply").c("k8").lighting("metallic").phong()
  40. car.scale(50).pos(-300, 0, 0).rotate(90)
  41. def update_scene(widget, event):
  42. """Update the forces FA and Fx based on the angle theta."""
  43. # Recalculate components of FA and Fx
  44. theta = np.radians(widget.value)
  45. FA_x, FA_y = FA * np.sin(theta), FA * np.cos(theta)
  46. Fx = FB * np.cos(np.radians(30)) + FA * np.sin(theta)
  47. arrow_FA = Arrow2D([0, 0, 0], (FA_x, FA_y, 0), c="red4").z(0.1)
  48. arrow_Fx = Arrow2D([0, 0, 0], (Fx, 0, 0), c="green4")
  49. label_FA.pos(FA_x, FA_y, 0.1).text(f"F_A : {FA:.0f}~N")
  50. label_Fx.pos(Fx, 0, 0).text(f"F_x : {Fx:.0f}~N")
  51. plt.remove("Arrow2D").add([arrow_FA, arrow_Fx])
  52. # Create the plotter and a slider to adjust the angle theta
  53. plt = Plotter(bg2="lightblue", size=(1200, 800))
  54. plt.add(car, vertical_line, theta_txt, deg, gio)
  55. plt.add(arrow_FA, arrow_FB, arrow_Fx, label_FA, label_FB, label_Fx)
  56. plt.add_slider(update_scene, 0, 90, value=theta, title="Theta Angle")
  57. plt.show(zoom=1.3).close()