I want to move an object on the screen a fixed distance in a fixed tim

  • Thread starter cubud
  • Start date
  • #1
29
0

Main Question or Discussion Point

Hi everyone.

I am a programmer and I think some basic physics will help me to achieve something I wanted to do.

I want to move an object on the screen a fixed distance in a fixed time, but rather than moving with a uniformed velocity I want it to slow down at a constant rate so that it runs to a smooth stop.

So if for example I wanted to move 64m over 2s, how would I work out what my initial velocity and acceleration should be?

I appreciate any help. In fact I don't actually need to do this any more, but because I didn't know how to achieve the requirement I now have a strong urge to know how it should be solved.
 

Answers and Replies

  • #2
CWatters
Science Advisor
Homework Helper
Gold Member
10,528
2,294


So you want it to start from rest (u=0) accelerate for a while then decelerate to a stop 64m away from the start in time (T=2 seconds).

Ok lets assume the rate of acceleration and deceleration are the same. In that case the problem can be split in two parts.

1) Acceleration phase...

It has to cover the first 32m in 1s. The relevant equation would be

s = ut + 0.5 at2

where
s = the displacement
u = initial velocity = 0
a = acceleration
t = time

Since u=0 it simplifies to

s= 0.5 at2

or

a = 2s/t2
= 2*32/12
= 64m/s2

2) The deceleration phase is identical except "a" will need to be negative.
 
  • #3
29
0


Let me see if I can clarify. At an acceleration rate of 1pixel/second^2 I would expect my object to have the following Y locations over 6 seconds.

0,1,3,6,10,15,21

What I need to do is the opposite. I wanted to understand an equation that when given the acceleration rate (-1 pixels/s^2), distance to travel (21pixels), and the total time over which to travel (6s) I end up with the initial velocity - resulting in the following

Distance = 21pixels
Time = 6 seconds
Acceleration -1pixels/second^2
= Initial velocity of 6 pixels/second

Which would give me the following positions.
21,15,10,6,3,1,0
 
  • #4
CWatters
Science Advisor
Homework Helper
Gold Member
10,528
2,294


In which case you need...

s = ut + 0.5 at2

rearranged to give u the initial velocity..

ut= s - 0.5at2

u = s/t - 0.5at

Edit:

gives u = 6.5 pixels/second
 
Last edited:
  • #5
29
0


I will give this a try, thank you!
 
  • #6
29
0


Worked great, I thought I'd share the source in case anyone else stumbles across this. It should be pretty easy to work out the calculations from it.

Code:
function TSpriteBehaviorMoveTo.GetFullyEasedOffset(
  Original: Single;
  Target: Single;
  DurationSecs: Single;
  TotalDeltaTime: Single
): Single;
var
  HalfDistance: Single;
  HalfTime: Single;
  Acceleration: Single;
  CurrentTime: Single;
begin
  HalfTime := DurationSecs / 2;
  HalfDistance := (Target - Original) / 2;
  Acceleration := HalfDistance / ((HalfTime * HalfTime) * 0.5);

  if (TotalDeltaTime <= HalfTime) then
  begin
    CurrentTime:= TotalDeltaTime;
    Result := Original + (0.5 * acceleration * CurrentTime * CurrentTime);
  end else
  begin
    CurrentTime:= DurationSecs - TotalDeltaTime;
    Result := Target - (0.5 * acceleration * CurrentTime * CurrentTime);
  end;
end;
 

Related Threads for: I want to move an object on the screen a fixed distance in a fixed tim

Replies
17
Views
2K
Replies
5
Views
1K
Replies
7
Views
698
Replies
4
Views
759
Replies
2
Views
4K
  • Last Post
Replies
5
Views
7K
Top