-
Notifications
You must be signed in to change notification settings - Fork 2
/
Box.cpp
60 lines (50 loc) · 1.7 KB
/
Box.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
#include "Box.h"
#include "Manager.h"
Box::Box(b2World& world, int x, int y, float density)
:mSprite(density>1?Manager::LoadImage("data/steel_box_texture.jpg"):Manager::LoadImage("data/box_texture.jpg"))
,mWorld(world)
{
mSprite.setOrigin(36,36);
mSprite.setPosition(x,y);
//mSprite.scale(0.4,0.4);
b2Vec2 mVertices[4];
mVertices[0].x = -36/METERTOPIXEL;
mVertices[1].x = 36/METERTOPIXEL;
mVertices[2].x = 36/METERTOPIXEL;
mVertices[3].x = -36/METERTOPIXEL;
mVertices[0].y = -36/METERTOPIXEL;
mVertices[1].y = -36/METERTOPIXEL;
mVertices[2].y = 36/METERTOPIXEL;
mVertices[3].y = 36/METERTOPIXEL;
bodyDef.position.Set(x/METERTOPIXEL, y/METERTOPIXEL);
bodyDef.type = b2_dynamicBody;
body = world.CreateBody(&bodyDef);
b2PolygonShape triangleDef;
triangleDef.Set(mVertices,4);
cartFixture.density = density;
cartFixture.friction = 1.0;
cartFixture.restitution = 0.5;
cartFixture.shape = &triangleDef;
body->CreateFixture(&cartFixture);
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(x/METERTOPIXEL, y/METERTOPIXEL);
// Define another box shape for our dynamic body.
mSprite.setPosition(body->GetPosition().x*METERTOPIXEL,body->GetPosition().y*METERTOPIXEL);
lastAngle = body->GetAngle()* (180.0f / 3.141569);
//ctor
}
void Box::Update()
{
mSprite.setPosition(body->GetPosition().x*METERTOPIXEL, body->GetPosition().y*METERTOPIXEL);
if(body->GetAngle()* (180.0f / 3.141569f)!= lastAngle)
{
mSprite.rotate(-lastAngle + body->GetAngle() * (180.0f / 3.141569));
lastAngle = body->GetAngle()* (180.0f / 3.141569);
}
}
Box::~Box()
{
mWorld.DestroyBody(body);
body = NULL;
//dtor
}