b2_edge_shape.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_EDGE_SHAPE_H
  19. #define B2_EDGE_SHAPE_H
  20. #include "b2_api.h"
  21. #include "b2_shape.h"
  22. /// A line segment (edge) shape. These can be connected in chains or loops
  23. /// to other edge shapes. Edges created independently are two-sided and do
  24. /// no provide smooth movement across junctions.
  25. class B2_API b2EdgeShape : public b2Shape
  26. {
  27. public:
  28. b2EdgeShape();
  29. /// Set this as a part of a sequence. Vertex v0 precedes the edge and vertex v3
  30. /// follows. These extra vertices are used to provide smooth movement
  31. /// across junctions. This also makes the collision one-sided. The edge
  32. /// normal points to the right looking from v1 to v2.
  33. void SetOneSided(const b2Vec2& v0, const b2Vec2& v1,const b2Vec2& v2, const b2Vec2& v3);
  34. /// Set this as an isolated edge. Collision is two-sided.
  35. void SetTwoSided(const b2Vec2& v1, const b2Vec2& v2);
  36. /// Implement b2Shape.
  37. b2Shape* Clone(b2BlockAllocator* allocator) const override;
  38. /// @see b2Shape::GetChildCount
  39. int32 GetChildCount() const override;
  40. /// @see b2Shape::TestPoint
  41. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const override;
  42. /// Implement b2Shape.
  43. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  44. const b2Transform& transform, int32 childIndex) const override;
  45. /// @see b2Shape::ComputeAABB
  46. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const override;
  47. /// @see b2Shape::ComputeMass
  48. void ComputeMass(b2MassData* massData, float density) const override;
  49. /// These are the edge vertices
  50. b2Vec2 m_vertex1, m_vertex2;
  51. /// Optional adjacent vertices. These are used for smooth collision.
  52. b2Vec2 m_vertex0, m_vertex3;
  53. /// Uses m_vertex0 and m_vertex3 to create smooth collision.
  54. bool m_oneSided;
  55. };
  56. inline b2EdgeShape::b2EdgeShape()
  57. {
  58. m_type = e_edge;
  59. m_radius = b2_polygonRadius;
  60. m_vertex0.x = 0.0f;
  61. m_vertex0.y = 0.0f;
  62. m_vertex3.x = 0.0f;
  63. m_vertex3.y = 0.0f;
  64. m_oneSided = false;
  65. }
  66. #endif