Advertisement

collision

Started by January 07, 2017 11:51 PM
77 comments, last by jpetrie 7 years, 8 months ago

well I guess I should go back to working on my original program, is this a good idea?

I suggest this approach to you not because I think it's a good way to learn or deal with collision detection but because I think it follows how you've initially approached the problem of making a breakout game (if I understand correctly from the code you've posted). And I think there may be value to you in following that. I'll note that I'm still working on getting my collision detection to do exactly what I want it to do so I'm not an expert but I hope this helps.

If your block array is 8 * 3 then I'm going to assume your blocks are 80 * 32 pixels on the screen and that your screen size is 640 * 480 pixels. And with the code you've posted I'm assuming you intend that when an array element is 1 then a block is there and when it is 0 there is no block. This all sounds fine to me for a beginner project and is how I might've approached it when I was starting out. You certainly could have a class that describes various attributes of a block and have an 8 * 3 array of those block objects but, for a beginner project, simply tracking whether a block is on or off is sufficient.

Let's say your ball (or bullet) is only 1 * 1 pixel. Maybe it's actually 5*5 or something else but to start with just work with it as a single point. For wherever the ball currently is, determine which block it's at. Get the value at the corresponding array element. For example, if your ball is at 247,82 then you want what's in the array at [3][2]. Just make sure you don't try to get things outside the range of your array (besides, if it's outside your array's range then you know you haven't collided with a block). With the value from the array you can decide whether you want to let the ball continue or if you want to change its direction or speed or if there's anything else you'd like to do to the ball, the block, the player, or anything that's in your game.

With a breakout game, it's possible that the speed of your ball or bullet might be a concern. If the speed gets high enough, it's possible that your ball could travel through a block without a collision being detected. Start with having the ball move more slowly to make sure the basic collision detection is working.

It won't be perfect collision detection at this point but I think you should be able to see some progress at this point that looks a lot like the way breakout should work.

Next, take a look at figuring out what to do so that instead of treating your ball as a single point you're taking into account its actual size. Instead of trying to determine if just a point is within a rectangle, you will be trying to find out if any part of one rectangle is in another. You could create functions and do calculations for this with your project as it is but it might good (or more convenient) at this point to have the additional information about an individual block's location and size in a class. You'll probably want a class for the ball too but don't keep that in the block array. Things that you need to keep in mind include that it's possible for the ball to collide with more than one block at the same time and you also need to deal with all the directions that the ball could travel (at least each direction on the x & y axes to start).

In general, collision detection gets more tricky as speeds increase, as sizes change, or with different sizes and shapes of things, or as you get more and more moving objects. These things can require their own specialized solutions each with their own trade-offs that may or may not be transferable to other projects.

Good luck.

Advertisement

well thanks for all the input

well I am still reading The OpenGL Programming Guide 7th Ed., Sometimes this helps me to understand programming principles as applied to OpenGL and a little C++.

well I am still reading The OpenGL Programming Guide 7th Ed., Sometimes this helps me to understand programming principles as applied to OpenGL and a little C++.

To be honest, this is probably a bad book for you. It assumes a level of programming knowledge that you don't yet possess. You need to find a book (or other resource) that covers the basics of programming before you tackle something like OpenGL. This would probably explain some of the problems you're having. You're trying to run without learning how to take a step first.

I also have a book called Sams Teach Yourself C+ in One Day which has 29 chapters on c++. it has exercises in the backs of the chapters which I like to work on.

Advertisement

I also have a book called Sams Teach Yourself C+ in One Day which has 29 chapters on c++. it has exercises in the backs of the chapters which I like to work on.

Yep. That's a much better place to work at for now. It'll get you "thinking" in C++, then you can start to focus on thinking about the problem you're trying to solve because you don't need to focus on the language itself any more.

edit:

If you can find any language-independent programming books or tutorials, they would be good to read as well, to get a good understanding of concepts like iteration, conditional statements, recursion, data structures, abstraction, etc.

Don't worry if this seems like a detour that might take some time. And it will take some time, that is true. But it will save you a lot more time in the long run.

thanks for the posting lenny

is the C++ Primer book a good one to get?

Yes, but you should try to get the 6th 5th edition if possible rather than an older printing; any edition will give good basic knowledge, but the latest edition covers the updated C++ standard.

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement