Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / mathematic help

Author
Message
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 17th Aug 2004 11:15 Edited at: 17th Aug 2004 11:20
I know how to calculate the speed time and distance.
distance=time*speed etc...
but how do you calculate the distance, if the time is given, and the speed is accelerating from x# until it reaches top speed y# ?
note that the time may "expire" when the speed has not reached the top speed.

Any help would be really appriciated.
JeBuS
20
Years of Service
User Offline
Joined: 20th Jul 2004
Location: Undisclosed Location, Dominion of JeBuS
Posted: 17th Aug 2004 13:14
what you're looking for is equations for acceleration. I'll let you google it.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Aug 2004 15:47
wouldn't speed = time*acceleration ?

distance = distance + speed

So you want to know what the distance is after 4 seconds? Sounds like an iterative function.




Something like that perhaps?

"eureka" - Archimedes
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 17th Aug 2004 16:37
I've searched google alot, but got sites that weren't usefull and didn't provide what i needed.

Altough a mathemtical solution would be more effective(speed wise), in the meantime i'll use your method phaelax, thx.
it should work.
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 17th Aug 2004 18:15 Edited at: 17th Aug 2004 18:17
If the acceleration is constant, and the initial speed is v0, the inital position is say x0, then the equations of motion are

speed = v0 + acceleration*time
position = x0 + v0*time + 0.5*acceleration*(time^2)

If you are accelerating from speed v0 to a final speed vf in a total time t0, then the (constant) acceleration is given by

acceleration = (vf - v0)/t0

and hence now

speed = v0 + (vf - v0)*(time/t0)
position = x0 + v0*time + 0.5*(vf - v0)*(time^2)/t0

Hope that helps

N.B. If the acceleration isn't constant, you can't use these formulae and have to use something else, like some numerical integration kind of like suggested before, but if it is constant, these formulae are fine!
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 17th Aug 2004 22:00
Code Machine - your equations are correct, but a bit of simplifying of your last equation gives:

distance = 0.5 * (initial speed + final speed) * change in time

