box2d_Body.cpp 6.1 KB

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