Java Forum / General / April 2006
How to get the velocity in real-time when acceleration is known?
iherage - 18 Apr 2006 13:45 GMT The problem is this: The acceleration is given. When beginning the program, the time is shown as 00:00. Then the time increases by second. And the velocity at that time is shown and changing by every second. I have checked the Java API and only find some classes(System.nanoseconds(), etc.) to measure elapsing time. But these classes do not seem to fit here. Do anyone know how to program the timing? Thank you!
Oliver Wong - 18 Apr 2006 21:27 GMT > The problem is this: > The acceleration is given. When beginning the program, the time is [quoted text clipped - 4 lines] > these classes do not seem to fit here. Do anyone know how to program > the timing? Thank you! Not sure I understand your question. You somehow "magically" get the acceleration, and from there you want to determine velocity? Well, to do this, you need the initial velocity, then, assuming the acceleration is constant, just apply basic physics. If the acceleration varies with time, you might need some calculus to integrate over the elapsed time.
AFAIK, there's no standard API to directly support Newtonian physics simulation.
- Oliver
iherage - 19 Apr 2006 07:48 GMT I DO know how to calculate the velocity when the initial velocity and the constant acceleration are given. Use the formula V_t=V_0 + at . But my point is how to increase the time by second so that I get the corresponding velocity when the program is running . E.g, the initial velocity is 0 m/s, the constant acceleration is 10 m/s^2, when the program is running , the time is increasing from 1 by second; that is t= 1s, 2s, 3s,...the program will give the velocity as 10m/s,20m/s,.... Just by second. Nanoseconds or Millseconds are too short time intervals. Thank you!
Roedy Green - 20 Apr 2006 01:24 GMT >my point is how to increase the time by second Do you mean wait one second of real time? In that case, you need a Timer. See http://mindprod.com/jgloss/timer.html
Do you mean simulate one second of time? In that case, you add 1 to t. and calculate your formulas for whatever else you need using t as a parameter.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
iherage - 19 Apr 2006 07:59 GMT I DO know how to calculate the velocity when the initial velocity and the constant acceleration are given. Use the formula V_t=V_0 + at . But my point is how to increase the time by second so that I get the corresponding velocity when the program is running . E.g, the initial velocity is 0 m/s, the constant acceleration is 10 m/s^2, when the program is running , the time is increasing from 1 by second; that is t= 1s, 2s, 3s,...the program will give the velocity as 10m/s,20m/s,.... Just by second. And Nanoseconds or Millseconds are too short time intervals.
Chris Uppal - 19 Apr 2006 09:27 GMT > I DO know how to calculate the velocity when the initial velocity and > the constant acceleration are given. Use the formula V_t=V_0 + at . But > my point is how to increase the time by second so that I get the > corresponding velocity when the program is running. Then you problem has nothing at all to do with acceleration. What you want is a way to have code executed at regular intervals (in wall-clock time). There are two Timer classes provided in the standard library, java.util.Timer and javax.swing.Timer, one or the other will probably do what you want. Or, if not, then you could create your own kind of timer using a helper thread, and Thread.sleep().
-- chris
Gabriel Belingueres - 19 Apr 2006 15:40 GMT Or you could calculate the elapsed time from one previous processing and the next, using System.nanoTime() (in Java 5) or System.currentTimeMillis() in Java 1.4 or before. If you have installed Java 3D, you have available the J3DTimer class, which has a nanosecond resolution and doesn't require Java 5.
Roughly:
long prev = System.nanoTime(); while (true) { long current = System.nanoTime(); if ( current - prev >= 1000000000 ) { // update movement prev = current; } else { sleep(1000 - current + prev) } }
This is basically what you would nned to do.
There are plenty of game programming books that show how to handle this. A good one is this: http://fivedots.coe.psu.ac.th/~ad/jg/
Gabriel
Michele Damian - 19 Apr 2006 10:29 GMT > I DO know how to calculate the velocity when the initial velocity and > the constant acceleration are given. Use the formula V_t=V_0 + at . But [quoted text clipped - 5 lines] > Just by second. And Nanoseconds or Millseconds are too short time > intervals. I think you have to create a Thread to calculate your formula. Try with this:
public class Clock extends Thread {
public Clock() { super(); }
public void run() { while(true) { try { /* YOUR FORMULA */ this.sleep(1000); } catch(InterruptedException e) {} } }
public static void main(String args[]) { Clock c = new Clock(); c.start(); ... } }
This isn't a good method because it calculate your formula after one second plus the time your computer execute the formula in the previus cicle. A better solution is to add this code:
try { long start = System.currentTimeInMillis(); /* YOUR FORMULA */ long end = System.currentTimeInMillis(); this.sleep(1000 - (start - end)); }
Of sure there are better solutions than mine. This is the first one that I guess.
For more information take a look at http://java.sun.com/j2se/1.3/docs/api/java/lang/Thread.html
Michele Damian - 19 Apr 2006 10:36 GMT I wrote:
> try { > long start = System.currentTimeInMillis(); > /* YOUR FORMULA */ > long end = System.currentTimeInMillis(); > this.sleep(1000 - (start - end)); > } I'm sorry. It's
this.sleep(1000 - (end - start));
Roedy Green - 18 Apr 2006 21:53 GMT >The problem is this: >The acceleration is given. When beginning the program, the time is >shown as 00:00. Then the time increases by second. And the velocity at >that time is shown and changing by every second. I think the answer to your problem is to be found in high school physics text not a programming text.
For example the sorts of formula you may be seeking are:
d = 1/2. * a * t * t;
d = v * t;
a = dv/dt;
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
James McGill - 19 Apr 2006 02:19 GMT > I think the answer to your problem is to be found in high school > physics text not a programming text. ...
> d = 1/2. * a * t * t; > > d = v * t; > > a = dv/dt; In reverse though, you need some numerical method to approximate integration, or else, the solution to a differential equation.
Maybe high school level stuff for Canada...
Depending on the mechanics of the actual problem, there might be trivial ways to get estimates. But he's not asking how to get acceleration from velocity, a deriviative, he's asking how to go the other way, a form of integration, and potentially much more difficult... especially when working in two or three dimensions.
Roedy Green - 19 Apr 2006 03:32 GMT On Tue, 18 Apr 2006 18:19:15 -0700, James McGill <jmcgill@cs.arizona.edu> wrote, quoted or indirectly quoted someone who said :
>Maybe high school level stuff for Canada... We did trig and calculus in high school. I don't think I did numerical integration and differential equation solving until perhaps my 4th year of university.
I remember Anthony Ralston, the esteemed author of our numerical analysis textbook came to visit circa 1968. I asked him about how you might go about interpolating for use in computer animation, to produce "well behaved" curves similar to those you could get with a Staedtler Mars interpolation snake. (I had been playing around with polynomials that were erratic).
He upbraided me for such as silly question since it there was no mathematics behind the desired interpolation, just a desired empirical result. To be fair, computer animation had not been invented at the time, and the pen plotter was still a novelty.
Later I discovered the spline curves and of course today there are millions of dollars spent computing such "meaningless" functions.
I got a similar brush off from Walt Disney in 1975 when I tried to explain a scheme for 2D computer animation.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Luke Webber - 19 Apr 2006 04:29 GMT > On Tue, 18 Apr 2006 18:19:15 -0700, James McGill > <jmcgill@cs.arizona.edu> wrote, quoted or indirectly quoted someone [quoted text clipped - 5 lines] > integration and differential equation solving until perhaps my 4th > year of university. Odd. In my Australian education, we started differential equations in (about) year 10. I particularly remember the blinding flash of light when we started kinematics in physics, and I suddenly realised that all the equations could be easily derived using my brand-new knowledge of calculus.
> I remember Anthony Ralston, the esteemed author of our numerical > analysis textbook came to visit circa 1968. I asked him about how you [quoted text clipped - 7 lines] > result. To be fair, computer animation had not been invented at the > time, and the pen plotter was still a novelty. [snip]
Not too surprising. Pierre Bezier didn't describe the Bezier curve until the 1970s.
> Later I discovered the spline curves and of course today there are > millions of dollars spent computing such "meaningless" functions. > > I got a similar brush off from Walt Disney in 1975 when I tried to > explain a scheme for 2D computer animation. "Were artists. Where's the artistry in that, Kid?" <g>
Luke
Scott Ellsworth - 18 Apr 2006 23:46 GMT > The acceleration is given. When beginning the program, the time is > shown as 00:00. Then the time increases by second. And the velocity at > that time is shown and changing by every second. Divide the task up into determining the new velocity given the acceleration, and updating the display as time advances.
Start with the first part - can you write a function that returns the velocity, given the acceleration and the time. This is simple newtonian physics, and should be in a standard textbook. (The OReilly book Physics for Game Programmers describes things like this, but in C++)
Then, once you have that function, write something with a thread that changes the 'velocity' variable and forces an update every second.
Feel free to post if you run into a stumbling block,
Scott
 Signature Scott Ellsworth scott@alodar.nospam.com Java and database consulting for the life sciences
Alex Hunsley - 19 Apr 2006 01:00 GMT > The problem is this: > The acceleration is given. When beginning the program, the time is [quoted text clipped - 4 lines] > these classes do not seem to fit here. Do anyone know how to program > the timing? Thank you! Sadly, there is no Java API to do your kinematics for you. Look up 'euler time stepping' or similar on google and get busy on this homework.
Your first mission, should you accept it, Mr Bond, is to find out the pseudo code/algorithm for doing Euler time step simulation of constant acceleration. Your second mission is to implement that algorithm in Java.
(Alternative to time stepping: you can use one of the classic kinematic equations to derive the velocity at any time for a given starting velocity and acceleration. s = ut + 1/2 at^2, or perhaps just v = u + at, mm'kay?)
Hal Rosser - 19 Apr 2006 01:33 GMT > The problem is this: > The acceleration is given. When beginning the program, the time is [quoted text clipped - 4 lines] > these classes do not seem to fit here. Do anyone know how to program > the timing? Thank you! IIRC Acceleration is expressed in (for example) ft/sec/sec (feet per second per second). As long as you know the elapsed time, if the acceleration is constant, then you just multiply the Seconds times the acceleration. example: if acceleration is 32 ft/sec/sec (gravity) then (ignoring other factors ) in 5 seconds, the velocity will be 5*32 (or 160) ft/sec. I think that's right. HTH Hal
Knute Johnson - 19 Apr 2006 05:38 GMT > The problem is this: > The acceleration is given. When beginning the program, the time is [quoted text clipped - 4 lines] > these classes do not seem to fit here. Do anyone know how to program > the timing? Thank you! Since nobody really answered your question; you can use a timer to cause an event periodically. Measure the elapsed time with System.currentTimeMillis() or System.nanoTime(). You can then use the formula that Roedy gave you to calculate the current velocity.
knute...
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|