Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Acting differently?

Started by CirdanValen Sep 16, 2010 at 4:11 PM 0 replies 450+ views
Original Post
CirdanValen
CirdanValen
Last night I decided to sit down and work out frame independent movement, and was successful. So this morning I moved the code over to my game, and it's not working. [Java]

In my PhysicsTest I wrote last night, I have it setup similar to the "Fix Your Timestamp" article that is floating around.

(stupid thing is removing the + charater)
			long now = System.currentTimeMillis();			long diff = now - previousTime;			previousTime = now;							accumulator (plus)= diff;						if((now - fpsStart) >= 1000) {				fpsStart = now;				fps = fpsCounter;				fpsCounter = 0;			}									while(accumulator >= TICK_SKIP) {				prev = x;				// game logic				if(x >= 450) {					dir = -1;				} else if(x < 0) {					dir = 1;				}				if(isMoving) {					x (plus)= dir*8;					}				accumulator -= TICK_SKIP;			}

And interpolation:
			float alpha = accumulator / TICK_SKIP;			float newX = x*alpha (plus) prev * (1.0f - alpha);


What I did in my test is I drew a red rect for the previous position, drew a blue rect for the current position, and drew a white rect for the interpolated position and it works perfectly. The red box is slightly behind, the blue box is slightly ahead, and the white box is in the middle moving as smooth as silk.

So I ported this over to my game, with a little differences.
	public Coord interpolate(float alpha) {		Coord newCoord = new Coord();		newCoord.x = mCurrentCoord.x*alpha (plus) mPrevCoord.x * (1.0f - alpha);		newCoord.y = mCurrentCoord.y*alpha (plus) mPrevCoord.y * (1.0f - alpha);		return newCoord;	}	public void translate(float x, float y) {		mPrevCoord.x = mCurrentCoord.x;		mPrevCoord.y = mCurrentCoord.y;		mCurrentCoord.x (plus)= x;		mCurrentCoord.y (plus)= y;	}


Every logic tick for the player:
translate(8*mDirection, 0.0f);


In the draw method I called
Coord newCoord = interpolate(alpha);

and draw a box at the new position which should be in between the old position and the new position.

The problem is, in my game when I draw the three boxes, they all align in every frame. The previous position, the current position, and the middle position are all the same and the movement is not smooth at all. I don't understand why this is happening. Both programs are running at identical FPS. In both cases, I set the previous location every logic tick so I don't think the problem is there. Do you see something that I don't? The biggest difference I see here is that my game loop calls screen.update() which calls player.update() which then calls translate. Same path applies for drawing also, but I don't think these extra calls is causing the problem.

CirdanValen
CirdanValen
Solved. The problem is because of passing by reference. I had to put
		mPrevCoord.x = mCurrentCoord.x;		mPrevCoord.y = mCurrentCoord.y;

To set the value. If I put mPrevCoord = mCurrentCoord it would make mPrevCoord point to mCurrentCoord, so when mCurrentCoord changed, mPrevCoord would change automatically.

One reason to love lower level languages. This stuff is more explicit.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.