Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


boundaries & collision detection

This topic is 5248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

m making a breakout clone and i have the boucning ball setup but im having trouble coming up with a way to have the game end when the ball hits the bottom of the screen, and if you have any suggestions of how to make the collison detrction for the paddle would be a great help ___________________________________________ i had this in mine for the detection 1|2 --- // The ball 4|3 +-------------+ |1 |2 //The Paddle |4 |3 +-------------+ so like when one of the corners hit it will bounce back ___________________________________________ //The Boucning ball #include <iostream> #include <math.h> #include <thagraphix.h> using namespace std; const double pi = 3.1415926; const long DT = 1; int main() { string title("Arkanoid"); cout << "Loading Arkanoid 2004"; int cx = 50, cy = 20, cr = 7; int vx = 3; int vy = 5; unsigned long time1, time2; Style mystyle = nostyle(Color::RED); Window test(640,680); test.disableAutoPageFlip(); time1 = WindowManager::getTime( ); while(true) { if(!test.isKeyboardQueueEmpty()) KeyboardEvent event = test.getKeyboardEvent(); if(KeyboardEvent(NamedKey::ESCAPE) == event) { return 0; } } time2 = WindowManager::getTime(); int widx, widy; widx = test.getWidth(); widy = test.getHeight(); if ((time2 - time1) > DT) { if (vx > 0) { if ((cx + cr + vx) > widx) { vx = - vx; } } else { if ((cx - cr + vx) < 0) { vx = -vx; } } cx += vx; if (vy > 0) { if ((cy + cr + vy) > widy) { vy = - vy; } } else { if ((cy - cr + vy) < 0) { vy = -vy; } } cy += vy; time1 = time2; } //if (time2 - time1) test.drawCircleFilled(mystyle,cx,cy,cr); test.flipPage(); // erase window, fill with white for next flip test.drawRectangleFilled(Style::BLACK, 0, 0, widx, widy); } // while (true) return 0; }

Share this post

Link to post
Share on other sites
Ball against bottom of screen is dirt simple, just check the balls y or y+height (whichever works for your coordinate system) and end it if the test is true

Collisions can be done a couple ways. One way would be to test if the rectangles containing the 2 objects are intersecting in any way (compare the left, right, top,and bottom edges of each rectangle) and then find the edges that collided by comparing the differences. (right2-left1 or left2-right1 or top1-bottom2 and so on and so forth). One you have the edge that was hit (or even the corner that was hit) you can flip the ball delta on either the x or y axis appropriatly.

Other methods involve more compilcated stuff, like vector math and linear algebra. If you can''t get anything to work and I miss your frustrated replies here then do a google on "collision detection" or "2d swept circles" or something

Life would be so much easier if we could just get the source code.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!