Skip to content

Commit 4bb185b

Browse files
committed
Use sf::Vector2f instead of sf::Transform
1 parent 421b0d3 commit 4bb185b

File tree

5 files changed

+53
-60
lines changed

5 files changed

+53
-60
lines changed

Collision.cpp

+24-27
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
void handleResult(const c2Manifold&, Manifold&);
66
void handleResult(const c2Raycast&, const c2Ray&, Raycast&);
7-
c2AABB shapeToc2(const Aabb&, const sf::Transform);
8-
c2Circle shapeToc2(const Circle&, const sf::Transform);
9-
c2Ray rayToc2(const Ray&, const sf::Transform);
7+
c2AABB shapeToc2(const Aabb&, const sf::Vector2f);
8+
c2Circle shapeToc2(const Circle&, const sf::Vector2f);
9+
c2Ray rayToc2(const Ray&, const sf::Vector2f);
1010
c2v toc2v(const sf::Vector2f&);
1111
sf::Vector2f tosfv(const c2v&);
1212

@@ -110,11 +110,11 @@ Manifold::Manifold()
110110
{
111111
}
112112

113-
void Manifold::solve(const Shape& a, const sf::Transform transformA,
114-
const Shape& b, const sf::Transform transformB)
113+
void Manifold::solve(const Shape& a, const sf::Vector2f positionA,
114+
const Shape& b, const sf::Vector2f positionB)
115115
{
116-
mTransformA = transformA;
117-
mTransformB = transformB;
116+
mPositionA = positionA;
117+
mPositionB = positionB;
118118

119119
dispatch(a, b);
120120
}
@@ -155,8 +155,8 @@ void Manifold::dispatch(const Shape& a, const Shape& b)
155155

