-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.cpp
82 lines (69 loc) · 1.71 KB
/
ball.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "ball.h"
ball::ball(float r, sf::Vector2f position, b2World &world, std::string src_img)
{
_forceApply = false;
//SFML init
_texture = new sf::Texture;
_texture->loadFromFile(src_img);
setTexture(_texture);
setRadius(r);
setPosition(position);
setOrigin(r, r);
//Box2D init
_b2_ball.position.Set(pxtom(position.x - WIDTH / 2), pxtom(position.y - HEIGHT / 2)*-1);
_b2_ball.type = b2_dynamicBody;
_b2_ball.bullet = true;
_b2_ball_body = world.CreateBody(&_b2_ball);
_b2_shape.m_radius = pxtom(r);
_b2_fix.shape = &_b2_shape;
_b2_fix.density = 1.f;
_b2_fix.restitution = 1.f;
_b2_fix.friction = 0.f;
_b2_ball_body->CreateFixture(&_b2_fix);
}
ball::ball(float r, sf::Vector2f position, b2World & world, sf::Texture & src_tex)
{
_forceApply = false;
//SFML init
_texture = &src_tex;
setTexture(_texture);
setRadius(r);
setPosition(position);
setOrigin(r, r);
//Box2D init
_b2_ball.position.Set(pxtom(position.x - WIDTH / 2), pxtom(position.y - HEIGHT / 2)*-1);
_b2_ball.type = b2_dynamicBody;
_b2_ball.bullet = true;
_b2_ball_body = world.CreateBody(&_b2_ball);
_b2_shape.m_radius = pxtom(r);
_b2_fix.shape = &_b2_shape;
_b2_fix.density = 1.f;
_b2_fix.restitution = 1.f;
_b2_fix.friction = 0.f;
_b2_ball_body->CreateFixture(&_b2_fix);
}
ball::~ball()
{
}
bool ball::isForceApply()
{
return _forceApply;
}
void ball::ApplyForce(b2Vec2 force, b2Vec2 point, bool wake)
{
if (_forceApply)
return;
_b2_ball_body->ApplyForce(force, point, wake);
_forceApply = true;
}
void ball::update_pos()
{
b2Vec2 pos = _b2_ball_body->GetPosition();
pos.x = mtopx(pos.x) + WIDTH / 2;
pos.y = mtopx(-1 * pos.y) + HEIGHT / 2;
setPosition(pos.x, pos.y);
}
b2Body* ball::getBody()
{
return _b2_ball_body;
}