box2d.pyi 3.4 KB

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