156156
void Manifold::collide(const Aabb& a, const Aabb& b)
157157
{
158-
c2AABB shapeA = shapeToc2(a, mTransformA);
159-
c2AABB shapeB = shapeToc2(b, mTransformB);
158+
c2AABB shapeA = shapeToc2(a, mPositionA);
159+
c2AABB shapeB = shapeToc2(b, mPositionB);
160160
c2Manifold m;
161161
c2AABBtoAABBManifold(shapeA, shapeB, &m);
162162

@@ -165,8 +165,8 @@ void Manifold::collide(const Aabb& a, const Aabb& b)
165165

166166
void Manifold::collide(const Circle& a, const Circle& b)
167167
{
168-
c2Circle shapeA = shapeToc2(a, mTransformA);
169-
c2Circle shapeB = shapeToc2(b, mTransformB);
168+
c2Circle shapeA = shapeToc2(a, mPositionA);
169+
c2Circle shapeB = shapeToc2(b, mPositionB);
170170
c2Manifold m;
171171
c2CircletoCircleManifold(shapeA, shapeB, &m);
172172

@@ -181,8 +181,8 @@ void Manifold::collide(const Aabb& a, const Circle& b)
181181

182182
void Manifold::collide(const Circle& a, const Aabb& b)
183183
{
184-
c2Circle shapeA = shapeToc2(a, mTransformA);
185-
c2AABB shapeB = shapeToc2(b, mTransformB);
184+
c2Circle shapeA = shapeToc2(a, mPositionA);
185+
c2AABB shapeB = shapeToc2(b, mPositionB);
186186
c2Manifold m;
187187
c2CircletoAABBManifold(shapeA, shapeB, &m);
188188

@@ -196,11 +196,11 @@ Raycast::Raycast()
196196
{
197197
}
198198

199-
void Raycast::solve(const Ray& a, const sf::Transform transformA,
200-
const Shape& b, const sf::Transform transformB)
199+
void Raycast::solve(const Ray& a, const sf::Vector2f positionA,
200+
const Shape& b, const sf::Vector2f positionB)
201201
{
202-
mTransformA = transformA;
203-
mTransformB = transformB;
202+
mPositionA = positionA;
203+
mPositionB = positionB;
204204

205205
dispatch(a, b);
206206
}
@@ -221,8 +221,8 @@ void Raycast::dispatch(const Ray& a, const Shape& b)
221221

222222
void Raycast::calculate(const Ray& a, const Aabb& b)
223223
{
224-
c2Ray ray = rayToc2(a, mTransformA);
225-
c2AABB shape = shapeToc2(b, mTransformB);
224+
c2Ray ray = rayToc2(a, mPositionA);
225+
c2AABB shape = shapeToc2(b, mPositionB);
226226
c2Raycast r;
227227
hit = false;
228228

@@ -232,8 +232,8 @@ void Raycast::calculate(const Ray& a, const Aabb& b)
232232

233233
void Raycast::calculate(const Ray& a, const Circle& b)
234234
{
235-
c2Ray ray = rayToc2(a, mTransformA);
236-
c2Circle shape = shapeToc2(b, mTransformB);
235+
c2Ray ray = rayToc2(a, mPositionA);
236+
c2Circle shape = shapeToc2(b, mPositionB);
237237
c2Raycast r;
238238
hit = false;
239239

@@ -260,27 +260,24 @@ void handleResult(const c2Raycast& raycast, const c2Ray& ray, Raycast& r)
260260
r.t = raycast.t;
261261
}
262262

263-
c2AABB shapeToc2(const Aabb& aabb, const sf::Transform transform)
263+
c2AABB shapeToc2(const Aabb& aabb, const sf::Vector2f position)
264264
{
265-
sf::Vector2f position = transform.transformPoint(0.0f, 0.0f);
266265
c2AABB tmp;
267266
tmp.min = toc2v(position);
268267
tmp.max = toc2v(position + aabb.getSize());
269268
return tmp;
270269
}
271270

272-
c2Circle shapeToc2(const Circle& circle, const sf::Transform transform)
271+
c2Circle shapeToc2(const Circle& circle, const sf::Vector2f position)
273272
{
274-
sf::Vector2f position = transform.transformPoint(0.0f, 0.0f);
275273
c2Circle tmp;
276274
tmp.p = toc2v(position);
277275
tmp.r = circle.getRadius();
278276
return tmp;
279277
}
280278

281-
c2Ray rayToc2(const Ray& ray, const sf::Transform transform)
279+
c2Ray rayToc2(const Ray& ray, const sf::Vector2f position)
282280
{
283-
sf::Vector2f position = transform.transformPoint(0.0f, 0.0f);
284281
c2Ray tmp;
285282
tmp.p = toc2v(position);
286283
tmp.d = toc2v(ray.getDirection());

Collision.hpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef COLLISION_HPP_INCLUDED
22
#define COLLISION_HPP_INCLUDED
33

4-
#include <SFML/Graphics/Transform.hpp>
4+
#include <SFML/System/Vector2.hpp>
55
#include <memory>
66
#include <cassert>
77

@@ -85,8 +85,8 @@ class Manifold
8585
public:
8686
Manifold();
8787

88-
void solve(const Shape& a, const sf::Transform transformA,
89-
const Shape& b, const sf::Transform transformB);
88+
void solve(const Shape& a, const sf::Vector2f positionA,
89+
const Shape& b, const sf::Vector2f positionB);
9090

9191
private:
9292
void dispatch(const Shape& a, const Shape& b);
@@ -102,17 +102,17 @@ class Manifold
102102
float depth;
103103

104104
private:
105-
sf::Transform mTransformA;
106-
sf::Transform mTransformB;
105+
sf::Vector2f mPositionA;
106+
sf::Vector2f mPositionB;
107107
};
108108

109109
class Raycast
110110
{
111111
public:
112112
Raycast();
113113

114-
void solve(const Ray& a, const sf::Transform transformA,
115-
const Shape& b, const sf::Transform transformB);
114+
void solve(const Ray& a, const sf::Vector2f positionA,
115+
const Shape& b, const sf::Vector2f positionB);
116116

117117
private:
118118
void dispatch(const Ray& a, const Shape& b);
@@ -126,8 +126,8 @@ class Raycast
126126
float t;
127127

128128
private:
129-
sf::Transform mTransformA;
130-
sf::Transform mTransformB;
129+
sf::Vector2f mPositionA;
130+
sf::Vector2f mPositionB;
131131
};
132132

133133
template<typename T>

DebugDraw.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,31 @@ DebugDraw::DebugDraw(sf::RenderTarget& target)
55
{
66
}
77

8-
void DebugDraw::draw(const Aabb& aabb, const sf::Transform transform, sf::Color color)
8+
void DebugDraw::draw(const Aabb& aabb, const sf::Vector2f position, sf::Color color)
99
{
1010
sf::RectangleShape shape(aabb.getSize());
11-
shape.setPosition(transform.transformPoint(0.0f, 0.0f));
11+
shape.setPosition(position);
1212
shape.setFillColor(sf::Color::Transparent);
1313
shape.setOutlineColor(color);
1414
shape.setOutlineThickness(1.0f);
1515

1616
mTarget.draw(shape);
1717
}
1818

19-
void DebugDraw::draw(const Circle& circle, const sf::Transform transform, sf::Color color)
19+
void DebugDraw::draw(const Circle& circle, const sf::Vector2f position, sf::Color color)
2020
{
2121
sf::CircleShape shape(circle.getRadius());
2222
shape.setOrigin(circle.getRadius(), circle.getRadius());
23-
shape.setPosition(transform.transformPoint(0.0f, 0.0f));
23+
shape.setPosition(position);
2424
shape.setFillColor(sf::Color::Transparent);
2525
shape.setOutlineColor(color);
2626
shape.setOutlineThickness(1.0f);
2727

2828
mTarget.draw(shape);
2929
}
3030

31-
void DebugDraw::draw(const Ray& ray, const sf::Transform transform, sf::Color color)
31+
void DebugDraw::draw(const Ray& ray, const sf::Vector2f position, sf::Color color)
3232
{
33-
sf::Vector2f position = transform.transformPoint(0.0f, 0.0f);
3433
sf::Vector2f direction = ray.getDirection() * ray.getLength();
3534
sf::Vertex line[] = {
3635
sf::Vertex(position, color),
@@ -78,14 +77,14 @@ void DebugDraw::draw(const Raycast& raycast, sf::Color color)
7877
}
7978
}
8079

81-
void DebugDraw::draw(const Shape& shape, const sf::Transform transform, sf::Color color)
80+
void DebugDraw::draw(const Shape& shape, const sf::Vector2f position, sf::Color color)
8281
{
8382
if (shape.getType() == Type::Aabb)
8483
{
85-
draw(castShape<Aabb>(shape), transform, color);
84+
draw(castShape<Aabb>(shape), position, color);
8685
}
8786
else if (shape.getType() == Type::Circle)
8887
{
89-
draw(castShape<Circle>(shape), transform, color);
88+
draw(castShape<Circle>(shape), position, color);
9089
}
9190
}

DebugDraw.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ class DebugDraw final
1212
public:
1313
DebugDraw(sf::RenderTarget& target);
1414

15-
void draw(const Aabb& aabb, const sf::Transform transform,
15+
void draw(const Aabb& aabb, const sf::Vector2f position,
1616
sf::Color color = sf::Color::White);
17-
void draw(const Circle& circle, const sf::Transform transform,
17+
void draw(const Circle& circle, const sf::Vector2f position,
1818
sf::Color color = sf::Color::White);
19-
void draw(const Ray& ray, const sf::Transform transform,
19+
void draw(const Ray& ray, const sf::Vector2f position,
2020
sf::Color color = sf::Color::White);
2121
void draw(const Manifold& manifold, sf::Color color = sf::Color::White);
2222
void draw(const Raycast& raycast, sf::Color color = sf::Color::White);
23-
void draw(const Shape& shape, const sf::Transform transform,
23+
void draw(const Shape& shape, const sf::Vector2f position,
2424
sf::Color color = sf::Color::White);
2525

2626
private:

main.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ int main()
1313
DebugDraw debug(window);
1414

1515
Circle::Ptr shapeA = std::make_unique<Circle>(30);
16-
sf::Transform positionA;
17-
positionA.translate(200, 200);
16+
sf::Vector2f positionA(200, 200);
1817
Aabb::Ptr shapeB = std::make_unique<Aabb>(50, 60);
19-
sf::Transform positionB;
20-
positionB.translate(300, 300);
18+
sf::Vector2f positionB(300, 300);
2119
Manifold m;
2220
Ray::Ptr ray = std::make_unique<Ray>(0, 1, 100);
23-
sf::Transform positionR;
24-
positionR.translate(100, 100);
21+
sf::Vector2f positionR(100, 100);
2522
Raycast r;
2623

2724
sf::Clock clock;
@@ -43,20 +40,20 @@ int main()
4340
accumulator -= delta;
4441

4542
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
46-
positionA.translate(1.0f * speed, 0.0f);
43+
positionA.x += 1.0f * speed;
4744
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
48-
positionA.translate(-1.0f * speed, 0.0f);
45+
positionA.x -= 1.0f * speed;
4946
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
50-
positionA.translate(0.0f, 1.0f * speed);
47+
positionA.y += 1.0f * speed;
5148
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
52-
positionA.translate(0.0f, -1.0f * speed);
49+
positionA.y -= 1.0f * speed;
5350

5451
m.solve(*shapeA, positionA, *shapeB, positionB);
5552
r.solve(*ray, positionR, *shapeA, positionA);
5653

5754
if (m.colliding)
5855
{
59-
positionA.translate(-m.normal * m.depth);
56+
positionA -= m.normal * m.depth;
6057
}
6158
}
6259

0 commit comments

Comments
 (0)