### Author Topic: Displacement and Distance travelled logic in Easy java simulation  (Read 16451 times)

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### Displacement and Distance travelled logic in Easy java simulation
« on: September 26, 2008, 12:52:02 pm »
Displacement and Distance traveled logic in Easy java simulation.

Displacement = Displacement - Displacement_initial; // easy in EJS

I was wondering how to do this elegantly?

Distance_travel = Math.abs ( Displacement) ?? does not look correct

Q1 :is there a

Distance_travel = Math.XXX ( Displacement) ?

or must i think of a  If ... else statement

if (Displacement < initial_Displacement) {
statements  ;

it is a bit more complicated that i thought

}
else if (expr) {
statements;
}
else {
statements;
}

Reference:
http://en.wikipedia.org/wiki/Java_syntax#If_..._else
« Last Edit: September 26, 2008, 02:35:02 pm by lookang »

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Displacement and Distance travelled logic in Easy java simulation
« Reply #1 on: September 26, 2008, 06:39:34 pm »
Displacement is a vector. Distance is line integral of absolute value of displacement.

It is possible to have zero displacement (back to original point) but the distance traveled is not zero.
For 2D case if you wan to calculate distance traveled=d.
define coordinate x,y and xs,ys (xs=x,ys=y, initially).
set d=0 at initialize page.

Assume evolution page is
dx/dt=vx;
dy/dt=vy;

At constraint page add the following lines:
d+= Math.sqrt((x-xs)*(x-xs)+(y-ys)*(y-ys));
xs=x;
ys=y;

Then, d is the distance traveled in 2D plane.

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### Re: Displacement and Distance travelled logic in Easy java simulation
« Reply #2 on: September 26, 2008, 11:28:54 pm »
thanks for the tip! i  will implement after 12 OCT 2008 as i am going to USA and Canada for holiday.

i  have not used "+=" before

At constraint page add the following lines:
d+= Math.sqrt((x-xs)*(x-xs)+(y-ys)*(y-ys));  // what is += ??  is it a syntax for line integral of absolute value of displacement.
xs=x;
ys=y;

that is so cool!

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Displacement and Distance travelled logic in Easy java simulation
« Reply #3 on: September 27, 2008, 12:15:33 am »
d+=a; is a the same as d=d+a;
d-=a; is the same as d=d-a;
d*=a; is the same as d=d*a;
d/=a; is the same as d=d/a;

d++; is the same as d=d+1;
d--; is the same as d=d-1;

But d++ is not quiet the sam as ++d;

if  d=3;
x=d++; is the same as x=d; d=d+1;
so x=3; after x=d++; is execute

x=++d; is the same as d=d+1; x=d;
so x=4; after x=d++; is execute

For the above two case: d=4; after x=d++; is execute

I have chinese version web page about basic syntax for java. Please check out Java ??
There are similar document in english available on the web. Google will help you find it.

Enjoy you holiday! :-)

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### Re: Displacement and Distance travelled logic in Easy java simulation
« Reply #4 on: October 14, 2008, 05:12:58 pm »
Hi prof!

I adapted the logic into the codes,

where d1 = distance traveled

x1s = x1 slider position
x1area = dummy variable for x1s counting of line integral

Codes:
d1 = d1 + Math.sqrt((x1s-x1area)*(x1s-x1area)); // same as +=
// d1 += Math.sqrt((x1-x1area)*(x1-x1area));
x1area=x1s; // x1area is just a way to do line integral
Codes:

The problem:
as I slide the time slider bar that controls ts = time slider variable.
The value of d1 is still counting as I slide time variable ts, which makes the value run continually which is not was i wanted.

I want to make it give the value for d1 = distance travel for time ts = 0 to ts = current position.

I tested these codes and they work fine in normal situation/simulation without the slider "ts".

Any tips how to trick the program into replay the value of d1 and ts ?

or do i need a different approach or strategy?

in case u need, the source code is attached
« Last Edit: October 14, 2008, 05:43:47 pm by lookang »

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Displacement and Distance travelled logic in Easy java simulation
« Reply #5 on: October 15, 2008, 11:02:53 am »
It is seems that what you need is not a real time simulation. You just need to show how displacement and velocity changed with time for constant acceleration.
The formula is all well know:
x(t)=x(0)+v(0)*t+0.5*t*t;
v(t)=v(0)+a*t;

Use the above equestion directly will be the easiest way to implement.
You can show the x-t or v-t diagram directly.

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### Re: Displacement and Distance travelled logic in Easy java simulation
« Reply #6 on: October 15, 2008, 12:54:58 pm »
i see what you mean.......

x(t)=x(0)+v(0)*t+0.5*t*t;
v(t)=v(0)+a*t;

is good and already implemented it this way.

codes:
x1 = x1s - x10;

but the distance traveled is another concept in secondary school as the Math.abs (integral of the area under the v-t graph) as distance traveled.

i am having difficulty implementing it elegantly (because there is positive area and Math.abs|negative| area).

Thanks!

so far, i think the best approach for me is by brute force aka not so elegant way Smiley( if statements )
codes:
if(v10>=0 && v1s>=0){
d1 = Math.abs(0.5*(v10+v1s)*(ts-0));
}
if(v10<=0 && v1s<=0){
d1 = Math.abs(0.5*(v10+v1s)*(ts-0));
}
if(v10>=0 && v1s<0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}
if(v10<=0 && v1s>0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}

I have do it!!!
« Last Edit: October 15, 2008, 03:12:39 pm by lookang »

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Displacement and Distance travelled logic in Easy java simulation
« Reply #7 on: October 15, 2008, 05:37:23 pm »
The above code can be simplified as
if(v10>=0 && v1s<0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}else if(v10<=0 && v1s>0){
tprime= v10*ts/(v10-v1s);
dtest = Math.abs(0.5*(ts-tprime)*v1s);
d1 = Math.abs(0.5*(v10)*tprime) + Math.abs(0.5*(ts-tprime)*v1s);
}else{
d1 = Math.abs(0.5*(v10+v1s)*(ts-0));
}