box2d_bindings.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #pragma once
  2. #include "box2d/b2_world.h"
  3. #include "box2d/box2d.h"
  4. #include "pocketpy/pocketpy.h"
  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. }
  20. using namespace pkpy;
  21. namespace imbox2d{
  22. // maybe we will use this class later
  23. struct PyDebugDraw: b2Draw{
  24. PK_ALWAYS_PASS_BY_POINTER(PyDebugDraw)
  25. VM* vm;
  26. PyObject* draw_like;
  27. PyDebugDraw(VM* vm): vm(vm){}
  28. void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) override{
  29. }
  30. void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) override{
  31. }
  32. void DrawCircle(const b2Vec2& center, float radius, const b2Color& color) override{
  33. }
  34. void DrawSolidCircle(const b2Vec2& center, float radius, const b2Vec2& axis, const b2Color& color) override{
  35. }
  36. void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) override{
  37. }
  38. void DrawTransform(const b2Transform& xf) override{
  39. }
  40. void DrawPoint(const b2Vec2& p, float size, const b2Color& color) override{
  41. }
  42. };
  43. struct PyContactListener: b2ContactListener{
  44. PK_ALWAYS_PASS_BY_POINTER(PyContactListener)
  45. VM* vm;
  46. PyContactListener(VM* vm): vm(vm){}
  47. void _contact_f(b2Contact* contact, StrName name){
  48. auto a = contact->GetFixtureA()->GetBody()->GetUserData().pointer;
  49. auto b = contact->GetFixtureB()->GetBody()->GetUserData().pointer;
  50. Body* bodyA = reinterpret_cast<Body*>(a);
  51. Body* bodyB = reinterpret_cast<Body*>(b);
  52. PyObject* self;
  53. PyObject* f;
  54. f = vm->get_unbound_method(bodyA->obj, name, &self, false);
  55. if(f != nullptr) vm->call_method(self, f, VAR_T(PyBody, bodyB));
  56. f = vm->get_unbound_method(bodyB->obj, name, &self, false);
  57. if(f != nullptr) vm->call_method(self, f, VAR_T(PyBody, bodyA));
  58. }
  59. void BeginContact(b2Contact* contact) override {
  60. DEF_SNAME(on_contact_begin);
  61. _contact_f(contact, on_contact_begin);
  62. }
  63. void EndContact(b2Contact* contact) override {
  64. DEF_SNAME(on_contact_end);
  65. _contact_f(contact, on_contact_end);
  66. }
  67. };
  68. struct PyBody{
  69. PY_CLASS(PyBody, box2d, Body)
  70. PK_ALWAYS_PASS_BY_POINTER(PyBody)
  71. b2Body* body;
  72. b2Fixture* fixture;
  73. PyObject* node_like;
  74. PyBody() = default;
  75. void _gc_mark() {
  76. PK_OBJ_MARK(node_like);
  77. }
  78. static void _register(VM* vm, PyObject* mod, PyObject* type);
  79. };
  80. struct PyWorld {
  81. PY_CLASS(PyWorld, box2d, World)
  82. PK_ALWAYS_PASS_BY_POINTER(PyWorld)
  83. b2World world;
  84. PyContactListener _contact_listener;
  85. PyDebugDraw _debug_draw;
  86. PyWorld(VM* vm);
  87. void _gc_mark(){
  88. PK_OBJ_MARK(_debug_draw.draw_like);
  89. }
  90. static void _register(VM* vm, PyObject* mod, PyObject* type);
  91. };
  92. struct Body final{
  93. b2Body* body;
  94. b2Fixture* fixture;
  95. PyObject* obj;
  96. Vec4 debug_color;
  97. Body(b2World* world, PyObject* obj){
  98. b2BodyDef def;
  99. def.type = b2_dynamicBody;
  100. // a weak reference to the object, no need to mark it
  101. def.userData.pointer = reinterpret_cast<uintptr_t>(this);
  102. body = world->CreateBody(&def);
  103. fixture = nullptr;
  104. this->obj = obj;
  105. this->debug_color = Vec4(std::rand() / float(RAND_MAX), std::rand() / float(RAND_MAX), std::rand() / float(RAND_MAX), 1.0f);
  106. }
  107. void _update_fixture(b2Shape* shape){
  108. body->DestroyFixture(fixture); // this takes care of NULL case
  109. fixture = body->CreateFixture(shape, 1.0f);
  110. }
  111. Vec4 get_debug_color() const{ return debug_color; }
  112. b2Vec2 get_position() const{ return body->GetPosition(); }
  113. void set_position(b2Vec2 v){ body->SetTransform(v, get_rotation()); }
  114. void set_rotation(float angle){ body->SetTransform(get_position(), angle); }
  115. float get_rotation() const{ return body->GetAngle(); }
  116. void set_velocity(b2Vec2 v){ body->SetLinearVelocity(v); }
  117. b2Vec2 get_velocity() const{ return body->GetLinearVelocity(); }
  118. void set_angular_velocity(float omega){ body->SetAngularVelocity(omega); }
  119. float get_angular_velocity() const{ return body->GetAngularVelocity(); }
  120. void set_damping(float damping){ body->SetLinearDamping(damping); }
  121. float get_damping(){ return body->GetLinearDamping(); }
  122. void set_angular_damping(float damping){ body->SetAngularDamping(damping); }
  123. float get_angular_damping() const{ return body->GetAngularDamping(); }
  124. void set_gravity_scale(float scale){ body->SetGravityScale(scale); }
  125. float get_gravity_scale() const{ return body->GetGravityScale(); }
  126. void set_type(int type){ body->SetType(static_cast<b2BodyType>(type)); }
  127. int get_type() const{ return static_cast<int>(body->GetType()); }
  128. float get_mass() const{ return body->GetMass(); }
  129. float get_inertia() const{ return body->GetInertia(); }
  130. bool get_fixed_rotation() const{ return body->IsFixedRotation(); }
  131. void set_fixed_rotation(bool fixed){ body->SetFixedRotation(fixed); }
  132. // fixture settings
  133. float get_density() const{ return fixture->GetDensity(); }
  134. void set_density(float density){ fixture->SetDensity(density); }
  135. float get_friction() const{ return fixture->GetFriction(); }
  136. void set_friction(float friction){ fixture->SetFriction(friction); }
  137. float get_restitution() const{ return fixture->GetRestitution(); }
  138. void set_restitution(float restitution){ fixture->SetRestitution(restitution); }
  139. float get_restitution_threshold() const{ return fixture->GetRestitutionThreshold(); }
  140. void set_restitution_threshold(float threshold){ fixture->SetRestitutionThreshold(threshold); }
  141. bool get_is_trigger() const{ return fixture->IsSensor(); }
  142. void set_is_trigger(bool trigger){ fixture->SetSensor(trigger); }
  143. // methods
  144. void apply_force(b2Vec2 force, b2Vec2 point){
  145. body->ApplyForce(force, point, true);
  146. }
  147. void apply_force_to_center(b2Vec2 force){
  148. body->ApplyForceToCenter(force, true);
  149. }
  150. void apply_torque(float torque){
  151. body->ApplyTorque(torque, true);
  152. }
  153. void apply_linear_impulse(b2Vec2 impulse, b2Vec2 point){
  154. body->ApplyLinearImpulse(impulse, point, true);
  155. }
  156. void apply_linear_impulse_to_center(b2Vec2 impulse){
  157. body->ApplyLinearImpulseToCenter(impulse, true);
  158. }
  159. void apply_angular_impulse(float impulse){
  160. body->ApplyAngularImpulse(impulse, true);
  161. }
  162. void destroy(){
  163. if(body == nullptr) return;
  164. body->GetWorld()->DestroyBody(body);
  165. body = nullptr;
  166. }
  167. };
  168. inline PyObject* get_body_object(b2Body* p){
  169. auto userdata = p->GetUserData().pointer;
  170. return reinterpret_cast<PyObject*>(userdata);
  171. }
  172. } // namespace imbox2d
  173. namespace pkpy{
  174. inline void add_module_box2d(VM* vm){
  175. PyObject* mod = vm->new_module("box2d");
  176. imbox2d::PyBody::register_class(vm, mod);
  177. imbox2d::PyWorld::register_class(vm, mod);
  178. }
  179. }