192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Time-based movement
Hi all, i'm moving my objects according to when the last frame was rendered.
I want things to move every 10ms, but on slow computers this won't happen, and on fast computers i want think to move as smoothly as possible.
I'm currently moving objects using this as a way of altering there velocity according to the time passed:
Code:
x*(GetTickCount()-LastThink)/10
where x is the actual velocity, and lastthink is the last time the object moved (the 10 is because i want things based on 10ms).
I think this works ok, but i'm having trouble with acceleration. I'm currently doing this to gradually reduce the speed of my object:
Code:
Velocity *= 0.98
This doesn't work, the object slows down faster when i'm on a fast computer (and it's called a lot), but takes ages to slow down on a slow computer (when it's not called as often).
To fix this i tried using this (pow is a funciton calculating is 1st parameter raised to the power of its 2nd parameter.):
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
If you are that could be the problem. It would be better to store GetTickCount()-LastThink in a variable at the start of a loop because otherwise the time the objects are all based on will get larger as the fame nears painting .
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
I actually find a frame limiter better than time based movement. I have a CPP file that might help you use the GetPerformance counter, which is more acuarate and can be used to increase the acuracy if you want to keep it time based.
One sec I'll just look for the file....
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Thanks for that class, it looks good.
I'll use it if i can't work out what i'm doing wrong, but i'd really like to get time-based movement working, because it's much better for moving objects the same speed on slow or fast computers (plus it uses a fast computers speed to create smooth movement).
btw. i am storeing gettickcount in a variable each time but i didn't post that to keep it simple.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
You don't have the book Game Programing Gems 2 do you....there is an article on how to make your own game loop, and your own counter there....it looks really good, havn't read it yet, but I will....
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
No, i don't have that book.
I have done game loops before, but i've always done it by checking if it's time to do the calcuations, if not then i wait. (to that effect anyway. I actually do the calculations then wait to render).
I don't want to do it that way unless i have to though.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
On my lastest project it seemed to go very strange and jumpy though. It relies on the idea that the current frame will take the same amount of time as the last one. However if you use the Performance Counter instead of GetTickCount then I guess it will be better, but I can't seem to get the performance counter to work in VB .
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
and i'm doing position = position + velocity*delta when i move an object.
I'm also doing the same when increasing the velocity using the arrow keys.
The only think i can think of that could be wrong is if i need to change my acceleration code to consider time, but i'm not sure how to.
Last edited by SLH; May 13th, 2004 at 10:42 AM.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Yeah the acceleration must also be set using the time. I think its just the same:
Vel = Vel + Acc * delta
if not try:
Vel = Vel + Acc * delta * delta
I think its the first though.
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
if (KeyPressed)
{
Acc = 10;
}
else
{
Acc = 0;
}
Vel += Acc * delta;
Vel -= Vel * CoF; // This is just to add friction so there is a max speed.
Pos += Vel * delta;
This will work quite nicley
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Vel -= Vel * CoF;
That is the line i'm having trouble with, when the computer runs fast, this'll have more effect that when it's ran slower, as a result you're able to travel fast when the computer is slow.
I've also tried Vel -= Vel * CoF * Delta; but that doesn't work either.
Thanks for all your help btw.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Vel -= Vel * CoF; // This is just to add friction so there is a max speed.
Vel *= (1-CoF);
which could be one constant
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
btw Gettickcount isn't that accurate, especially on slow computer.. use multimedia timer or QPC
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Thanx keda, I was thinking that looked strange what'd done.
I used this on a project of mine that was based on a constant frame rate:
VB Code:
Public Sub CalcPosition(ByRef X As Single, ByRef Y As Single, ByRef XVel As Single, ByRef YVel As Single, _
ByVal XAcc As Single, ByVal YAcc As Single, ByVal CoFriction, ByVal GravityOn As Boolean)
If Mode = 0 Then
XVel = XVel + XAcc
YVel = YVel + YAcc
ElseIf Mode = 1 And GravityOn Then
XVel = XVel + XAcc
YVel = YVel + YAcc + Gravity
End If
XVel = XVel - (XVel * CoFriction)
YVel = YVel - (YVel * CoFriction)
X = X + XVel
Y = Y + YVel
End Sub
Basically I would expect you put * delta on the end of all these lines ?
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
Posted by kedaman btw Gettickcount isn't that accurate, especially on slow computer.. use multimedia timer or QPC
The file I attached further up uses the QPC .
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
lol.. don't throw everything in a function, it'll be slower that way
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posted by kedaman lol.. don't throw everything in a function, it'll be slower that way
This is only for VB version, I'm gonna be rewritting the project in C++ once I have it working in VB, then the function will be an inline one .
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
You do know it doesn't show delta being set anywhere in the code you just gave?
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
doh, edited.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
#define TimeAltered(x) (x*Delta)
/////////////////
// Somewhere before the game loop starts...
// Will just initiate the CurrentGetTickCount for the first loop...
CurrentGetTickCount = GetTickCount() - 10;
/////////////////
LastThink = CurrentGetTickCount;
CurrentGetTickCount = GetTickCount();
Delta = ((float)CurrentGetTickCount-(float)LastThink)/1000.0f;
if(KEYDOWN(InputBuffer,DIK_UP))
{
Ship.Velocity.x += Delta*(cos(Ship.Rotation+D3DX_PI/2)*5);
Ship.Velocity.y += Delta*(sin(Ship.Rotation+D3DX_PI/2)*5);
}
Velocity.x *= TimeAltered(0.98);
Velocity.y *= TimeAltered(0.98);
Position.x += TimeAltered(Velocity.x);
Position.y += TimeAltered(Velocity.y);
Render();
Just because you need to know delta before going into the loop.
What is happening to it now, is it just running unpredictably or just slower on slower machines?
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
I've changed the delta line now, thanks.
At the moment when running fast (rendering taking little time) the ship barely moves at all, and when running slowly (rendering taking longer) the ship moves fast (all be it in large steps).
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Isn't that right?, try increasing the 5 you have as the velocity and it should appear normal when the loop is fast and just jumpy when the loop takes a long time. If it looks like they are both going the same speed just ones smoother then alls good. Slight discrepancies can be cleared out by using the performance counter.
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
If your not sure send me the exe and I'll try it on mine
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
oh.. one more thing Electroman, why do you have this delta thing? You could make sure a loop takes a certain amount of time instead.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Thats what I said higher up ^^^^.
I normally use a frame limiter. Consoles are restricted to only working on a frame limiter, they have a video frame every 16ms and if you miss it then you gotta wait till the next one, which means on a console you can only have 60fps or 30fps.
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Originally posted by Electroman Isn't that right?, try increasing the 5 you have as the velocity and it should appear normal when the loop is fast and just jumpy when the loop takes a long time. If it looks like they are both going the same speed just ones smoother then alls good. Slight discrepancies can be cleared out by using the performance counter.
Yeah, i think it's right, but it doesn't work. The slow one goes miles faster and in big jumps, but the faster one hardly moves.
Kedaman: That's what i'd do previously, but i want to try and do it this way as it's better (it'll run as smoothly as the computer can handle).
If i can't get this sorted by today then i think i'll just stick with a simple loop though.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
yeah I see what you are getting at you two. How about if you store the velocities in relative units instead, and you can keep out the delta
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
But to store the velocities as relative i would have to multiply them by delta??
Right now the velocities represent movement per second, i then scale that up or down based on the time elapsed. It just gets complicated when you think about acceleration etc.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
I think the problem just lies with the GetTickCount's acuracy now. Becuase if you think about it the error has been squared when you go into acceleration and that will mess things up alot. Explains my my current project didn't like the Time based method suddenly. Its the first time I properly added Acceleration.
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Ok, i'll have a go using queryperformancecounter and let you know the results.
Thanks a lot for all your help, both of you.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
The stored velocities would be pixels or whatever/frame, and by dividing off delta(seconds per frame), you get pixels or whatever/second. The idea is that you run a test loop to find delta on beforehand and then adjust all measures given in seconds^-n in frame^-n instead
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Personnaly, I store the last 10 frames, then average the time between them. I use that for my time. It makes the speed not do a MASSIVE jump if you pause code execution (by dragging the form, for example).
Don't pay attention to this signature, it's contradictory.