473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

changing ball position

59
i am trying to make a breakout game where you have a paddle and ball bounces around breaking bricks.
the problem is that if ball touches the corner of player(green paddle). than it goes inside of player. i could not understand why. only thing it might be bc of all the dx, -dx, dy, -dy's. i am doing this so it would change ball postion. so it looks like it bounces of the player or window.


here i am check collision between ball and window. x,y,dx,dy are ball postion and speed.
Expand|Select|Wrap|Line Numbers
  1. public void BallWCollision()
  2. {
  3.     //dx, dy is ball speed
  4.  
  5.     if(x <= 0){   //if ball goes right of screen 
  6.       dx = -dx;
  7.     }
  8.    else if(x >= Main.WINDOW_WIDTH){  //if ball goes left of screen
  9.     dx = -dx;
  10. }
  11.  
  12.    if(y <= 0){    //if ball goes top of screen
  13.      dy = -dy;
  14.    }
  15.    else if(y >= Main.WINDOW_HEIGHT){  //if ball goes bottom of screen
  16.      dy = -dy;
  17.     }
  18. }
  19.  

here i am check collision between player and ball. if ball touches playeer than change ball dy.
Expand|Select|Wrap|Line Numbers
  1. public void playerBallCollision(Ball b)
  2. {
  3.     if(player.getBounds().intersects(b.getBounds())){  //if player and ball touch each other
  4.      b.setDy(-b.getDy());
  5.     }
  6. }
  7.  
Apr 28 '13 #1
4 1973
Nepomuk
3,112 Expert 2GB
From your description, I'm guessing the problem is within code you haven't posted; how do the "getBounds()" functions work? How does "intersects(...))" work?

Also, from what I can see you change the sign of the ball speeds but not the angle; from what I remember from physics, when reflected the angle of incidence equals the angle of reflection (and Wikipedia seems to agree with me). Of course, this is unrelated to the problem you mentioned.
Apr 29 '13 #2
chaarmann
785 Expert 512MB
You list the code for checking that the ball does not go out of the screen. But I can't imagine what it has to do with checking the collision between paddle and ball. Please explain. For me the first should not be related to the second and the listed source code has nothing to do with your problem.

Now to the real problem: collision between paddle and ball:
Do you check collision only when ball moves, or also when paddle moves? I guess you only check in one case, because you said that the ball goes through the paddle when hit at the edge. So I can imagine following situations:
1.) Ball is at position X,Y and checked that it's free. So it moves there. Now Paddle moves at X,Y without checking. Crash!
2.) Paddle is at position X,Y and checked that it's free. So it moves there. Now Ball moves at X,Y without checking. Crash!
3.) If you have parallel running threads each for ball and paddle, following can happen even if both check:
a) Paddle is at position X,Y and checked that it's free. But it does not yet move there, because the thread is interrupted and goes to ball thread. Now Ball checks for position X,Y and moves there, because it was free. Now Paddle moves at X,Y position. Crash! (Solution: synchronize)
Apr 29 '13 #3
chaarmann
785 Expert 512MB
Nepomuk,

I guess that he does not compute with angles, but just sets the number of horizontal pixels before he moves verical and vice versa (That's how I implemented my demo years ago). So changing the sign of these dx, dy values inverts the angle. Example:
dx=1, dy=1 : The ball moves one right, one down, one right, one down etc. So it moves in 90+45 degree.
After reflection at wall or paddle:
dx=1, dy=-1 The ball moves one right, one up, one right, one up etc. So it moves in 45 degree.
Just to clarify: if you need another angle, for example half of 45 degree, you would define:
dx=1, dy=2 The ball moves one right, two up, one right, two up etc. So just inverting dy=-dy does the reflection trick.
Apr 29 '13 #4
Nepomuk
3,112 Expert 2GB
I guess that he does not compute with angles, but just sets the number of horizontal pixels before he moves verical and vice versa
OK, that makes sense. Also, we seem to understand the problem differently, so just to clarify: @game2d, does the ball eventually bounce back or doesn't it?
Apr 29 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: wqhdebian | last post by:
I get a resutSet from one database as xml and want to insert these data to another database,and I want to form sql statement from the xml, and then using these sql statement to insert these data....
5
by: Ron Brennan | last post by:
Good evening. I'd like to replace whatever character is aways at the postion 2 in a String with another character. For example, the y in wxyx with b. I'm sure it must be possible, but I'll be...
3
by: Emjay | last post by:
Newbie needs to know how to set the mouse position.
0
by: Guoqi Zheng | last post by:
Sir, A few days ago, I asked a question about how to insert something into the last row of a repeater control. I got the following code from Ken, Private Sub Page_PreRender (ByVal sender As...
4
by: Woody Splawn | last post by:
I have a winform where I would like to explicitelty set the Horizontal scroll postion for a panel. I would have guessed something like the following would work but it does not ...
3
by: OJ | last post by:
It's easy to control one ball (a graphic) to move and bounce inside a square. But I don't know how to create and control a large number of balls (10 or 100) and count how many times they hit a wall...
3
by: News | last post by:
I know that IE does not support position: fixed, yet I have seen elements that seem to float down on a IE browser. Where can I go to learn how to make elements appear to be postion: fixed on IE? ...
1
by: lg | last post by:
Is there a way to set/get vertical scroll postion without Javascript. At times I need to repaint the page but the page should scroll to certain postion depending on user input.
8
by: Lukas Designs | last post by:
My design is not centering in Firefox but it looks correct in IE, can anyone tell me that is wrong: Here is the page: http://lukasdesigns.com/nolte/ Here is my CSS code: /* CSS Document */ ...
3
by: sungung | last post by:
Hi group, I want to back orginal scroll postion after reload page. Is it possible? My situation is call popup windows -enter data on popup -close popup -refresh parent page -reload data. As...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.