SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Collision Detection. › Ответ в теме: Collision Detection.
А вот и код, который я имею, взгляните, может подскажите чего:
C++
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
#include <SFML\Graphics.hpp> using namespace sf; using namespace std; bool intersects (const RectangleShape & rect1,const RectangleShape & rect2) { FloatRect r1=rect1.getGlobalBounds(); FloatRect r2=rect2.getGlobalBounds(); return r1.intersects (r2); } int clamp (const int x, const int a, const int b) { return min(max(a,x),b); } int main() { const int width = 640; const int height= 480; Texture back; if(!back.loadFromFile("background.jpg")) return -1; Sprite background; background.setTexture(back); background.setPosition(0,0); VideoMode videoMode(width, height); ContextSettings antialiasing; antialiasing.antialiasingLevel = 8; RenderWindow window(videoMode,"Rectangle Collision", Style::Default, antialiasing); //walls RectangleShape top; RectangleShape left; RectangleShape right; //RectangleShape bottom; //bricks RectangleShape brick1; brick1.setSize(Vector2f(60,12)); brick1.setPosition(40,40); brick1.setFillColor(Color(255,255,255,128)); brick1.setOutlineColor(Color::Black); brick1.setOutlineThickness(3); RectangleShape brick2; brick2.setSize(Vector2f(60,12)); brick2.setPosition(150,40); brick2.setFillColor(Color(255,255,255,128)); brick2.setOutlineColor(Color::Black); brick2.setOutlineThickness(3); RectangleShape brick3; brick3.setSize(Vector2f(60,12)); brick3.setPosition(260,40); brick3.setFillColor(Color(255,255,255,128)); brick3.setOutlineColor(Color::Black); brick3.setOutlineThickness(3); RectangleShape brick4; brick4.setSize(Vector2f(60,12)); brick4.setPosition(370,40); brick4.setFillColor(Color(255,255,255,128)); brick4.setOutlineColor(Color::Black); brick4.setOutlineThickness(3); RectangleShape brick5; brick5.setSize(Vector2f(60,12)); brick5.setPosition(480,40); brick5.setFillColor(Color(255,255,255,128)); brick5.setOutlineColor(Color::Black); brick5.setOutlineThickness(3); RectangleShape paddle; top.setPosition(0, 0); top.setSize(Vector2f(640, 5)); top.setFillColor(Color(0,151,255)); top.setOutlineColor(Color(0,151,255)); /*bottom.setPosition(0, 475); bottom.setSize(Vector2f(640, 5)); bottom.setFillColor(Color(0,151,255)); bottom.setOutlineColor(Color(0,151,255));*/ left.setPosition(0, 0); left.setSize(Vector2f(5, height)); left.setFillColor(Color(0,151,255)); left.setOutlineColor(Color(0,151,255)); right.setPosition(635,0); right.setSize(Vector2f(5, height)); right.setFillColor(Color(0,151,255)); right.setOutlineColor(Color(0,151,255)); //paddle paddle.setPosition(width/2-45, height-50-12); paddle.setSize(Vector2f(90, 12)); paddle.setFillColor(Color::Red); paddle.setOutlineColor(Color::Green); paddle.setOutlineThickness(3); //ball CircleShape ball(10); ball.setPosition(width/2, height/2); ball.setFillColor(Color(255,255,255,128)); ball.setOutlineColor(Color::Yellow); ball.setOutlineThickness(3); Vector2<float> ballSpeed(0.1,0.1); Time deltaTime; Clock clock; while (window.isOpen()) { deltaTime = clock.restart(); float dtAsSeconds = deltaTime.asSeconds(); window.clear(Color::White); window.draw(background); window.draw(top); /*window.draw(bottom);*/ window.draw(left); window.draw(right); window.draw(brick1); window.draw(brick2); window.draw(brick3); window.draw(brick4); window.draw(brick5); window.draw(paddle); window.draw(ball); window.display(); Event event; while (window.pollEvent(event)) { if ( (event.type == Event::Closed) || ((event.type == Event::KeyPressed) && (event.key.code==Keyboard::Escape)) ) window.close(); } //move paddle if(Keyboard::isKeyPressed(Keyboard::Left)) { paddle.move(-5/50.0, 0); } else if (Keyboard::isKeyPressed(Keyboard::Right)) { paddle.move(5/50.0, 0); } //keep paddle inside the window if (intersects(paddle, left) || intersects(paddle, right)) { FloatRect l = left.getGlobalBounds(); FloatRect r = right.getGlobalBounds(); Vector2f p = paddle.getPosition(); p.x = clamp(p.x, l.left + l.width+5, r.left-paddle.getGlobalBounds().width-5); paddle.setPosition(p.x, p.y); } } return EXIT_SUCCESS; } |