• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phil67rpg

2d collision

2 posts in this topic

well I am building a breakout game using c#.I am able to get the ball to bounce off of the paddle and walls, but I am confused about how to get it to bounce off the bricks.I have done alot of research on this topic and I am kind of lost.there is sure alot of info. on the net. here is the code I am using.


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (e.KeyChar == ([/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]char[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"])32)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]


[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Image[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] curImage17 = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Image[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].FromFile([/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]@"c:\Users\phil\Desktop\ball.bmp"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawImage(curImage17, x + vx, y + vy);[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]x += vx;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]y += vy;[/size][/font][/size][/font]



[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (y >= bottom_wall || y<=top_wall)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]vy = -vy;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (x >= right_wall || x <= left_wall)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]vx = -vx;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ((x >= paddle_left+i && x <= paddle_right+i) && (y >= paddle_top ))[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]vx = -vx;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]vy = -vy;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]this is probably the hardest part of this game,I am doing the best I can to solve on my own, but another pair of eyes is always appreciated.[/size][/font][/size][/font]
0

Share this post


Link to post
Share on other sites
I see a few problems where you ignore how fast the ball might be moving.

"If the ball is beyond the left wall, reverse horizontal direction". What if the ball is STILL beyond the left wall the next frame? If all your values are integers that should be impossible right now, but if at any point you decide to add a random variance to the balls velocity on collision it becomes a possibility.

Your paddle collision only works if vy will never grow larger than your paddle height or the ball can simply move right through it. Taking that into account means you have to stop just looking at snapshots and check for intersection in single frames. In other words, moving from intersection detection to actual collision detection. For now I'd simply stick with it and make sure the ball won't get too fast.

I'm also not sure why you think that the blocks are so much different from the paddle. You already treat your ball as a point and not as a sphere, which is making things a lot easier, since all you have to do is "is this point inside this rectangle". Unless you start being fancy with wildly rotated or weirdly shaped blocks, that is exactly like your paddle (except that you can't just skip the lower edge).
0

Share this post


Link to post
Share on other sites
You should not mix rendering and game logic like that. It is a great way to get bugs. Instead, having one function which moves the objects and manages collision, and another function which draws the current state of the game.

Images are Disposable, which means that loading and discarding them like that is a resource leak. You should load the image exactly once at the start of your game, and it should be deallocated properly as the game shuts down.

The variable name "currImage17" is poor, better would be "ballImage". The variable name "i" is terrible. It appears to be the paddle's current x position. Thus, paddleX would be a better name.

You are "confused about how to get it to bounce off the bricks", but you haven't shown how you are modelling the bricks.


For a simple breakout clone, you have a set of fixed bricks and a free ball. The former can be modelled as an two dimensional array. To start off, I'd recommend implementing a uniform set of bricks, in which case your array is simply an array of boolean values. Each entry represents the presence or absence of a brick. As the ball hits the bricks, the corresponding boolean in the array is set to false. To start with, populate the array as all true (you can implement random levels or loading later).

You can infer the collision detection data from the index into the array. For example, to do collision detection in screen space units, with a 100 pixel border between the bricks and the side of the screen, and with each brick being 10 pixels high and 25 pixels wide, then a brick at index (y, x) has a collision rectangle { x, y, width, height } of { 100 + (25 * x), 100 + (10 * y), 25, 10 }. Obviously you would use named constants instead of hard coding these values, this is for illustration.

As Trienco says, the collision detection is similar to the the paddle (though you need to test more conditions). It will suffice to brute force through the array of bricks to see if any of the "active" ones are colliding.

In future, please use [ code ][ /code ] tags and paste the source as plain text. This way your code will stay indented, it won't have such a strange font and will be easier for us to copy, paste and test (if necessary). The post editor has a "code" button which you can use for this.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0