//eTime is elapsed time last frame.
//dVelox and y is a float for storeing between world pos and VFactor, if I dont use this there will be loss in movement.
//If player are moving.
if((VFactor.vX != 0) || (VFactor.vY != 0))
{
//Movement
dVeloX+= VFactor.vX*( ((float)eTime)/20.0f );
dVeloY+= VFactor.vY*( ((float)eTime)/20.0f );
//Add the integer part of the velocity, and decrease it from dvelox and dveloy.
Player.Pos.ulWorldX+= (long)dVeloX;
dVeloX-= (long)dVeloX;
Player.Pos.ulWorldY+= (long)dVeloY;
dVeloY-=(long)dVeloY;
return true;
}
🔒 Locked
Movement flicker
Advertisement
Original Post
Hello
I think there is something wrong with my movement calculation. When a player wants to move I calculate the incx and incy factors that will move him in right direction.
The playerworld position is a unsigned long and im using a float variable to store the movement and when it > 1 < -1 that will increase the world position.
ex (long)0.1 = 0, worldx +=0;
ex (long)1.1 = 0, worldx +=1;
The problem is that it flickers, it’s not a smooth movement, can there be something wrong with my code?
Why should it create a contiunous movement? Your code gives whatever it moves good reason to jump around. Granted the basic idea works, but every once in a while we have remember that its a computer working with.
I assume your problems begin with the scale of .vX and .vY compared to the unit scales of the long type.
Assume a slow motion with a low value of .vX. Like vX = 0.01
so only every 100 frames the positions get updated. If your Player position cooridates do not correspond to pixels you get a jump every now and then. (every 5th second). Why not use float variables for the storage of your players position? Do not optimze to early.
Your other problem is probably the eTime variable. On most systems time() calls of the time.h show pretty disgusting behaviour: call time() often in a loop an print the results. You will notice that most of the values are the same, jumping up about 10 times a second, sometimes more. So most of your programm''s jerkiness comes from the used timing function. If you are working in a Win environment you might consider using the QueryPerformanceFrequency and QueryPerformanceCounter funtions.
Though i have to admit that those work even worse on my labtop :-)
---------------------------
I may be getting older, but I refuse to grow up
I assume your problems begin with the scale of .vX and .vY compared to the unit scales of the long type.
Assume a slow motion with a low value of .vX. Like vX = 0.01
so only every 100 frames the positions get updated. If your Player position cooridates do not correspond to pixels you get a jump every now and then. (every 5th second). Why not use float variables for the storage of your players position? Do not optimze to early.
Your other problem is probably the eTime variable. On most systems time() calls of the time.h show pretty disgusting behaviour: call time() often in a loop an print the results. You will notice that most of the values are the same, jumping up about 10 times a second, sometimes more. So most of your programm''s jerkiness comes from the used timing function. If you are working in a Win environment you might consider using the QueryPerformanceFrequency and QueryPerformanceCounter funtions.
Though i have to admit that those work even worse on my labtop :-)
---------------------------
I may be getting older, but I refuse to grow up
I dont think you understand the code!!
one frame:
//init
VFactor.vX = 0.7
VFactor.vY = -0.7
dVeloX =0;
dVeloY =0;
//First frame
dVeloX+= 0.7 //dVeloX = 0.7
dVeloY+= -0.7//dVeloX = -0.7
worldX+=(long)dVeloX //worldx + 0;
worldY+=(long)dVeloY //worldx + 0;
dVeloX-=(long)dVeloX //0.7 - 0
dVeloY-=(long)dVeloY //-0.7 - 0
//Second frame
dVeloX+= 0.7 //dVeloX = 1.4
dVeloY+= -0.7//dVeloX = -1.4
worldX+=(long)dVeloX //worldx + (1);
worldY+=(long)dVeloY //worldx + (-1);
dVeloX-=(long)dVeloX //1.4 - 1
dVeloY-=(long)dVeloY //-1.4 - (-1)
//end
now worldx have change 1 unit and worldy one less in y. I can’t understand how this is different than using float for player position. And this is no optimze its how I want it, I want a big world.
And who said im using time(), im currently using timeGetTime(), but im thinking of testing another time function.
[edited by - leadorn on May 17, 2003 4:33:41 PM]
one frame:
//init
VFactor.vX = 0.7
VFactor.vY = -0.7
dVeloX =0;
dVeloY =0;
//First frame
dVeloX+= 0.7 //dVeloX = 0.7
dVeloY+= -0.7//dVeloX = -0.7
worldX+=(long)dVeloX //worldx + 0;
worldY+=(long)dVeloY //worldx + 0;
dVeloX-=(long)dVeloX //0.7 - 0
dVeloY-=(long)dVeloY //-0.7 - 0
//Second frame
dVeloX+= 0.7 //dVeloX = 1.4
dVeloY+= -0.7//dVeloX = -1.4
worldX+=(long)dVeloX //worldx + (1);
worldY+=(long)dVeloY //worldx + (-1);
dVeloX-=(long)dVeloX //1.4 - 1
dVeloY-=(long)dVeloY //-1.4 - (-1)
//end
now worldx have change 1 unit and worldy one less in y. I can’t understand how this is different than using float for player position. And this is no optimze its how I want it, I want a big world.
And who said im using time(), im currently using timeGetTime(), but im thinking of testing another time function.
[edited by - leadorn on May 17, 2003 4:33:41 PM]
OK, fairly simple problem.
Go read a text book...
(long)dVeloX
Theres the problem, right there. If dVeloX is less than 1.0 (i.e. 0.9, 0.5, 0.1, whatever), it will be considered to be ZERO. In case you didn''t know, "long" means long integer (integer meaning whole number, no decimal data).
"And this is no optimze its how I want it, I want a big world."
He''s not saying "dont use long" because he''s limiting the size of the world; heck, floats can store very large numbers as it is (floating-point meaning the exponent can be specified to be quite large!)
-K
p.s. it''s late
Go read a text book...
(long)dVeloX
Theres the problem, right there. If dVeloX is less than 1.0 (i.e. 0.9, 0.5, 0.1, whatever), it will be considered to be ZERO. In case you didn''t know, "long" means long integer (integer meaning whole number, no decimal data).
"And this is no optimze its how I want it, I want a big world."
He''s not saying "dont use long" because he''s limiting the size of the world; heck, floats can store very large numbers as it is (floating-point meaning the exponent can be specified to be quite large!)
-K
p.s. it''s late
You don’t get it guys, read trough my last post again.
dVeloX is just temporary to store the decimals that the long integer cant store, I will extract the integer part from the float dVeloX and let the decimal be to next frame.
ex
float x = 0.1
cout << (long)x will print 0
float x = 1.1;
cout << (long)x will print 1
float x = 5.446;
x-=(long)x
cout << x // will print 0.446
[edited by - leadorn on May 17, 2003 5:05:50 PM]
dVeloX is just temporary to store the decimals that the long integer cant store, I will extract the integer part from the float dVeloX and let the decimal be to next frame.
ex
float x = 0.1
cout << (long)x will print 0
float x = 1.1;
cout << (long)x will print 1
float x = 5.446;
x-=(long)x
cout << x // will print 0.446
[edited by - leadorn on May 17, 2003 5:05:50 PM]
If you mean jerky movement then the most common couse is mot updating the screen regularly, in line with the monitor refresh rate. Perhaps the error is not in your movement code but the timing in the main loop. timeGetTime by the way should be fine, I use it with no complaints.
Mark
Cornutopia Games
http://www.cornutopia.net
Bytten Independent Games Magazine
http://www.bytten.com
[edited by - Mark Sheeky on May 18, 2003 3:59:12 PM]
Mark
Cornutopia Games
http://www.cornutopia.net
Bytten Independent Games Magazine
http://www.bytten.com
[edited by - Mark Sheeky on May 18, 2003 3:59:12 PM]
thank you :-|
---------------------------
I may be getting older, but I refuse to grow up
---------------------------
I may be getting older, but I refuse to grow up
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement