coll2d.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "Crown.h"
  2. #include <cstdlib>
  3. #include <GL/glew.h>
  4. using namespace Crown;
  5. void DrawCircle(const Circle& circle);
  6. void DrawLine(const Vec2& start, const Vec2& end);
  7. void DrawRectangle(const Crown::Rect& rect, const Color4& color);
  8. class WndCtrl: public KeyboardListener
  9. {
  10. public:
  11. WndCtrl()
  12. {
  13. GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
  14. }
  15. virtual void KeyReleased(const KeyboardEvent& event)
  16. {
  17. if (event.key == KC_ESCAPE)
  18. {
  19. GetDevice()->StopRunning();
  20. }
  21. }
  22. };
  23. class MainScene: public Scene, public MouseListener//, public KeyboardListener
  24. {
  25. public:
  26. MainScene(uint windowWidth, uint windowHeight) :
  27. c1(Vec2::ZERO, 0.0f),
  28. c2(Vec2::ZERO, 0.0f)
  29. {
  30. GetDevice()->GetInputManager()->RegisterMouseListener(this);
  31. //GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
  32. }
  33. virtual ~MainScene()
  34. {
  35. }
  36. virtual void ButtonPressed(const MouseEvent& event)
  37. {
  38. }
  39. virtual void OnLoad()
  40. {
  41. Crown::Renderer* renderer = Crown::GetDevice()->GetRenderer();
  42. renderer->SetClearColor(Crown::Color4(0.6f, 0.6f, 0.6f, 1.0f));
  43. c1.SetCenter(Vec2(300.0f, 300.0f));
  44. c1.SetRadius(32.0f);
  45. c2.SetCenter(Vec2(500.0f, 700.0f));
  46. c2.SetRadius(16.0f);
  47. ortho.BuildProjectionOrtho2dRH(1000, 625, 0, 10);
  48. }
  49. virtual void RenderScene()
  50. {
  51. Renderer* renderer = GetDevice()->GetRenderer();
  52. renderer->_SetLighting(false);
  53. renderer->_SetBlending(false);
  54. renderer->_SetAlphaTest(false);
  55. renderer->_SetBackfaceCulling(false);
  56. renderer->_SetTexturing(0, false);
  57. renderer->_SetTexturing(1, false);
  58. renderer->_SetTexturing(2, false);
  59. Scene::RenderScene();
  60. renderer->SetMatrix(MT_PROJECTION, ortho);
  61. glDisable(GL_CULL_FACE);
  62. glColor4f(0, 1, 0, 1);
  63. Crown::Rect r1;
  64. r1.SetFromCenterAndDimensions(Vec2(400, 400), 100, 80);
  65. c1.SetCenter(GetDevice()->GetInputManager()->GetMouse()->GetCursorXY().ToVec2());
  66. Color4 color = Color4::LIME;
  67. Vec2 penetration;
  68. if (Intersection::TestCircleRect(c1, r1, penetration))
  69. {
  70. color = Color4::RED;
  71. }
  72. Circle cr1 = r1.ToCircle();
  73. DrawCircle(cr1);
  74. Rect rc1 = c1.ToRect();
  75. DrawRectangle(rc1, color);
  76. DrawRectangle(r1, color);
  77. DrawCircle(c1);
  78. glColor4f(0, 0, 1, 1);
  79. DrawLine(c1.GetCenter(), c1.GetCenter() + penetration);
  80. //DrawCircle(c2);
  81. //Vec2 c1vel = GetDevice()->GetInputManager()->GetMouse()->GetCursorXY().ToVec2() - c1.GetCenter();
  82. //Vec2 c2vel = Vec2(250.0f, -200.0f);
  83. //DrawLine(c1.GetCenter(), c1.GetCenter() + c1vel);
  84. //DrawLine(c2.GetCenter(), c2.GetCenter() + c2vel);
  85. //real ct;
  86. //if (Intersection::TestDynamicCircleCircle(c1, c1vel, c2, c2vel, ct))
  87. //{
  88. // glColor4f(1, 0, 0, 1);
  89. // Circle cc1(c1.GetCenter() + c1vel * ct, c1.GetRadius());
  90. // Circle cc2(c2.GetCenter() + c2vel * ct, c2.GetRadius());
  91. // DrawCircle(cc1);
  92. // DrawCircle(cc2);
  93. //}
  94. }
  95. private:
  96. Circle c1;
  97. Circle c2;
  98. Mat4 ortho;
  99. };
  100. int main(int argc, char** argv)
  101. {
  102. int wndW = 1000;
  103. int wndH = 625;
  104. if (argc == 3)
  105. {
  106. wndW = atoi(argv[1]);
  107. wndH = atoi(argv[2]);
  108. }
  109. Device* mDevice = GetDevice();
  110. if (!mDevice->Init(wndW, wndH, 32, false))
  111. {
  112. return 0;
  113. }
  114. WndCtrl ctrl;
  115. MainScene* mainScene = new MainScene(wndW, wndH);
  116. GetDevice()->GetSceneManager()->SelectNextScene(mainScene);
  117. mDevice->GetMainWindow()->SetTitle("Crown Engine v0.1 - Collision 2D Test");
  118. mDevice->Run();
  119. mDevice->Shutdown();
  120. return 0;
  121. }
  122. void DrawCircle(const Circle& circle)
  123. {
  124. float step = (360.0f / 16.0f) * Math::DEG_TO_RAD;
  125. glBegin(GL_TRIANGLE_FAN);
  126. glVertex2fv(circle.GetCenter().ToFloatPtr());
  127. for (int i = 0; i <= 16; i++)
  128. {
  129. Vec2 vert = circle.GetCenter() + (Vec2(Math::Cos((float)i * step), Math::Sin((float)i * step)) * circle.GetRadius());
  130. glVertex2fv(vert.ToFloatPtr());
  131. }
  132. glEnd();
  133. }
  134. void DrawLine(const Vec2& start, const Vec2& end)
  135. {
  136. glBegin(GL_LINES);
  137. glVertex2fv(start.ToFloatPtr());
  138. glVertex2fv(end.ToFloatPtr());
  139. glEnd();
  140. }
  141. void DrawRectangle(const Crown::Rect& rect, const Color4& color)
  142. {
  143. Renderer* r = GetDevice()->GetRenderer();
  144. Vec2 min = rect.GetVertext(0);
  145. Vec2 size = rect.GetSize();
  146. r->DrawRectangle(Point2(min.x, min.y), Point2(size.x, size.y), DM_BORDER, color);
  147. }