hx3d  1
2D/3D Simple Game Framework
collision_matrix.cpp
1 /*
2  2D physics collision matrix.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/physics/2d/collision_matrix.hpp"
22 
23 #include "hx3d/utils/log.hpp"
24 
25 namespace hx3d {
26 namespace physics2d {
27 
28 CollisionMatrix::CollisionMatrix() {
29  _currentCategory = 0x1;
30 }
31 
32 void CollisionMatrix::addCategory(std::string name) {
33  if (_categories.find(name) != _categories.end()) {
34  Log.Error("Physics2D: Category `%s` already exists !", name.c_str());
35  return;
36  }
37 
38  _categories[name] = _currentCategory;
39  _masks[name] = 0x0;
40  _currentCategory <<= 0x1;
41 }
42 
43 void CollisionMatrix::addOneMask(std::string name, std::string category) {
44  if (_categories.find(name) == _categories.end()) {
45  Log.Error("Physics2D: Category `%s` does not exists !", name.c_str());
46  return;
47  }
48 
49  if (_categories.find(category) == _categories.end()) {
50  Log.Error("Physics2D: Category `%s` does not exists !", category.c_str());
51  return;
52  }
53 
54  unsigned int mask = _masks[name];
55  unsigned int cat = _categories[name];
56  unsigned int otherCategory = _categories[category];
57  unsigned int otherMask = _masks[category];
58 
59  mask |= otherCategory;
60  otherMask |= cat;
61 
62  _masks[name] = mask;
63  _masks[category] = otherMask;
64 }
65 
66 unsigned int CollisionMatrix::getCategory(std::string category) {
67  if (_categories.find(category) == _categories.end()) {
68  Log.Error("Physics2D: Category `%s` does not exists !", category.c_str());
69  return 0;
70  }
71 
72  return _categories[category];
73 }
74 
75 unsigned int CollisionMatrix::getMask(std::string category) {
76  if (_categories.find(category) == _categories.end()) {
77  Log.Error("Physics2D: Category `%s` does not exists !", category.c_str());
78  return 0;
79  }
80 
81  return _masks[category];
82 }
83 
84 } /* physics2d */
85 } /* hx3d */
hx3d framework namespace
Definition: audio.hpp:26
unsigned int getCategory(std::string category)
Get a category bit from a name.
unsigned int getMask(std::string category)
Get a mask bit from a name.
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
void addCategory(std::string name)
Add a category.