How to get the velocity in real-time when acceleration is known?

I

iherage

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!
 
O

Oliver Wong

iherage said:
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!

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
 
R

Roedy Green

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;
 
S

Scott Ellsworth

iherage said:
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
 
A

Alex Hunsley

iherage said:
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!

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?)
 
H

Hal Rosser

iherage said:
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!
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
 
J

James McGill

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.
 
R

Roedy Green

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.
 
L

Luke Webber

Roedy said:
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.

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
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.
[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
 
K

Knute Johnson

iherage said:
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!

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...
 
I

iherage

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!
 
I

iherage

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.
 
C

Chris Uppal

iherage said:
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
 
M

Michele Damian

iherage said:
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.

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
 
M

Michele Damian

I said:
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));
 
G

Gabriel Belingueres

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top