box2d_bindings.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #include "box2d/b2_world.h"
  3. #include "box2d/box2d.h"
  4. #include "pocketpy/pocketpy.h"
  5. #include <cstdlib>
  6. namespace pkpy{
  7. template<>
  8. inline b2Vec2 py_cast<b2Vec2>(VM* vm, PyObject* obj){
  9. Vec2 v = py_cast<Vec2>(vm, obj);
  10. return b2Vec2(v.x, v.y);
  11. }
  12. template<>
  13. inline b2Vec2 _py_cast<b2Vec2>(VM* vm, PyObject* obj){
  14. Vec2 v = _py_cast<Vec2>(vm, obj);
  15. return b2Vec2(v.x, v.y);
  16. }
  17. inline PyObject* py_var(VM* vm, b2Vec2 v){
  18. return py_var(vm, Vec2(v.x, v.y));
  19. }
  20. namespace imbox2d{
  21. struct Body final{
  22. b2Body* body;
  23. b2Fixture* fixture;
  24. PyObject* obj;
  25. Vec4 debug_color;
  26. Body(b2World* world, PyObject* obj){
  27. b2BodyDef def;
  28. def.type = b2_dynamicBody;
  29. // a weak reference to the object, no need to mark it
  30. def.userData.pointer = reinterpret_cast<uintptr_t>(this);
  31. body = world->CreateBody(&def);
  32. fixture = nullptr;
  33. this->obj = obj;
  34. this->debug_color = Vec4(std::rand() / float(RAND_MAX), std::rand() / float(RAND_MAX), std::rand() / float(RAND_MAX), 1.0f);
  35. }
  36. void _update_fixture(b2Shape* shape){
  37. body->DestroyFixture(fixture); // this takes care of NULL case
  38. fixture = body->CreateFixture(shape, 1.0f);
  39. }
  40. Vec4 get_debug_color() const{ return debug_color; }
  41. b2Vec2 get_position() const{ return body->GetPosition(); }
  42. void set_position(b2Vec2 v){ body->SetTransform(v, get_rotation()); }
  43. void set_rotation(float angle){ body->SetTransform(get_position(), angle); }
  44. float get_rotation() const{ return body->GetAngle(); }
  45. void set_velocity(b2Vec2 v){ body->SetLinearVelocity(v); }
  46. b2Vec2 get_velocity() const{ return body->GetLinearVelocity(); }
  47. void set_angular_velocity(float omega){ body->SetAngularVelocity(omega); }
  48. float get_angular_velocity() const{ return body->GetAngularVelocity(); }
  49. void set_damping(float damping){ body->SetLinearDamping(damping); }
  50. float get_damping(){ return body->GetLinearDamping(); }
  51. void set_angular_damping(float damping){ body->SetAngularDamping(damping); }
  52. float get_angular_damping() const{ return body->GetAngularDamping(); }
  53. void set_gravity_scale(float scale){ body->SetGravityScale(scale); }
  54. float get_gravity_scale() const{ return body->GetGravityScale(); }
  55. void set_type(int type){ body->SetType(static_cast<b2BodyType>(type)); }
  56. int get_type() const{ return static_cast<int>(body->GetType()); }
  57. float get_mass() const{ return body->GetMass(); }
  58. float get_inertia() const{ return body->GetInertia(); }
  59. bool get_fixed_rotation() const{ return body->IsFixedRotation(); }
  60. void set_fixed_rotation(bool fixed){ body->SetFixedRotation(fixed); }
  61. // fixture settings
  62. float get_density() const{ return fixture->GetDensity(); }
  63. void set_density(float density){ fixture->SetDensity(density); }
  64. float get_friction() const{ return fixture->GetFriction(); }
  65. void set_friction(float friction){ fixture->SetFriction(friction); }
  66. float get_restitution() const{ return fixture->GetRestitution(); }
  67. void set_restitution(float restitution){ fixture->SetRestitution(restitution); }
  68. float get_restitution_threshold() const{ return fixture->GetRestitutionThreshold(); }
  69. void set_restitution_threshold(float threshold){ fixture->SetRestitutionThreshold(threshold); }
  70. bool get_is_trigger() const{ return fixture->IsSensor(); }
  71. void set_is_trigger(bool trigger){ fixture->SetSensor(trigger); }
  72. // methods
  73. void apply_force(b2Vec2 force, b2Vec2 point){
  74. body->ApplyForce(force, point, true);
  75. }
  76. void apply_force_to_center(b2Vec2 force){
  77. body->ApplyForceToCenter(force, true);
  78. }
  79. void apply_torque(float torque){
  80. body->ApplyTorque(torque, true);
  81. }
  82. void apply_linear_impulse(b2Vec2 impulse, b2Vec2 point){
  83. body->ApplyLinearImpulse(impulse, point, true);
  84. }
  85. void apply_linear_impulse_to_center(b2Vec2 impulse){
  86. body->ApplyLinearImpulseToCenter(impulse, true);
  87. }
  88. void apply_angular_impulse(float impulse){
  89. body->ApplyAngularImpulse(impulse, true);
  90. }
  91. void destroy(){
  92. if(body == nullptr) return;
  93. body->GetWorld()->DestroyBody(body);
  94. body = nullptr;
  95. }
  96. };
  97. struct PyBody: OpaquePointer<Body>{
  98. PY_CLASS(PyBody, box2d, Body)
  99. using OpaquePointer<Body>::OpaquePointer;
  100. static void _register(VM* vm, PyObject* mod, PyObject* type);
  101. };
  102. } // namespace imbox2d
  103. void add_module_box2d(VM* vm);
  104. } // namespace pkpy