1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #include "ChainShape.h" |
22 | |
23 | // Module |
24 | #include "Body.h" |
25 | #include "World.h" |
26 | #include "Physics.h" |
27 | |
28 | namespace love |
29 | { |
30 | namespace physics |
31 | { |
32 | namespace box2d |
33 | { |
34 | |
35 | love::Type ChainShape::type("ChainShape" , &Shape::type); |
36 | |
37 | ChainShape::ChainShape(b2ChainShape *c, bool own) |
38 | : Shape(c, own) |
39 | { |
40 | } |
41 | |
42 | ChainShape::~ChainShape() |
43 | { |
44 | } |
45 | |
46 | void ChainShape::setNextVertex(float x, float y) |
47 | { |
48 | b2Vec2 v(x, y); |
49 | b2ChainShape *c = (b2ChainShape *)shape; |
50 | c->SetNextVertex(Physics::scaleDown(v)); |
51 | } |
52 | |
53 | void ChainShape::setNextVertex() |
54 | { |
55 | b2ChainShape *c = (b2ChainShape *)shape; |
56 | c->m_hasNextVertex = false; |
57 | } |
58 | |
59 | void ChainShape::setPreviousVertex(float x, float y) |
60 | { |
61 | b2Vec2 v(x, y); |
62 | b2ChainShape *c = (b2ChainShape *)shape; |
63 | c->SetPrevVertex(Physics::scaleDown(v)); |
64 | } |
65 | |
66 | void ChainShape::setPreviousVertex() |
67 | { |
68 | b2ChainShape *c = (b2ChainShape *)shape; |
69 | c->m_hasPrevVertex = false; |
70 | } |
71 | |
72 | bool ChainShape::getNextVertex(float &x, float &y) const |
73 | { |
74 | b2ChainShape *c = (b2ChainShape *)shape; |
75 | |
76 | if (c->m_hasNextVertex) |
77 | { |
78 | b2Vec2 v = Physics::scaleUp(c->m_nextVertex); |
79 | x = v.x; |
80 | y = v.y; |
81 | return true; |
82 | } |
83 | |
84 | return false; |
85 | } |
86 | |
87 | bool ChainShape::getPreviousVertex(float &x, float &y) const |
88 | { |
89 | b2ChainShape *c = (b2ChainShape *)shape; |
90 | |
91 | if (c->m_hasPrevVertex) |
92 | { |
93 | b2Vec2 v = Physics::scaleUp(c->m_prevVertex); |
94 | x = v.x; |
95 | y = v.y; |
96 | return true; |
97 | } |
98 | |
99 | return false; |
100 | } |
101 | |
102 | EdgeShape *ChainShape::getChildEdge(int index) const |
103 | { |
104 | b2ChainShape *c = (b2ChainShape *)shape; |
105 | b2EdgeShape *e = new b2EdgeShape; |
106 | |
107 | try |
108 | { |
109 | c->GetChildEdge(e, index); |
110 | } |
111 | catch (love::Exception &) |
112 | { |
113 | delete e; |
114 | throw; |
115 | } |
116 | |
117 | return new EdgeShape(e, true); |
118 | } |
119 | |
120 | int ChainShape::getVertexCount() const |
121 | { |
122 | b2ChainShape *c = (b2ChainShape *)shape; |
123 | return c->m_count; |
124 | } |
125 | |
126 | b2Vec2 ChainShape::getPoint(int index) const |
127 | { |
128 | b2ChainShape *c = (b2ChainShape *)shape; |
129 | if (index < 0 || index >= c->m_count) |
130 | throw love::Exception("Physics error: index out of bounds" ); |
131 | const b2Vec2 &v = c->m_vertices[index]; |
132 | return Physics::scaleUp(v); |
133 | } |
134 | |
135 | const b2Vec2 *ChainShape::getPoints() const |
136 | { |
137 | b2ChainShape *c = (b2ChainShape *)shape; |
138 | return c->m_vertices; |
139 | } |
140 | |
141 | } // box2d |
142 | } // physics |
143 | } // love |
144 | |