box2d_Body.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "box2d_bindings.hpp"
  2. namespace pkpy{
  3. void PyBody::_register(VM* vm, PyObject* mod, PyObject* type){
  4. vm->bind(type, "__new__(cls, world: World, node: _NodeLike = None)",
  5. [](VM* vm, ArgsView args){
  6. PyWorld& world = CAST(PyWorld&, args[1]);
  7. PyObject* node = args[2];
  8. PyObject* obj = vm->heap.gcnew<PyBody>(PyBody::_type(vm));
  9. PyBody& body = _CAST(PyBody&, obj);
  10. b2BodyDef def;
  11. def.type = b2_dynamicBody;
  12. // a weak reference to this object
  13. def.userData.pointer = reinterpret_cast<uintptr_t>(obj);
  14. body.body = world.world.CreateBody(&def);
  15. body.fixture = nullptr;
  16. body.node_like = node;
  17. return obj;
  18. });
  19. PK_REGISTER_PROPERTY(PyBody, "type: int", _b2Body, GetType, SetType)
  20. PK_REGISTER_PROPERTY(PyBody, "gravity_scale: float", _b2Body, GetGravityScale, SetGravityScale)
  21. PK_REGISTER_PROPERTY(PyBody, "fixed_rotation: bool", _b2Body, IsFixedRotation, SetFixedRotation)
  22. PK_REGISTER_PROPERTY(PyBody, "enabled: bool", _b2Body, IsEnabled, SetEnabled)
  23. PK_REGISTER_PROPERTY(PyBody, "bullet: bool", _b2Body, IsBullet, SetBullet)
  24. PK_REGISTER_READONLY_PROPERTY(PyBody, "mass: float", _b2Body, GetMass)
  25. PK_REGISTER_READONLY_PROPERTY(PyBody, "inertia: float", _b2Body, GetInertia)
  26. PK_REGISTER_PROPERTY(PyBody, "position: vec2", _, get_position, set_position)
  27. PK_REGISTER_PROPERTY(PyBody, "rotation: float", _, get_rotation, set_rotation)
  28. PK_REGISTER_PROPERTY(PyBody, "velocity: vec2", _, get_velocity, set_velocity)
  29. PK_REGISTER_PROPERTY(PyBody, "angular_velocity: float", _b2Body, GetAngularVelocity, SetAngularVelocity)
  30. PK_REGISTER_PROPERTY(PyBody, "damping: float", _b2Body, GetLinearDamping, SetLinearDamping)
  31. PK_REGISTER_PROPERTY(PyBody, "angular_damping: float", _b2Body, GetAngularDamping, SetAngularDamping)
  32. PK_REGISTER_PROPERTY(PyBody, "density: float", _b2Fixture, GetDensity, SetDensity)
  33. PK_REGISTER_PROPERTY(PyBody, "friction: float", _b2Fixture, GetFriction, SetFriction)
  34. PK_REGISTER_PROPERTY(PyBody, "restitution: float", _b2Fixture, GetRestitution, SetRestitution)
  35. PK_REGISTER_PROPERTY(PyBody, "restitution_threshold: float", _b2Fixture, GetRestitutionThreshold, SetRestitutionThreshold)
  36. PK_REGISTER_PROPERTY(PyBody, "is_trigger: bool", _b2Fixture, IsSensor, SetSensor)
  37. vm->bind(type, "set_box_shape(self, hx: float, hy: float)",
  38. [](VM* vm, ArgsView args){
  39. PyBody& body = CAST(PyBody&, args[0]);
  40. float hx = CAST(float, args[1]);
  41. float hy = CAST(float, args[2]);
  42. b2PolygonShape shape;
  43. shape.SetAsBox(hx, hy);
  44. body.fixture = body.body->CreateFixture(&shape, 1.0f);
  45. return vm->None;
  46. });
  47. vm->bind(type, "set_circle_shape(self, radius: float)",
  48. [](VM* vm, ArgsView args){
  49. PyBody& body = CAST(PyBody&, args[0]);
  50. float radius = CAST(float, args[1]);
  51. b2CircleShape shape;
  52. shape.m_radius = radius;
  53. body.fixture = body.body->CreateFixture(&shape, 1.0f);
  54. return vm->None;
  55. });
  56. vm->bind(type, "set_polygon_shape(self, points: list[vec2])",
  57. [](VM* vm, ArgsView args){
  58. PyBody& body = CAST(PyBody&, args[0]);
  59. List& points = CAST(List&, args[1]);
  60. if(points.size() < 3 || points.size() > b2_maxPolygonVertices){
  61. vm->ValueError("invalid vertices count");
  62. }
  63. b2PolygonShape shape;
  64. std::vector<b2Vec2> vertices;
  65. for(auto& point : points){
  66. Vec2 vec = CAST(Vec2, point);
  67. vertices.push_back(b2Vec2(vec.x, vec.y));
  68. }
  69. shape.Set(vertices.data(), vertices.size());
  70. body.fixture = body.body->CreateFixture(&shape, 1.0f);
  71. return vm->None;
  72. });
  73. vm->bind(type, "set_chain_shape(self, points: list[vec2])",
  74. [](VM* vm, ArgsView args){
  75. PyBody& body = CAST(PyBody&, args[0]);
  76. List& points = CAST(List&, args[1]);
  77. if(points.size() < 3){
  78. vm->ValueError("invalid vertices count");
  79. }
  80. b2ChainShape shape;
  81. std::vector<b2Vec2> vertices;
  82. for(auto& point : points){
  83. Vec2 vec = CAST(Vec2, point);
  84. vertices.push_back(b2Vec2(vec.x, vec.y));
  85. }
  86. shape.CreateLoop(vertices.data(), vertices.size());
  87. body.fixture = body.body->CreateFixture(&shape, 1.0f);
  88. return vm->None;
  89. });
  90. // methods
  91. _bind(vm, type, "apply_force(self, force: vec2, point: vec2)", &PyBody::apply_force);
  92. _bind(vm, type, "apply_force_to_center(self, force: vec2)", &PyBody::apply_force_to_center);
  93. _bind(vm, type, "apply_torque(self, torque: float)", &PyBody::apply_torque);
  94. _bind(vm, type, "apply_impulse(self, impulse: vec2, point: vec2)", &PyBody::apply_impulse);
  95. _bind(vm, type, "apply_impulse_to_center(self, impulse: vec2)", &PyBody::apply_impulse_to_center);
  96. _bind(vm, type, "apply_angular_impulse(self, impulse: float)", &PyBody::apply_angular_impulse);
  97. // get_node
  98. vm->bind(type, "get_node(self)", [](VM* vm, ArgsView args){
  99. PyBody& body = CAST(PyBody&, args[0]);
  100. return body.node_like;
  101. });
  102. // get_contacts
  103. vm->bind(type, "get_contacts(self) -> list[Body]", [](VM* vm, ArgsView args){
  104. PyBody& self = _CAST(PyBody&, args[0]);
  105. b2ContactEdge* edge = self.body->GetContactList();
  106. List list;
  107. while(edge){
  108. b2Fixture* fixtureB = edge->contact->GetFixtureB();
  109. b2Body* bodyB = fixtureB->GetBody();
  110. list.push_back(get_body_object(bodyB));
  111. edge = edge->next;
  112. }
  113. return VAR(std::move(list));
  114. });
  115. // destroy
  116. vm->bind(type, "destroy(self)", [](VM* vm, ArgsView args){
  117. PyBody& body = CAST(PyBody&, args[1]);
  118. body.body->GetWorld()->DestroyBody(body.body);
  119. body.body = nullptr;
  120. body.fixture = nullptr;
  121. body.node_like = nullptr;
  122. return vm->None;
  123. });
  124. }
  125. } // namespace pkpy