You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ugeentity.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "ugeentity.h"
  2. #include "utils/tools.h"
  3. UGEEntity::UGEEntity(UGameEngine *engine, QObject *parent)
  4. : QObject(parent)
  5. , _engine(engine)
  6. {
  7. init();
  8. }
  9. UGEEntity::UGEEntity(UGameEngine *engine)
  10. : QObject(engine)
  11. , _engine(engine)
  12. {
  13. init();
  14. }
  15. UGEEntity::~UGEEntity()
  16. {
  17. _engine->removeEntity(this);
  18. }
  19. Vector3D UGEEntity::getPosition() const
  20. {
  21. return _position;
  22. }
  23. void UGEEntity::setPosition(const Vector3D &position)
  24. {
  25. _position = position;
  26. emit positionChanged();
  27. emit positionChanged(_position);
  28. }
  29. void UGEEntity::move(const Vector3D &move)
  30. {
  31. _position += move;
  32. emit positionChanged();
  33. emit positionChanged(_position);
  34. }
  35. //Vector3D UGEEntity::getSpeed() const
  36. //{
  37. // return _speed;
  38. //}
  39. //void UGEEntity::setSpeed(const Vector3D &speed)
  40. //{
  41. // _speed = speed;
  42. //}
  43. //void UGEEntity::accelerate(const Vector3D &speed)
  44. //{
  45. // _speed += speed;
  46. //}
  47. Vector3D UGEEntity::getRotation() const
  48. {
  49. return _rotation;
  50. }
  51. void UGEEntity::setRotation(const Vector3D &rotation)
  52. {
  53. _rotation = Tools::normalizeAngle(rotation);
  54. emit rotationChanged();
  55. emit rotationChanged(rotation);
  56. }
  57. void UGEEntity::rotate(const Vector3D &rotation)
  58. {
  59. _rotation = Tools::normalizeAngle(rotation + _rotation);
  60. emit rotationChanged();
  61. emit rotationChanged(rotation);
  62. }
  63. Vector3D UGEEntity::getScale() const
  64. {
  65. return _scale;
  66. }
  67. void UGEEntity::setScale(const Vector3D &scale)
  68. {
  69. _scale = scale;
  70. emit scaleChanged();
  71. emit scaleChanged(_scale);
  72. }
  73. void UGEEntity::scale(const Vector3D &scale)
  74. {
  75. _scale += scale;
  76. emit scaleChanged();
  77. emit scaleChanged(_scale);
  78. }
  79. bool UGEEntity::isVisible() const
  80. {
  81. return _visible;
  82. }
  83. void UGEEntity::setVisible(bool visible)
  84. {
  85. _visible = visible;
  86. emit visibilityChanged();
  87. emit visibilityChanged(_visible);
  88. }
  89. void UGEEntity::show()
  90. {
  91. _visible = true;
  92. emit visibilityChanged();
  93. emit visibilityChanged(_visible);
  94. }
  95. void UGEEntity::hide()
  96. {
  97. _visible = false;
  98. emit visibilityChanged();
  99. emit visibilityChanged(_visible);
  100. }
  101. const QColor& UGEEntity::getColor() const
  102. {
  103. return _color;
  104. }
  105. void UGEEntity::setColor(const QColor &color)
  106. {
  107. _color = color;
  108. emit colorChanged();
  109. emit colorChanged(_color);
  110. }
  111. Vector3D UGEEntity::getRealPoint(const Vector3D &pos)
  112. {
  113. return (_tranformation.multMatrix(pos) + _position);
  114. }
  115. ColorVector3D UGEEntity::getRealPoint(const ColorVector3D &pos)
  116. {
  117. return ColorVector3D(pos.getColor(), getRealPoint((Vector3D)pos));
  118. }
  119. TextureVector3D UGEEntity::getRealPoint(const TextureVector3D &pos)
  120. {
  121. return TextureVector3D(pos.getTextureCoord(), getRealPoint((ColorVector3D)pos));
  122. }
  123. Matrix3x3 UGEEntity::getTransformationMatrix() const
  124. {
  125. return getRotationMatrix().multMatrix(getScaleMatrix());
  126. }
  127. Matrix3x3 UGEEntity::getScaleMatrix() const
  128. {
  129. Matrix3x3 scale;
  130. scale.setScalar(0, 0, _scale.getX());
  131. scale.setScalar(1, 1, _scale.getY());
  132. scale.setScalar(2, 2, _scale.getZ());
  133. return scale;
  134. }
  135. Matrix3x3 UGEEntity::getRotationMatrix() const
  136. {
  137. Vector3D r = Tools::degreeToRad(_rotation);
  138. Matrix3x3 mx;
  139. mx.setToIdentity();
  140. mx.setScalar(1, 1, cos(r.getX()));
  141. mx.setScalar(1, 2, -sin(r.getX()));
  142. mx.setScalar(2, 1, sin(r.getX()));
  143. mx.setScalar(2, 2, cos(r.getX()));
  144. Matrix3x3 my;
  145. my.setToIdentity();
  146. my.setScalar(0, 0, cos(r.getY()));
  147. my.setScalar(0, 2, sin(r.getY()));
  148. my.setScalar(2, 0, -sin(r.getY()));
  149. my.setScalar(2, 2, cos(r.getY()));
  150. Matrix3x3 mz;
  151. mz.setToIdentity();
  152. mz.setScalar(0, 0, cos(r.getZ()));
  153. mz.setScalar(0, 1, -sin(r.getZ()));
  154. mz.setScalar(1, 0, sin(r.getZ()));
  155. mz.setScalar(1, 1, cos(r.getZ()));
  156. return mx.multMatrix(my).multMatrix(mz);
  157. }
  158. void UGEEntity::drawPoint(AbstractRenderDevice *device, const ColorVector3D &point)
  159. {
  160. device->drawPoint(getRealPoint(point));
  161. }
  162. void UGEEntity::drawLine(AbstractRenderDevice *device, const ColorVector3D &begin, const ColorVector3D &end, double width)
  163. {
  164. device->drawLine(getRealPoint(begin), getRealPoint(end), width);
  165. }
  166. void UGEEntity::drawPolygon(AbstractRenderDevice *device, QList<ColorVector3D> points)
  167. {
  168. for (int i = 0; i < points.size(); ++i) {
  169. points[i] = getRealPoint(points[i]);
  170. }
  171. device->drawPolygon(points);
  172. }
  173. void UGEEntity::drawPolygonTexture(AbstractRenderDevice *device, QList<TextureVector3D> points, const QVariant &textureId)
  174. {
  175. for (int i = 0; i < points.size(); ++i) {
  176. points[i] = getRealPoint(points[i]);
  177. }
  178. device->drawPolygonTexture(points, textureId);
  179. }
  180. void UGEEntity::draw(AbstractRenderDevice *device)
  181. {
  182. onDraw(device);
  183. }
  184. void UGEEntity::update()
  185. {
  186. if (_needUpdate) {
  187. _tranformation = getTransformationMatrix();
  188. onUpdate();
  189. _needUpdate = false;
  190. }
  191. }
  192. void UGEEntity::onUpdate()
  193. {
  194. }
  195. void UGEEntity::needUpdate()
  196. {
  197. _needUpdate = true;
  198. }
  199. void UGEEntity::init()
  200. {
  201. connect(this, SIGNAL(positionChanged()), this, SLOT(needUpdate()));
  202. connect(this, SIGNAL(rotationChanged()), this, SLOT(needUpdate()));
  203. connect(this, SIGNAL(scaleChanged()), this, SLOT(needUpdate()));
  204. connect(this, SIGNAL(colorChanged()), this, SLOT(needUpdate()));
  205. _scale = Vector3D(1.0, 1.0, 1.0);
  206. _visible = true;
  207. _needUpdate =true;
  208. _zero = 0.001;
  209. _engine->addEntity(this);
  210. }
  211. Vector3D UGEEntity::getVectorNearestFaceIntersection(const Vector3D & p0, const Vector3D &p1, const Vector3D &p2, const Vector3D &vector, const Vector3D &pos, bool* ok){
  212. Vector3D v1 = p1 - p0;
  213. Vector3D v2 = p2 - p0;
  214. Vector3D n = v1.crossProduct(v2);
  215. double dotProduct = vector.dotProduct(n);
  216. if(isZero(dotProduct)){
  217. *ok = false;
  218. return Vector3D();
  219. }
  220. double l2 = n.dotProduct(p0 - pos) / dotProduct;
  221. if(-_zero > l2){
  222. *ok = false;
  223. return Vector3D();
  224. }
  225. *ok = true;
  226. Vector3D unit = (vector / vector.norm()) * l2;
  227. return pos + unit;
  228. }
  229. bool UGEEntity::isZero(double v){
  230. return v < _zero && v > -_zero;
  231. }