• 0

[JAVA]Problem with game(simple block breaker game)


Question

Hey. I have a problem with my simple java game. It's a block breaking game and it's almost done. The last thing to do is make some of the detection methods perfect. The methods should detect if the ball hits one of the blocks in the air. My current code does it well if the ball hits one of the longer sides of the blocks(north and south), but if the ball hits the west or east side of the block, it starts bugging. The ball will move into the block, until one of the current if-tests detects it, and reacts by changing the y-axis direction. When it first reacts like this it may also bring some other blocks with it. This will all be fixed if I can get some rules/if-tests to detect when the ball hits the shorter sides of the block and then changes the x-axis direction. Could anyone help me, please? :)

The block to test is called b, and has the getX/Y/Width/Height methods to call. The ball has the ints ballX, ballY and ballSize. dy and dx are the directions the ball moves. So changing the direction on x-axis is of course dx = -dx;

Picture of the situations I need code to detect

Detection code

I really hope some of you guys could help me fix this. Been struggeling all day to get it right. The y-axis test might also need to be fixed for this to work, I actually don't know anymore :s Thanks

9 answers to this question

Recommended Posts

  • 0

I always cringe at this sort of style:

boolean predicate() {
    if (condition) {
         return true;
    }
    else {
         return false;
    }
}

Could be written in one line and much more legibly:

boolean predicate() {
    return (condition);
}

Also your:

if(checkHitY(b)) {
    if(checkHitX(b)) {
        addToScore(b.getValue());
        model.hitBlock(i);
        dy = -dy;
        i = model.getSize() - 1;
    }
}

Could be rewritten, more logically, as

if(checkHitY(b) && checkHitX(b)) {
    addToScore(b.getValue());
    model.hitBlock(i);
    dy = -dy;
    i = model.getSize() - 1;
}

Stylistic issues aside, I see a dy = -dy, but not a dx = -dx in your code. No wonder then, that side collisions are not handled correctly. You need not only to determine if the ball hits, but whether it's a side (dx = -dx) or frontal (dy = -dy) collision, before applying the right velocity adjustement.

  • 0

Dr_Asik:

Stylistic issues aside, I see a dy = -dy, but not a dx = -dx in your code. No wonder then, that side collisions are not handled correctly. You need not only to determine if the ball hits, but whether it's a side (dx = -dx) or frontal (dy = -dy) collision, before applying the right velocity adjustement.

Thank you for your answer, thought it didn't help. I'm aware of the kinda messy if-test structure at the moment, but they won't be like that. I change specifically to this style to try out new methods of detection. Siince I might need a seperate checkHitXdx() method to detect the sides, I found it would be better to seperate the if-test for checkHitY() and checkHitX() while testing.

When it comes to the dx = -dx; it's not written there because that's what I need help with. I need help finding a similar method as checkHitX and checkHitY that detects the side, because I'm struggelin to make the formulas/if-tests myself. So there's not dx = -dx; at the moment, since hitting the sides is atm not beeing looked for by the methods. I've tried multiple methods to detect just the sides, but the game just got lots of bugs when trying them out. The ball would hit the corner of a block, and always change direction in x-axis, even though it hit on the left end of the south wall, which should make it just change the y-axis direction.

  • 0

You could simply test for the ball being below or above a block. If yes, reflect on y, if no, reflect on x. This will break if the ball speed is too high, but that's a problem with all simplistic collision schemes. If you want perfect collision you need to take into account before and after points, trace a vector and find the intersections with the segments defining the blocks, so there's some linear algebra involved.

  • 0
  On 06/04/2011 at 18:46, Dr_Asik said:

You could simply test for the ball being below or above a block. If yes, reflect on y, if no, reflect on x. This will break if the ball speed is too high, but that's a problem with all simplistic collision schemes. If you want perfect collision you need to take into account before and after points, trace a vector and find the intersections with the segments defining the blocks, so there's some linear algebra involved.

Yeah, but then I'd be checking the y-axis for hit 5 times(the dy value) for each block on each frameupdate, which would be lots of work for this. I tried to fix this by adding a area it could hit on both sides. Checking if it hit on the border, or at least before (border +/- dy pixels). The problem with this is as since I'm not detecting the sides now, it may slide into the middle of a block, and both upper test(with +/-dy) and lower test would be true and the ball starts bouncing up and down inside :p

To check the movement pixel for pixel for each frameupdate, would you simply check every object dy times, with lastX + (dx/dy) and lastY + (dy/dy) to check for hit? Is that a better solution then the "overflow" tactic (or whatever you could call my current method), or would that be too heavy on resources, with lets say 5 tests just for the y-test, for 100blocks on every frameupdate ?

  • 0
  On 06/04/2011 at 19:45, Graimer said:
Yeah, but then I'd be checking the y-axis for hit 5 times(the dy value) for each block on each frameupdate, which would be lots of work for this.
No, you start by detecting if there is a collision, and only if there is a collision you check on which side it is. So this usually adds 0 test per update, and at most 2 (when there is a collision). Unless you're running a 386 this should not present any performance problems.
  • 0
  On 06/04/2011 at 21:43, Dr_Asik said:

No, you start by detecting if there is a collision, and only if there is a collision you check on which side it is. So this usually adds 0 test per update, and at most 2 (when there is a collision). Unless you're running a 386 this should not present any performance problems.

Okey, I understand how you think. But would you mind actually making the tests for me, if it isn't too much to ask for? I've tried so many methods the last days I'm actually getting dizzy :p

