box2d.pyi 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 step(self, dt: float, velocity_iterations: int, position_iterations: int) -> None:
  25. """step the simulation, e.g. world.step(1/60, 8, 3)"""
  26. # enum
  27. # {
  28. # e_shapeBit = 0x0001, ///< draw shapes
  29. # e_jointBit = 0x0002, ///< draw joint connections
  30. # e_aabbBit = 0x0004, ///< draw axis aligned bounding boxes
  31. # e_pairBit = 0x0008, ///< draw broad-phase pairs
  32. # e_centerOfMassBit = 0x0010 ///< draw center of mass frame
  33. # };
  34. def debug_draw(self, flags: int):
  35. """draw debug shapes of all bodies in the world."""
  36. def set_debug_draw(self, draw: _DrawLike):
  37. """set the debug draw object."""
  38. def create_weld_joint(self, body_a: 'Body', body_b: 'Body'):
  39. """create a weld joint between two bodies."""
  40. class Body:
  41. type: int # 0-static, 1-kinematic, 2-dynamic, by default 2
  42. gravity_scale: float
  43. fixed_rotation: bool
  44. enabled: bool
  45. bullet: bool # whether to use continuous collision detection
  46. @property
  47. def mass(self) -> float: ...
  48. @property
  49. def inertia(self) -> float: ...
  50. position: vec2
  51. rotation: float # in radians (counter-clockwise)
  52. velocity: vec2 # linear velocity
  53. angular_velocity: float
  54. damping: float # linear damping
  55. angular_damping: float
  56. # fixture settings
  57. density: float
  58. friction: float
  59. restitution: float
  60. restitution_threshold: float
  61. is_sensor: bool
  62. def __new__(cls, world: World, node: _NodeLike = None):
  63. """create a body in the world."""
  64. def set_box_shape(self, hx: float, hy: float): ...
  65. def set_circle_shape(self, radius: float): ...
  66. def set_polygon_shape(self, points: list[vec2]): ...
  67. def set_chain_shape(self, points: list[vec2]): ...
  68. def apply_force(self, force: vec2, point: vec2): ...
  69. def apply_force_to_center(self, force: vec2): ...
  70. def apply_torque(self, torque: float): ...
  71. def apply_impulse(self, impulse: vec2, point: vec2): ...
  72. def apply_impulse_to_center(self, impulse: vec2): ...
  73. def apply_angular_impulse(self, impulse: float): ...
  74. def get_node(self) -> _NodeLike:
  75. """return the node that is attached to this body."""
  76. def get_contacts(self) -> list['Body']:
  77. """return all bodies that are in contact with this body."""
  78. def destroy(self):
  79. """destroy this body."""