(which is the 2nd of Newton's Equations of Motion, and the one Zokomoko needs).

As mentioned, this will give accurate distances if the acceleration is constant. (ie. if the change in velocity is linear). If it isn't, you still use the same equation, but work out the distance over small time intervals and add them together (integration).
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 17th Aug 2004 22:37
@Ric
That equation is fine if you want to know the total distance travelled, but if you need to know the distance at any time in the motion (which Zokomoko may have wanted, but didn't really say), then you have to use something similar to what I wrote above.

Maybe I should write in words to clarify the equations:

speed = initial speed + acceleration*change-in-time
distance = initial speed*change-in-time + 0.5*acceleration*(change-in-time^2)
where
acceleration = (Final speed - inital speed)/total-change-in-time
and change-in-time^2 = change-in-time*change-in-time

p.s. that's not Newtons 2nd equation, it's simply a kinematical equation describing the motion
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 17th Aug 2004 23:32
With respect, I think you've misunderstood slightly.

What was asked for was the distance. This is given by:

distance = 0.5 * (initial speed + final speed) * change in time (usually abbreviated to s=0.5(u+v)t ) This is indeed the total distance travelled in a straight line over the particular time interval t. The position, as you no doubt realise, is then given by the old position plus this distance.

What you wrote, algebraicly, is s=ut+[0.5(u-v)t^2]/t for the distance. The point I made was that, whilst this is correct, it is not reduced to its simplest form. Expanding the brackets and simplifying the terms will give you s=0.5(u+v)t, as I mentioned, and will give the computer less work to do.

By the way, just for your information, Newton wrote 4 equations of motion for uniform acceleration in a straight line, which are

1. v=u+at
2. s=0.5(u+v)t
3. s=ut+0.5(at)^2
4. (v^2)=(u^2)+2as

What you actually did in forming your equation, as I'm sure you know, was to substitute equation 1 into eq 3. Upon simplification, this gives eq 2, as I've said. In fact, what Newton did was to derive eq 3 by substituting eq 1 into eq 2, where eq 2 is derived from the area under a speed time graph. So you kind of did it backwards. These equations are accredited to Newton because they appeared in his 'Principia' in the seventeenth century. Not to be confused with Newton's 3 Laws of Motion, which he incidentally published in the very same book (a rivetting read, so long as you can read Latin ).
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 18th Aug 2004 00:02 Edited at: 18th Aug 2004 00:02
Quote: "note that the time may "expire" when the speed has not reached the top speed"

which suggests that Zokomoko may need to know the speed/distance at any time, rather than just the end, because as he says, the time may 'expire' before the top speed is reached, hence why you may need a more general formula. By the way, there's no chance this formula is in any way computationally intensive compared to yours. Numerical integration would be very intensive in comparison, but not a simple formula like this.

That's just how I see it. Best wait for Zokomoko to set the record straight , although I'm sure that he has all he needs with all these posts anyway.

p.s. I'm not so sure those equations are (or should be) attributed to Newton. He may have wrote them down in Principia, but they were around a long time before him, at least the 14th century. As far as I know, they are simply called the kinematical equations of uniformally accelerated motion.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 18th Aug 2004 02:40 Edited at: 18th Aug 2004 02:55
Agreed - the difference in computation is minimal - it's just for the sake of neatness in my opinion to give an equation in its simplest form.

The equation does, however, give the distance travelled in a straight line at any point in time, so long as the 'final' speed is the actual speed at that point in time. Perhaps, to be clearer, it should read

'distance = 0.5 * (previous speed + new speed) * change in time'

And I stand corrected - it seems, after a bit of quick research, that William Heytesbury, Richard Swineshead, and John Dumbleton of Merton college - between 1328 and 1350 deserve credit for early forms of those equations. Thanks for pointing that out - the likes of Newton and Galileo get enough credit as it is!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Aug 2004 03:42
1300's? woh.

"eureka" - Archimedes
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 18th Aug 2004 10:21
Thx for all the replies.
I'll clarify some things.
In my game i've a ship, which has an initial speed, and top speed.
the ship is traveling in a straight line, the acceleration itself is constant, but the speed is not.
the effect i was trying to make, is place a cube(e.g.) in front of the ship in a distance that will take the ship 1.84 seconds to reach it.

I accomplished this in phaelex's method.

Frequently, the ship has not reaches top speed before 1.84 seconds has passed, hence what i meant by the time could "expire".

If the ship has reached top speed before the time "expired", the time between the ship reached top speed till the time expired shouldn't be hard to find out.

It could be done in equations, but i doubt a "for i=1 to 1840" would be computer intesive considering there's so little inside the loop.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 18th Aug 2004 12:52 Edited at: 18th Aug 2004 12:52
Just to clarify even further, is this what you mean:

Scenareo 1. Time interval is known; constant acceleration over this time interval; need to find distance travelled

Scenareo 2. Time interval known; constant acceleration for part of this time interval followed by a period of constant (top) speed for the rest of this time interval; need to find distance travelled

If that is the case, then we need to derive a general equation for both of those scenarios (goes to find piece of scrap paper .....)

OK - that would be, accounting for the fact my brain is fried at this time in the morning;

distance=(v*t)-(1/(2*a))*((v^2)+(u^2)-(u*v)) where a is the acceleration, v is the maximum speed, u is the initial speed at the start of the time interval, and t is the total time interval.

[** Codemachine - can you verify this? Basically, I've derived it from the area under a velocity time graph for a section of constant acceleration (ie. a triangle) and then a section of constant velocity v (a rectangle), where the time intervals t1 and t2 for each of those two sections are given by t1=(v-u)/a and t2=t-t1, and then followed by about 5 lines of simplification **]
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 18th Aug 2004 13:59
Thank you very much, that works good (scenerio 2).
The distance was negative, but no problem.

Too bad i can't understand how this equation works, i'll try to find some info on google.
Powersoft
21
Years of Service
User Offline
Joined: 1st Aug 2003
Location: United Kingdom
Posted: 18th Aug 2004 16:07
speed = distance / time taken

distance = speed * time

time = distance / speed


Create or Play? You choose!
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 18th Aug 2004 20:25
@Ric
I got something slightly different, with an extra factor of 2 somewhere:
distance = (v*t) + (2*u*v - (v^2) - (u^2))/(2*a)

I had a good check, but I can't see where I might of made a mistake. The only thing I noted is that you said a 'triangle', whereas it should be more like a trapezium (or something like a rectangle with a triangle on top). The other thing to note is that this can only be used if the maximum velocity is reached before the time 'expires'. If it does reach the maximum speed, then I think you have to use one of the formulas we said earlier, so I think it should be:

if t is the total time (e.g. 1.84 seconds) and
time to reach maximum speed, t1 = (v-u)/a

if t1<t then
distance = (v*t) + (2*u*v - (v^2) - (u^2))/(2*a)

but if t1>t then
distance = ut + 0.5*a*(t1^2) (or other variations as discussed earlier )

Does this make sense Ric? I think you need 2 different formulas because the acceleration is not a countinuous function (i.e. there is a discontinuity at t1, where a suddenly drops from a to zero)


@Zokomoko
I hope we've covered enough here. I think you suggested you wanted to know the time between reaching maximum velocity and the time expired. If we call this dt, then it is simply

dt = t - t1
= t - (v-u)/a

So just put in the values. If dt comes out negative, then the ship hasn't reached the maximum velocity yet.

You said a for-next loop wouldn't be computer intensive, and for simple games that is ok. But this isn't always the case, particularly for complicated 3-D games and BASIC is notorously slow in dealing with numerical computations, so you should always be looking for short-cuts and ways to optimise your code. The integration is a very general way of solving this sort of problem, but if you know a formula that gives you the answer without further effort, then why not use it?
Also, I'm sure there are some sites that have info on this stuff. If I come across one, I'll be sure to post it here.

Code Machine
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 18th Aug 2004 21:44
Yea you've helped alot.

I replaced the for-next loop with ric's equation, and it gives the same result.

so far so good
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 18th Aug 2004 22:43 Edited at: 19th Aug 2004 10:40
Yes - don't know why I said triangle - I did in fact use the formula for a trapezium - (area = s = 0.5(u+v)t which is the second of those equations again!) - I did however forget to carry the factor of 2 at the last line - well spotted Codemachine.

So we are agreed on the equation, distance=(v*t)-(1/(2*a))*((v^2)+(u^2)-2*(u*v))

To go further, I would argue that this is applicable for scenareo 1 as well. That's because the ratio between t1 and t2 is automatically set in the equation by the acceleration a. If a is small enough, and the object is accelerating all the way up to the final value of t, (ie the whole v-t graph is a trapezium) then t1 will tend to t, and t2 will tend to zero (as if t2=t-t1 and t1=t then t2=0). If a is very large, on the other hand, then t1 will tend to 0, and t2 will tend to t. Kind of beautiful, when an equation does all the hard work for you!

Note - Powersoft - those equations you stated are only accurate if the speed is constant. In this case the speed is changing for part of the time, those equations don't work.

Note - Zokomoko - I'll to try and explain a little more clearly what's been done here, so you can see where the equation comes from. Your boat's motion can be simply described by drawing a velocity time graph. The total distance travelled by an object is equal to the area under the velocity time graph. In your case, this forms a trapezium shape joined to a square. The equation is then found by adding these two areas together. This is the (neat version of!) the derivation:

Hope that helps explain things.
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 18th Aug 2004 23:17
@Ric
Yep, I think we've agreed on the form of that equation. Getting slightly confused at the moment, but here's the way I'm thinking about it.

t1 = (v-u)/a is the time it takes to accelerate to a 'chosen' final speed v, and therefore does not change. So if a is very small, then the speed will not reach v before the total time t has expired. So t1, the required time to accelerate to v, is greater than t. Therefore, while it is only the trapezium that you are working with, it is not a trapezium from u to v, but from u to v', where v'<v. This is why I think you need two separate equations. I think one equation would work if you changed v to v' (i.e. the final velocity, whatever it is), but you can't know v' without doing a seperate calculation beforehand, so you still have to do at least two separate calculations here. Does this make sense?
I agree with you in the limit of high acceleration though, that equation does sort things out for you, but I'm still unconvinced you don't need a separate equation for t<t1.

@Zokomoko
Sorry for slightly hijacking the post with this 'debate'
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 19th Aug 2004 02:23 Edited at: 19th Aug 2004 14:03
Yes - sorry Zokomoko, this is probably beginning to get a bit deeper than you wanted.

**EDIT** Don't bother reading this - it's rubbish!!



Codemachine, yes, where t<t1, ie. where the object doesn't reach max velocity v before the time t elapses is something I hadn't fully considered. My scenario 1 was assuming t=t1, and the object just reaches v at time t. In a semiconscious way, I'd kind of thought that if t<t1, and v isn't reached, then that would make the time t2 negative, which would make the rectangle negative, which would mean the equation would automatically subtract this distance from the total distance that would be covered - on applying a little more brain power though I don't think it would actually work in that particular case. It would be like taking away a rectangle from a trapezium whereas you'd need to take away a trapezium. Arghh my brain hurts.....


Bear with me on this one ....

So, if we say the object only reached v' after the interval t, then we could make our v-t graph out of two trapeziums, the first with area as given before, s1=0.5(u+v)t1, which will give the distance the object would cover if it reached v in a time t1, and the second trapezium s2=0.5(v'+v)t2, the distance it would have travelled after t, from v' to top speed v. This will then be taken away from s1 if a is so small that t1>t, making t2 negative.

So the equation for s becomes s=0.5(u+v)t1 + 0.5(v'+v)t2.

If scenareo 2 is the case (ie it reaches v in t1<t) then v' would be equal to v, and the equation reduces back to what we had before. Making sense so far? Okay, so, now we can simplify the equation. Well, v'=u+at (remember t is the total time, at which point the object has reached v'). And t1=(v-u)/a and t2=t-t1 as before, which barring major cockups, gives an equation for s as follows,

distance=[{(v^2)-(u^2)}/2*a] + [0.5{u+at+v}{t-(v-u)/a}]

upon expanding and simplifying, I make that:

distance=(1/(2*a))*(((a^2)*(t^2))+(2*(v^2))+(2*u*a*t)-(2*u*v))

I'd be interested to know if a) Codemachine can confirm/correct that, and b) if it seems to work in Zokomoko's code.


BiggAdd
Retired Moderator
20
Years of Service
User Offline
Joined: 6th Aug 2004
Location: != null
Posted: 19th Aug 2004 03:15 Edited at: 19th Aug 2004 03:21
(V*V)=(U*U)+2*(a*S)

V= Starting velocity
U=Final Velocity
a=Acceleration
S=Distance
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 19th Aug 2004 04:14
That assumes constant acceleration BigAdd. If it was that easy, we'd have got there a long time ago!
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 19th Aug 2004 09:57 Edited at: 19th Aug 2004 10:00
@ric
Using the previous equation: distance#=(v#*t)-(1/(2*a#))*((v#^2)+(u#^2)-(u#*v#))

does this formula work in both scenerios ?

Using the current equation: distance#=(1/(2*a#))*(((a#^2)*(t#^2))+(2*(v#^2))+(2*u#*a#*t#)-(2*u#*v#))

The distance I got seems to be pretty bigger than the previous equation, and the for-next loop method. Also, the distance value was given in positive unlike the previous equation, so maybe that's relavent...
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 19th Aug 2004 10:24 Edited at: 19th Aug 2004 14:08
Firstly, the first equation needs a *2 in it at the end, which codemachine spotted :

distance#=(v#*t#)-(1/(2*a#))*((v#^2)+(u#^2)-(u#*v#*2))

This equation works if the boat reaches top speed before the time elapses, or if it reaches top speed exactly at the point the time elapses. However, it probably won't work if the boat doesn't quite get to top speed before the time elapses.

The second equation 'should' work in all situations, assuming I derived it correctly. If the values seem way out though, it's quite possible that I've gone wrong somewhere. I'd suggest using it if it seems to work, and if it screws up your program we'll go back to the drawing board!
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 19th Aug 2004 11:43 Edited at: 19th Aug 2004 11:44
I've tested it some more with different values.
I noticed that, no matter if the time is 1840, or 920, or 184 (all milliseconds), the distance remains the approx same:
about 1400-1610, this is because intial speed varied between exremely low values (it doesn't matter, the game has physics, hence the variation in intial speeds).
So i think the problem may be the time aspect of the equation.

Here are the variable values if you might want:

a#=acceleration#(obj) `=58
v#=topspeed#(obj) `=297
u#=speed#(obj) `approx 0 (always positive)
t#=time#/1000

the time is given in milliseconds, so i've divided by 1000.


On the good side though, i understand why you'd need a trapezium shape, and the graph aspect of the equation.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 19th Aug 2004 14:02 Edited at: 19th Aug 2004 14:06
Quote: "no matter if the time is 1840, or 920, or 184 (all milliseconds), the distance remains the approx same"


Right, my second equation is wrong then. I thought I could combine all scenarios into one equation, but my logic was probably wrong.

Not to worry, just do what codemachine suggested - split it into two seperate equations as follows:

If boat reaches max speed before time expires (in code, "IF (u#+a#*t#)>=v# "), then use the equation:

distance#=(v#*t#)-(1/(2*a#))*((v#^2)+(u#^2)-(u#*v#))

If boat's speed is less than max speed when time expires, (which you could code as 'else') then use this equation:

distance#=u#*t#+(0.5)*a#*(t#^2)

Your code would then be of this form


which, I think actually works at last!!
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 19th Aug 2004 14:43
Thx, that works
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 19th Aug 2004 20:33 Edited at: 19th Aug 2004 20:33
@Zokomoko
Super! Glad things have been sorted out then . Just wondering after going through all that, what kind of game exactly are you making?

@Ric
I'm assuming you're from somewhere over the other side of the Atlantic considering the times of your messages.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 19th Aug 2004 20:40 Edited at: 19th Aug 2004 21:04
Quote: "I'm assuming you're from somewhere over the other side of the Atlantic considering the times of your messages. "


!!! No - I just don't sleep much!! I'm actually based in North Wales at the moment. You?

Glad it works now Zokomoko - good team effort.
Zokomoko
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 19th Aug 2004 21:29
I'm making a 3d star trek themed rpg, over 6 months.
That equation was required to position the warp flash in the correct position, so it will be synced with the warp sound.
I'll post some pics in the WIP section later on, and if I could find a some host. But i'm working very hard on the engine, and a.i programming for the last months.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 19th Aug 2004 21:34
Ah - I thought when you said 'ship' you were talking about boats!!!
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 19th Aug 2004 22:21
North Wales? I'm based in South Wales, Cardiff to be more precise . Yes, you must have really strange sleeping patterns then
bibz1st
21
Years of Service
User Offline
Joined: 2nd Jan 2003
Location:
Posted: 21st Aug 2004 22:38
is it me or can else anybody smell burning ?
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 21st Aug 2004 23:15 Edited at: 21st Aug 2004 23:15
Eh?
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 21st Aug 2004 23:34
@Bibz 1st: nah! just you! its`s your brain thats smokeing
(I put mine out with a glass of Grants 7yr malt...lol..I was beginning to remember school lessons here...even more scary it was starting to look understandable)

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, Nvidia FX5900 gfx, 6 way surround sound, PC2: AMD 2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster.

Login to post a reply

Server time is: 2024-09-22 22:20:23
Your offset time is: 2024-09-22 22:20:23