Solved

Moving a ball - simple casting problem

Posted on 2004-09-26
15
267 Views
Last Modified: 2010-03-31
I have a really annoying problem that I can't figure out.. It's gotta be a problem with my casting.  I'm working on a simple pong game, but can't get the ball moving around right.  I call this method every time I go through my game loop:

public void tick()
{
      float delta = System.currentTimeMillis() - timeAtLastTick;
      timeAtLastTick = System.currentTimeMillis();
      float ychange = (float)(ball.getYVol()*(delta/1000));
      float xchange = (float)(ball.getXVol()*(delta/1000));
      float newx = xchange + ball.getXPos();
      float newy = ychange + ball.getYPos();
      System.out.println("xchange: " + xchange + " newx: " + newx + " oldx: " + ball.getXPos());
      ball.setYPos(newy);
      ball.setXPos(newx);
}

What gets printed is something like this:
...
xchange: 7.5 newx: 5.48133175E11 oldx: 5.48133175E11
xchange: 16.0 newx: 5.48133175E11 oldx: 5.48133175E11
xchange: 7.5 newx: 5.48133175E11 oldx: 5.48133175E11
xchange: 8.0 newx: 5.48133175E11 oldx: 5.48133175E11
...

xchange and ychange are always positive, but newx and newy are always equal to oldx and oldy!  I'm using Tiger, so accurate timing isn't the problem.  Even if that was the problem, xchange and ychange are always postive, so I don't understand why newx and newy never change.  Xpos and ypos are stored as floats in ball, and get/set x/y don't do any casting.

I'm stumped.  Any thoughts?
0
Comment
Question by:Breadstick
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12158147
Well, adding 7.5 to 5.48133175E11 is like adding one drop of water to the ocean, no?
I think that's the problem.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158151
setXPos/setYPos may be operating on 'hidden' variables perhaps. In any case, they seem to be having no effect
0
 
LVL 37

Accepted Solution

by:
zzynx earned 35 total points
ID: 12158154
5.48133175E11 is 548133175???
and 548133175??? + 7.5 = 548133175???.5
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Expert Comment

by:Giant2
ID: 12158160
>xchange: 7.5 newx: 5.48133175E11 oldx: 5.48133175E11

the number you are using are really big (float). So if to a big number add a little number, the bigger remain the same.
5.48133175E11 is
548133175000
then add 7.5, it became:
548133175007.5 in float notation became:
5.48133175E11 (the same!)

Hope this clarify your doubt.
Bye, Giant.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12158179
Ahum ;°)
0
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12158759
How are you updating your graphics ?

Is it using a Thread and if not then it should be, otherwise you will not get any graphics updates.  This may be besides the point but just make sure you have the game running ina loop Thread, or you can use a Timer.

Link at Sun site regarding Threads and Timers.  http://java.sun.com/docs/books/tutorial/essential/threads/
Best of luck

pjcrooks2000
0
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12158781
Sorry just re read your question this is not your problem obviously :)
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12163669
Woah, I guess I never noticed that "E".  Where do you think that's coming from? The ball's xpos and ypos are initialized to about 100 I think.  Also, when I draw the ball, it's always in the top left of the screen.

Here's how I'm drawing it:
     g2.fillOval(Math.round(ball.getXPos()), Math.round(ball.getYPos()), BALL_WIDTH, BALL_HEIGHT);
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12164250
> Where do you think that's coming from?

Sorry, I meant: "Where do you think that giant number is coming from?", I know what the E represents.
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12164638
I think that I initialize timeAtLastTick to zero.  That means that by the time I first call tick(), delta (System.currentTimeMillis() - timeAtLastTick) would be over a trillion.  Do you think this could be the problem? If so, why is the ball getting drawn in the top-left of the screen?
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12166977
I don't know what kind of object is your ball object. I know that the top-left of the screen normally has coordinate 0,0.
Maybe when you use ball.set?Pos you do not change the position of the graphical object (I think) linked to the ball object you use.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12166982
>>I think that I initialize timeAtLastTick to zero.
You *think* it?

>>That means that by the time I first call tick(), delta (System.currentTimeMillis() - timeAtLastTick) would be
>>over a trillion.  Do you think this could be the problem?
Yes.

>>If so, why is the ball getting drawn in the top-left of the screen?
Don't know. Maybe that's the "fall back position" in case of drawing off-screen.
You could try that out by incrementing the x and y position and look what happens.
0
 
LVL 1

Expert Comment

by:KartikShah
ID: 12169015
>I think that I initialize timeAtLastTick to zero.  That means that by the time I first call tick(), delta
>(System.currentTimeMillis() - timeAtLastTick) would be over a trillion.  Do you think this could be the problem? If so,
>why is the ball getting drawn in the top-left of the screen?

The problem is that you are initializing timeAtLastTick to 0. Try to initialize it to System.currentTimeMillis(), this will remove the majot discrepancies in the code.

Kartik
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12202416
> You *think* it?
Sorry, I was at school and didn't have my code handy.

> You could try that out by incrementing the x and y position and look what happens.
That's was my problem, I was trying to increment x and y, but nothing was happening!

I got it working though.  Setting timeAtLastTick to System.currentTimeMillis() right before my first tick fixed it.  Thanks for you help everyone.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12206446
Thanks
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
thymeleaf natural templating vs JSP 2 100
Java Jpanels and Jframe 8 33
web application structure 18 77
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question