Game Development Community

Different scene objects scroll at different speeds?

by Michael Vargas · in Torque X 2D · 04/14/2007 (11:32 pm) · 4 replies

I have two T2DSceneObjects, each from a different material, and both initialized to have an X position of 0. Each contains the same Torque component which implements the IAnimatedObject interface.

In the UpdateAnimation() method of this component, I simply add a fixed amount to the owner T2DSceneObject's x coordinate.

To put it shortly, I am trying to scroll both T2DSceneObjects to the right by a small amount each frame. But inevitably one of the scene objects always scrolls faster than the other. I've been debugging this for a while, but it still eludes me. My initial thought was that UpdateAnimation() might be getting called more often for one object than the other somehow, but I have a counter at the end of that function and it remains the same for both objects. Even if I run the program for a few seconds, then set a breakpoint in UpdateAnimation(), I'll see that:

1) UpdateAnimation() was called the same number of times for each object (in other words, their positions were offset to the right an equal number of times)
2) Their positions will *differ* nevertheless! One of them will get larger faster, *unless* I'm stepping through each frame in which case I never notice a difference.

I'm trying to determine if this is a bug in my code or in the engine.

Any help would be greatly appreciated.

Thank you,
Mike

Edit: Here's a bit of additional info that may be relevant. One of these objects is a T2DStaticSprite and the second is a T2DTileLayer. The slower moving one is the T2DStaticSprite.

#1
04/15/2007 (4:42 pm)
It's hard to say if the bug is in your code without seeing your code. How are you calculating the offset during each frame?
#2
04/16/2007 (12:48 am)
This alone was enough to reproduce the problem. scrollableBackground is a T2DSceneObject. For whatever reason, even though I'm adding 0.01 each time, sometimes the position only changes by 0.0037 or so. No other components are changing the background objects' position.

The 0.01 is arbitrary; any value seems to reproduce the problem.

public void UpdateAnimation(float dt)
{
Vector2 backgroundPosition = this.scrollableBackground.Position;
this.scrollableBackground.Position = new Vector2(backgroundPosition.X + 0.01f, backgroundPosition.Y);
}
#3
04/16/2007 (3:34 am)
@Michael:

I don't see you taking the dt value into account, which may be the reason it is not staying in sync.

I take it dt is delta time or elapsed time so you should really have something like this instead:
(Ive seperated it out into variables as a better example of what I mean)

// velocity - e.g. pixels per second or world coords per second
float velX   = 0.01f; 

// add the amount of velocity to apply over the elapsed time
float newX = backgroundPosition.X + velX * dt; 

this.scrollableBackground.Position = new Vector2( newX, backgroundPosition.Y );
#4
04/16/2007 (8:19 pm)
I thought about that, but considering that my implementation only ever adds 0.01 to the background's position every time UpdateAnimation is called, I don't understand why sometimes the new X value ends up only 0.0037 (or so) more instead of 0.01. It's like somewhere along the line it's interpolating something, but I don't know where.

I ended up working around the problem by calculating what the absolute position of the background should be, rather than incrementing in each call to UpdateAnimation.