box2d.pyi 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from linalg import vec2, vec4
  2. from typing import Iterable
  3. class _NodeLike: # duck-type protocol
  4. def on_box2d_contact_begin(self, other: 'Body'): ...
  5. def on_box2d_contact_end(self, other: 'Body'): ...
  6. def on_box2d_pre_step(self): ...
  7. def on_box2d_post_step(self): ...
  8. class _DrawLike: # duck-type protocol
  9. def draw_polygon(self, vertices: list[vec2], color: vec4): ...
  10. def draw_solid_polygon(self, vertices: list[vec2], color: vec4): ...
  11. def draw_circle(self, center: vec2, radius: float, color: vec4): ...
  12. def draw_solid_circle(self, center: vec2, radius: float, axis: vec2, color: vec4): ...
  13. def draw_segment(self, p1: vec2, p2: vec2, color: vec4): ...
  14. def draw_transform(self, position: vec2, rotation: float): ...
  15. def draw_point(self, p: vec2, size: float, color: vec4): ...
  16. class World:
  17. gravity: vec2 # gravity of the world, by default vec2(0, 0)
  18. def get_bodies(self) -> Iterable['Body']:
  19. """return all bodies in the world."""
  20. def ray_cast(self, start: vec2, end: vec2) -> list['Body']:
  21. """raycast from start to end"""
  22. def box_cast(self, lower: vec2, upper: vec2) -> list['Body']:
  23. """query bodies in the AABB region."""
  24. def point_cast(self, point: vec2) -> list['Body']:
  25. """query bodies that contain the point."""
  26. def step(self, dt: float, velocity_iterations: int, position_iterations: int) -> None:
  27. """step the simulation, e.g. world.step(1/60, 8, 3)"""
  28. # enum
  29. # {
  30. # e_shapeBit = 0x0001, ///< draw shapes
  31. # e_jointBit = 0x0002, ///< draw joint connections
  32. # e_aabbBit = 0x0004, ///< draw axis aligned bounding boxes
  33. # e_pairBit = 0x0008, ///< draw broad-phase pairs
  34. # e_centerOfMassBit = 0x0010 ///< draw center of mass frame
  35. # };
  36. def debug_draw(self, flags: int):
  37. """draw debug shapes of all bodies in the world."""
  38. def set_debug_draw(self, draw: _DrawLike):
  39. """set the debug draw object."""
  40. def create_weld_joint(self, body_a: 'Body', body_b: 'Body'):
  41. """create a weld joint between two bodies."""
  42. class Body:
  43. type: int # 0-static, 1-kinematic, 2-dynamic, by default 2
  44. gravity_scale: float
  45. fixed_rotation: bool
  46. enabled: bool
  47. bullet: bool # whether to use continuous collision detection
  48. @property
  49. def mass(self) -> float: ...
  50. @property
  51. def inertia(self) -> float: ...
  52. position: vec2
  53. rotation: float # in radians (counter-clockwise)
  54. velocity: vec2 # linear velocity
  55. angular_velocity: float
  56. damping: float # linear damping
  57. angular_damping: float
  58. # fixture settings
  59. density: float
  60. friction: float
  61. restitution: float
  62. restitution_threshold: float
  63. is_sensor: bool
  64. def __new__(cls, world: World, node: _NodeLike = None):
  65. """create a body in the world."""
  66. def set_box_shape(self, hx: float, hy: float): ...
  67. def set_circle_shape(self, radius: float): ...
  68. def set_polygon_shape(self, points: list[vec2]): ...
  69. def set_chain_shape(self, points: list[vec2]): ...
  70. def apply_force(self, force: vec2, point: vec2): ...
  71. def apply_force_to_center(self, force: vec2): ...
  72. def apply_torque(self, torque: float): ...
  73. def apply_impulse(self, impulse: vec2, point: vec2): ...
  74. def apply_impulse_to_center(self, impulse: vec2): ...
  75. def apply_angular_impulse(self, impulse: float): ...
  76. def get_node(self) -> _NodeLike:
  77. """return the node that is attached to this body."""
  78. def get_contacts(self) -> list['Body']:
  79. """return all bodies that are in contact with this body."""
  80. def destroy(self):
  81. """destroy this body."""