0

I am writing a program which moves a square image 1 pixel to the right every millisecond. The image starts at a x position of -469 and when it is done it ends up at an x position of 11. I want to move the image to the right 60 pixels a second but the Runnable code in java uses milliseconds to handle when it moves the pixel by a value of one. What value in milliseconds would I use to achieve that increase of 60 a second.

PS sorry if this involves programming but Im not the best at math and it is a math problem not a programming problem.

  • As a programmer, you should actively be seeking to remedy why you can't solve this problem. This is case of math illiteracy and I don't think is good thing for programmer to have. Explaining what you tried and where you failed to solve this problem, maybe can help someone improve your math skills? Rather than just answer 1 very simple homework problem. – marshal craft Jan 02 '18 at 13:35
  • It's more of a hobby programming, i just got out of high school and i did OK in maths class. 70's and 80's around the board. I wrote the code late at night so i wasn't thinking straight, but i do need to improve my maths skills what resources could i use to achieve that? – Dragon4c3_ Jan 03 '18 at 02:34
  • Actually i figured it out its not a math issue it's a programming issue, all of the answers yielded slow animations, i need to write a new loop that runs 60 times a second. – Dragon4c3_ Jan 03 '18 at 02:49
  • One tool you could use to improve your maths skills: the very books you learned from. By now, that material should be relatively easy, but evidently you didn't really completely "get it." So go back and do every problem in your algebra 1 book. Then do the same with algebra 2. A chapter worth of problems should take a half-hour or so. If it doesn't...you don't know the material well enough, so it's a half-hour well invested. :) – John Hughes Jan 03 '18 at 03:09
  • In my class the textbooks were kept in class unless you needed them for homework so i don't have any on me. Plus i have a good grasp on the basics of math its just this question was a little different than i thought it was, I tried all of the values the people commented before i posted the question and they did not work hence why i asked the question. And i would rather spend my time perfecting my programming skills than focusing on math. Plus i got to spend my time with the family and work, etc. – Dragon4c3_ Jan 03 '18 at 03:11
  • Is there an alternative to textbooks that does not cost money? – Dragon4c3_ Jan 03 '18 at 03:18

2 Answers2

0

60 pixels per second is 60/1000 pixels per millisecond; that's the same as

1000/60 milliseconds per pixel, hence 100/6, which is $16 \frac{2}{3}$. I don't know whether Java allows floats for that item within a Runnable, but if so, you should use 16 + 2.0/3.0.

If it requires an int...then you're not going to get yourself exactly 60 pixels per second.

What you can do is run it at 16 milliseconds per pixel and pause for a few milliseconds per second at some random point during the second; that'll give a slightly irregular motion, but the right number of pixels per second.

Alternatively, you can recognize that this is what's happening inside the box anyhow: you don't get to say when frames are actually rendered to the screen, so under the hood there's a "what's the current time? OK, what's that make the pixel offset be, in integers because it's easier? OK, let's go with that" kind of loop happening. :)

John Hughes
  • 93,729
0

Converting units, we get:

$$\frac{60 \text{ pixels}}{1 \text{ sec}}=\frac{60 \text{ pixels}}{1000 \text{ ms}}=\frac{1 \text{ pixel}}{16.666... \text{ ms}}$$

This says you should move $1$ pixel every $16\frac23$ milliseconds. If you can only work with whole numbers of milliseconds, then you could either round to $17$ and call it good, or you could use one $16$, followed by two $17$s to get the right average: $16,17,17,16,17,17,16,17,17,\ldots$.

G Tony Jacobs
  • 31,218