Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ball doesn't bind to the boundry correctly  (Read 1019 times)
0 Members and 1 Guest are viewing this topic.
Offline 8bitooze

Senior Newbie





« Posted 2011-02-09 15:52:48 »

I am programming my game and have hit another snag. I am trying to get the ball to bounce from left to right. I managed to get that working, but when the ball hits the left boundary it intersects it as if you do a print screen you will see half the ball within the screen and half of it is outside the screen. What's strange is this behaviour isn't exhibited by the right boundary checking code. To make matters worse the code for the ball's boundary checking is is almost the same as the paddle's boundary checking code, yet that code works fine. Any assistance in the matter would be greatly appreciated. You can download the source code here

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
Offline aazimon
« Reply #1 - Posted 2011-02-09 22:25:14 »

You logic is good for detecting the right size. I suspect that the width of your applet may be bigger then what you see on your screen. Try drawing a line on the right side and see where disappears from the screen.
Offline IronclawsBt

Junior Member


Medals: 1



« Reply #2 - Posted 2011-02-09 23:09:37 »

I believe you have two errors. First is that during your x intersection test, it should be x + vx < ... not x - vx < ... if your intent was to predict the objects next location. This change breaks the paddle movement, but I think there was another mistake there. You are using the paddles vx as a speed (i.e. the absolute value of the velocity). In the paddle movement code it should be using -paddle.getVX() to test against the left boundary.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 8bitooze

Senior Newbie





« Reply #3 - Posted 2011-02-10 19:40:31 »

Quote
First is that during your x intersection test, it should be x + vx < ... not x - vx < ... if your intent was to predict the objects next location.
1  
x + vx < 0 - offset
don't seem logical to me as your moving the object to the left by subtracting from the x position. Could you care to explain?

Quote
You are using the paddles vx as a speed (i.e. the absolute value of the velocity). In the paddle movement code it should be using -paddle.getVX() to test against the left boundary.
Why are we negating the paddles x velocity just to check for the left boundary? Again this doesn't seem logically to me. If you would mind explaining it would be very helpful. Any assistence in this matter would be greatly appreciated.

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
Offline IronclawsBt

Junior Member


Medals: 1



« Reply #4 - Posted 2011-02-10 20:21:03 »

To predict where an object will be you add its velocity (times the amount of time forward you want to predict) to its position. This is why you should always be adding the velocity, never subtracting. Think about how the objects position will be updated in the next frame.

When the paddle is moving left and needs to be tested against the left boundary, it has a negative velocity. Its x position is decreasing. In your code you are using your paddle velocity different than your ball velocity. You are adding or subtracting the velocity depending on which direction it should be moving. That is why you need to flip the sign for the left boundary test.

I would recommend you rewrite your paddle movement so that it uses negative velocity to move left. This would also fix your problem.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
Offline 8bitooze

Senior Newbie





« Reply #5 - Posted 2011-02-10 21:16:30 »

To predict where an object will be you add its velocity (times the amount of time forward you want to predict) to its position. This is why you should always be adding the velocity, never subtracting. Think about how the objects position will be updated in the next frame.

When the paddle is moving left and needs to be tested against the left boundary, it has a negative velocity. Its x position is decreasing. In your code you are using your paddle velocity different than your ball velocity. You are adding or subtracting the velocity depending on which direction it should be moving. That is why you need to flip the sign for the left boundary test.

I would recommend you rewrite your paddle movement so that it uses negative velocity to move left. This would also fix your problem.
Thanks for the explanation, that makes a lot of sense. I made the adjustments you suggested and it worked!

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (24 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (68 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!