box2d_bindings.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #include "box2d/box2d.h"
  3. #include "pocketpy/pocketpy.h"
  4. #include <queue>
  5. namespace pkpy{
  6. template<>
  7. inline b2Vec2 py_cast<b2Vec2>(VM* vm, PyObject* obj){
  8. Vec2 v = py_cast<Vec2>(vm, obj);
  9. return b2Vec2(v.x, v.y);
  10. }
  11. template<>
  12. inline b2Vec2 _py_cast<b2Vec2>(VM* vm, PyObject* obj){
  13. Vec2 v = _py_cast<Vec2>(vm, obj);
  14. return b2Vec2(v.x, v.y);
  15. }
  16. inline PyObject* py_var(VM* vm, b2Vec2 v){
  17. return py_var(vm, Vec2(v.x, v.y));
  18. }
  19. inline PyObject* get_body_object(b2Body* p){
  20. auto userdata = p->GetUserData().pointer;
  21. return reinterpret_cast<PyObject*>(userdata);
  22. }
  23. // maybe we will use this class later
  24. struct PyDebugDraw: b2Draw{
  25. PK_ALWAYS_PASS_BY_POINTER(PyDebugDraw)
  26. VM* vm;
  27. PyObject* draw_like; // world will mark this
  28. PyDebugDraw(VM* vm): vm(vm){}
  29. void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) override;
  30. void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) override;
  31. void DrawCircle(const b2Vec2& center, float radius, const b2Color& color) override;
  32. void DrawSolidCircle(const b2Vec2& center, float radius, const b2Vec2& axis, const b2Color& color) override;
  33. void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) override;
  34. void DrawTransform(const b2Transform& xf) override;
  35. void DrawPoint(const b2Vec2& p, float size, const b2Color& color) override;
  36. };
  37. struct PyContactMessage{
  38. PyObject* a;
  39. PyObject* b;
  40. StrName name;
  41. };
  42. struct PyContactListener: b2ContactListener{
  43. PK_ALWAYS_PASS_BY_POINTER(PyContactListener)
  44. VM* vm;
  45. std::queue<PyContactMessage> messages;
  46. PyContactListener(VM* vm): vm(vm){}
  47. void _contact_f(b2Contact* contact, StrName name);
  48. void BeginContact(b2Contact* contact) override {
  49. DEF_SNAME(on_contact_begin);
  50. _contact_f(contact, on_contact_begin);
  51. }
  52. void EndContact(b2Contact* contact) override {
  53. DEF_SNAME(on_contact_end);
  54. _contact_f(contact, on_contact_end);
  55. }
  56. };
  57. struct PyBody{
  58. PY_CLASS(PyBody, box2d, Body)
  59. PK_ALWAYS_PASS_BY_POINTER(PyBody)
  60. b2Body* body;
  61. b2Fixture* fixture;
  62. PyObject* node_like;
  63. PyBody(): body(nullptr), fixture(nullptr), node_like(nullptr){}
  64. void _gc_mark() {
  65. if(node_like != nullptr){
  66. PK_OBJ_MARK(node_like);
  67. }
  68. }
  69. PyBody& _() { return *this; }
  70. b2Body& _b2Body() { return *body; }
  71. b2Fixture& _b2Fixture() { return *fixture; }
  72. static void _register(VM* vm, PyObject* mod, PyObject* type);
  73. // methods
  74. b2Vec2 get_position() const { return body->GetPosition(); }
  75. void set_position(b2Vec2 v){ body->SetTransform(v, body->GetAngle()); }
  76. float get_rotation() const { return body->GetAngle(); }
  77. void set_rotation(float v){ body->SetTransform(body->GetPosition(), v); }
  78. b2Vec2 get_velocity() const { return body->GetLinearVelocity(); }
  79. void set_velocity(b2Vec2 v){ body->SetLinearVelocity(v); }
  80. void apply_force(b2Vec2 force, b2Vec2 point){ body->ApplyForce(force, point, true); }
  81. void apply_force_to_center(b2Vec2 force){ body->ApplyForceToCenter(force, true); }
  82. void apply_torque(float torque){ body->ApplyTorque(torque, true); }
  83. void apply_impulse(b2Vec2 impulse, b2Vec2 point){
  84. body->ApplyLinearImpulse(impulse, point, true);
  85. }
  86. void apply_impulse_to_center(b2Vec2 impulse){
  87. body->ApplyLinearImpulseToCenter(impulse, true);
  88. }
  89. void apply_angular_impulse(float impulse){
  90. body->ApplyAngularImpulse(impulse, true);
  91. }
  92. };
  93. struct PyWorld {
  94. PY_CLASS(PyWorld, box2d, World)
  95. PK_ALWAYS_PASS_BY_POINTER(PyWorld)
  96. b2World world;
  97. PyContactListener _contact_listener;
  98. PyDebugDraw _debug_draw;
  99. PyWorld(VM* vm);
  100. void _gc_mark(){
  101. PK_OBJ_MARK(_debug_draw.draw_like);
  102. }
  103. static void _register(VM* vm, PyObject* mod, PyObject* type);
  104. };
  105. inline void add_module_box2d(VM* vm){
  106. PyObject* mod = vm->new_module("box2d");
  107. PyBody::register_class(vm, mod);
  108. PyWorld::register_class(vm, mod);
  109. }
  110. } // namespace pkpy