I know it's asking a lot, but it seems like you got both the experience and a clear vision of how to solve this puzzle :)

  • 0
  On 06/04/2011 at 23:18, Dr_Asik said:

I'm sorry but I don't have time for this atm.

Well anyways, thank you very much =) Now I'll just need to get my sketchboard and start drawing/making test-conditions.

  • 0
  On 07/04/2011 at 21:02, Graimer said:

Well anyways, thank you very much =) Now I'll just need to get my sketchboard and start drawing/making test-conditions.

Last update: Made it work now. My tries with vectors(or one pixelmovement at a time) didn't work to well. But I found another solution and thought I'd share some hints of how I did it in case someone else is wondering about the same thing.

I found out that the Rectangle class had a bool intersect(rectangle r)-method and a rectangle intersection(rectangle r)-method. So I made a rectangle out of the block to check and one for the ball, checked it ball.intersect(block), if so , found the hitZone/intersected area with ball.intersection(block) and made tests on this very small rectangle to check if the ball hit on top, left, right or bottom side and the correct action for each side. Ex. if the ball hit the left side, the ball would now change to move to the left.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • New fiscal year, this happens in companies all over.
    • Windows 11 reaches a new all-time high share among gamers by Taras Buria The results of the June 2025 Steam Hardware & Software Survey are now available on the official website, showing Windows 11's continuous growth among gamers and the steady decline of Windows 10 ahead of the end of support. Valve reports that in June 2025, the almost-four-year-old Windows 11 holds nearly 60% of all PC users on Steam. It currently has a market share of 59.84%, which is a 1.54-point increase over the May 2025 results. Windows 10 has 35.69% (-1.31 point), and 64-bit Windows 7 nearly went extinct—only 0.09% of all Steam users who participated in the survey reported using the operating system that Microsoft stopped supporting years ago (the latest Steam client also no longer works in Windows 7). With Microsoft offering Windows 10 users free security updates for one more year, the outgoing OS is expected to stick around for a while longer on the gaming market and among regular users as well. Worldwide, Windows 11 has nearly caught up to Windows 10, as StatCounter reported earlier. Overall, 95.67% of the survey participants use Windows on their gaming machines (+0.22 points). Linux is second with 2.57% (-0.12 points), and macOS is third with 1.76% (-0.09 points). On the hardware side, the most popular PC configuration has 16GB of RAM (43.05%), a 6-core CPU (29.50%), an Nvidia RTX 4060 Laptop graphics card (4.79%), 8GB of VRAM (33.92%), and a 1080p monitor (55.54%). In June, the laptop variant of the RTX 4060 became the most popular GPU on Steam, taking over the desktop RTX 3060 with its 4.42% market share. Speaking of GPUs, Nvidia holds an overwhelming 74.07% share, while AMD has 17.68%, and Intel is third with 7.88%. Intel remains the most popular CPU vendor with a 59.29% share, while AMD has 40.71%. You can find more stats on the official Steam Hardware and Software Survey here.
    • Does it mean that the game will have weapons now? If that is the case, I'm interested. Not having weapons does not make any sense, even if it changes the gameplay. In such a hostile environment the first thing anyone would do is get as much weapons as they could.
    • This DDR5-6400 CL36 32GB RGB RAM that supports both AMD and Intel is only $72 by Sayan Sen Recently, we covered several SSD deals, both internal and external. These include the Crucial X9 Pro and X10 Pro, the P310 2280, and the Samsung 990 EVO Plus. Meanwhile if you are looking for RAM to upgrade your desktop PC or build a new one, the PNY XLR8 Gaming EPIC-X RGB DDR5-6400 Kit can be your go-to choice as it is currently just $72 (purchase link down below). In terms of specs, this dual-RAM kit delivers 32GB of total DDR5 capacity (each module is 16GB) running at 3200 Hz to deliver 6400 MT/s (PC5-51200) at 1.4 volts. Pre-programmed Intel XMP 3.0 and AMD EXPO memory overclocking preset profiles mean you can fire up the kit to its rated speed with a simple BIOS tweak, rather than having to deal with manual timing adjustments. The CAS latency for this DDR5-6400 kit is 36, which is quite tight for a preset profile. Thermal performance is said to be stellar thanks to the aluminum heat spreader, which should help dissipate heat during extended gaming sessions. Additionally, the heat spreader is also said to feature an "embossed pennant design that enhances the overall look and complements the lighting of other components." Speaking of lighting, the included EPIC-X RGB model features ARGB LEDs diffused through a geometric polymer light pipe and allows syncing via Asus Aura Sync, Gigabyte RGB Fusion, MSI Mystic Light, or ASRock Polychrome Sync software. Get the PNY RAM at the link below: PNY XLR8 Gaming Epic-X RGB™ 32GB (2x16GB) DDR5 RAM 6400 CL36-48-48-104 Desktop Memory Kit (MD32GK2D5640036XRGB): $72.24 (Sold and Shipped by Amazon US) (MSRP: $109.99) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • First Post
      loose_observer earned a badge
      First Post
    • Week One Done
      BeeJay_Balu earned a badge
      Week One Done
    • Week One Done
      filminutz earned a badge
      Week One Done
    • Reacting Well
      SteveJaye earned a badge
      Reacting Well
    • One Month Later
      MadMung0 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      446
    2. 2
      ATLien_0
      161
    3. 3
      +FloatingFatMan
      148
    4. 4
      Nick H.
      65
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!