-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollisionobject.cpp
executable file
·137 lines (111 loc) · 3.12 KB
/
collisionobject.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "collisionobject.h"
CollisionObject::CollisionObject(b2World *w)
: world(w)
, body(NULL)
, fixture(NULL)
{
}
CollisionObject::~CollisionObject()
{
if (world && body && fixture)
{
body->DestroyFixture(fixture);
}
if (world && body)
{
world->DestroyBody(body);
}
Delete(pcName);
fixture = NULL;
body = NULL;
world = NULL;
}
void CollisionObject::Update(f32 dt)
{
}
void CollisionObject::Render()
{
#if DEBUG
//pRendererDevice->DrawRect(this->GetX(), this->GetY(), this->GetWidth(), this->GetHeight(), PIXEL_COLOR(255, 0, 255, 255));
#endif
/*if (body)
{
float pixelX = 1.0f / pScreen->GetWidth();
float pixelY = 1.0f / pScreen->GetHeight();
b2Vec2 pos = body->GetPosition();
if (body->GetType() == b2_dynamicBody)
{
pRendererDevice->DrawRect(pos.x * PIXEL2METER - GetWidth() * 0.5f, pos.y * PIXEL2METER * -1.0f - GetHeight() * 0.5f, GetWidth(), GetHeight(), PIXEL_COLOR(0, 255, 0, 255));
}
else
{
pRendererDevice->DrawRect(pos.x * PIXEL2METER - GetWidth() * 0.5f, pos.y * PIXEL2METER * -1.0f - GetHeight() * 0.5f, GetWidth(), GetHeight(), PIXEL_COLOR(0, 0, 255, 255));
}
pRendererDevice->DrawRect(pos.x * PIXEL2METER - 5.0f * pixelX, pos.y * PIXEL2METER * -1.0f - 5.0f * pixelY, pixelX * 10.0f, pixelY * 10.0f, PIXEL_COLOR(255, 255, 0, 255), true);
}
else
{
pRendererDevice->DrawRect(GetX(), GetY(), GetWidth(), GetHeight(), PIXEL_COLOR(255, 0, 0, 255));
}*/
}
void CollisionObject::SetName(const char* name)
{
pcName = New(char[strlen(name) + 1]);
sprintf((char*)pcName, "%s", name);
}
void CollisionObject::CreateDinamycBody(f32 x, f32 y, f32 width, f32 height, u16 category, u16 mask)
{
this->CreateBody(x, y, width, height, true, category, mask);
}
void CollisionObject::CreateStaticBody(f32 x, f32 y, f32 width, f32 height)
{
this->CreateBody(x, y, width, height, false);
}
void CollisionObject::CreateBody(f32 x, f32 y, f32 width, f32 height, bool dynamic, u16 category, u16 mask)
{
f32 sizeX = pScreen->GetWidth() * PIXEL2METER;
f32 sizeY = pScreen->GetHeight() * PIXEL2METER;
f32 b2x = x * pScreen->GetWidth() / sizeX;
f32 b2y = -(y * pScreen->GetHeight() / sizeY);
f32 b2w = width * pScreen->GetWidth() / sizeX;
f32 b2h = height * pScreen->GetHeight() / sizeY;
if (dynamic)
{
bodyDef.type = b2_dynamicBody;
}
else
{
bodyDef.type = b2_staticBody;
}
bodyDef.position.Set(b2x + b2w * 0.5f, b2y + b2h * 0.5f);
bodyDef.fixedRotation = true;
if (!body)
{
body = world->CreateBody(&bodyDef);
}
else
{
body->DestroyFixture(fixture);
body->SetType(bodyDef.type);
}
shape.SetAsBox(b2w * BOXSIZE, b2h * BOXSIZE);
fixtureDef.shape = &shape;
if (dynamic)
{
fixtureDef.density = 0.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.3f;
fixtureDef.filter.categoryBits = category;
fixtureDef.filter.maskBits = mask;
}
else
{
fixtureDef.density = 0.0f;
fixtureDef.friction = 0.05f;
fixtureDef.restitution = 0.0f;
fixtureDef.filter.categoryBits = COLLISION_GROUND;
fixtureDef.filter.maskBits = COLLISION_ALL;
}
fixture = body->CreateFixture(&fixtureDef);
body->SetUserData(this);
}