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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
#include <SFML\Graphics.hpp> #include <iostream> using namespace sf; using namespace std; /*AABB-Circle Distance Step one: Vector dist = B.centre - A.centre Step two: clamp dist to A.extends if(dist.x >= 0)clamp.x = min(dist.x,extents.x) if(dist.x < 0) clamp.x = max(dist.x,-extents.x) if(dist.y >= 0)clamp.y = min(dist.y,extents.x) if(dist.y < 0) clamp.y = max(dist.y,-extents.x) Step three: Take the difference diff = dist - clamp Final step: Find the distance between A and B Distance = diff.magnitude - B.radius*/ void updateBallPhysics(); 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); } bool win(); int main() { const int width = 640; const int height= 480; Font font; if(!font.loadFromFile("assets/comic.ttf")) cout << "ERROR" << endl; Texture back; if(!back.loadFromFile("assets/background.jpg")) return -1; Texture redball; if(!redball.loadFromFile("assets/ball.png")) 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; //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); //paddle RectangleShape 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 RectangleShape ball; ball.setPosition(width/2, height/2); ball.setSize(Vector2f(20,20)); //ball.setOutlineColor(Color(255,255,255,128)); //ball.setFillColor(Color(255,255,255,128)); //ball.setOutlineThickness(1); ball.setTexture(&redball); Vector2<float> ballSpeed(0.15, 0.15); top.setPosition(0, 0); top.setSize(Vector2f(640, 5)); top.setFillColor(Color(0,151,255)); top.setOutlineColor(Color(0,151,255)); top.setFillColor(Color::Yellow); bottom.setPosition(0, 475); bottom.setSize(Vector2f(640, 5)); bottom.setFillColor(Color(0,151,255)); bottom.setOutlineColor(Color(0,151,255)); bottom.setFillColor(Color::Yellow); left.setPosition(0, 0); left.setSize(Vector2f(5, height)); left.setFillColor(Color(0,151,255)); left.setOutlineColor(Color(0,151,255)); left.setFillColor(Color::Yellow); right.setPosition(635,0); right.setSize(Vector2f(5, height)); right.setFillColor(Color(0,151,255)); right.setOutlineColor(Color(0,151,255)); right.setFillColor(Color::Yellow); Text gamename, start, won, lost; gamename.setFont(font); gamename.setString("Arkanoid"); gamename.setCharacterSize(48); gamename.setColor(Color::Red); gamename.setPosition(210, 100); //start start.setFont(font); start.setString("Press SPACE to start"); start.setCharacterSize(32); start.setColor(Color::Green); start.setPosition(165, 200); //won won.setFont(font); won.setString("You won!"); won.setCharacterSize(32); won.setStyle(Text::Bold); won.setColor(Color::Green); won.setPosition(250, 200); //lost lost.setFont(font); lost.setString("Game Over!"); lost.setCharacterSize(24); lost.setStyle(Text::Bold); lost.setColor(Color::Red); lost.setPosition(250, 200); //GameStates enum GameStates {STARTSCREEN, GAME, WIN, GAMEOVER}; int gameState = STARTSCREEN; while (window.isOpen()) { window.clear(Color::White); switch(gameState) { case STARTSCREEN: window.draw(background); window.draw(gamename); window.draw(start); break; case GAME: 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); break; case WIN: window.draw(background); window.draw(won); break; case GAMEOVER: window.draw(background); window.draw(lost); break; } window.display(); Event event; while (window.pollEvent(event)) { if ( (event.type == Event::Closed) || ((event.type == Event::KeyPressed) && (event.key.code==Keyboard::Escape)) ) window.close(); else if((event.type == Event::KeyPressed) && (event.key.code==Keyboard::Space) && (gameState == STARTSCREEN)) gameState = GAME; } if(gameState!=GAME) continue; ball.move(ballSpeed.x, ballSpeed.y); //move paddle if(Keyboard::isKeyPressed(Keyboard::Left)) { paddle.move(-0.3, 0); } else if (Keyboard::isKeyPressed(Keyboard::Right)) { paddle.move(0.3, 0); } //keep paddle inside the window if (intersects(paddle, left) || intersects(paddle, right)) { FloatRect leftWall = left.getGlobalBounds(); FloatRect rightWall = right.getGlobalBounds(); Vector2f p = paddle.getPosition(); p.x = clamp(p.x, leftWall.left + leftWall.width+5, rightWall.left-paddle.getGlobalBounds().width-5); paddle.setPosition(p.x, p.y); } //ball collides with walls if(intersects(ball, top)) { ballSpeed.y = -ballSpeed.y; } if(intersects(ball, left) || intersects(ball, right)) { ballSpeed.x = -ballSpeed.x; } //ball collides with paddle if(intersects(ball, paddle)) { ballSpeed.y= -ballSpeed.y; } //ball collides with bricks if(intersects(ball, brick1)) { FloatRect b1 = brick1.getGlobalBounds(); FloatRect bBall = ball.getGlobalBounds(); brick1.move(width+1, height+1); ballSpeed.y= -ballSpeed.y; } if(intersects(ball, brick2)) { FloatRect b2 = brick2.getGlobalBounds(); FloatRect bBall = ball.getGlobalBounds(); brick2.move(width+1, height+1); ballSpeed.y= -ballSpeed.y; } if(intersects(ball, brick3)) { FloatRect b3 = brick3.getGlobalBounds(); FloatRect bBall = ball.getGlobalBounds(); brick3.move(width+1, height+1); ballSpeed.y= -ballSpeed.y; } if(intersects(ball, brick4)) { FloatRect b4 = brick4.getGlobalBounds(); FloatRect bBall = ball.getGlobalBounds(); brick4.move(width+1, height+1); ballSpeed.y= -ballSpeed.y; } if(intersects(ball, brick5)) { FloatRect b5 = brick5.getGlobalBounds(); FloatRect bBall = ball.getGlobalBounds(); brick5.move(width+1, height+1); ballSpeed.y= -ballSpeed.y; } //Game won if(brick1.getPosition().x > width && brick2.getPosition().x > width && brick3.getPosition().x > width && brick4.getPosition().x > width && brick5.getPosition().x > width) { gameState = WIN; } //game over if(ball.getPosition().y > 480) { gameState = GAMEOVER; } } return EXIT_SUCCESS; } |