Using my Timer Class with my Object Class

Hello all,

Up until this point I had no problem with my 3D Mesh Object class I used for my game engine, however, the larger and more advanced my game engine is becoming is obviously forcing me to, well, code it like an engine. So I'm making a .cpp/header for every class. One of these classes is my Objects class.

Since there's no way I have enough characters for this in my post, here's the code: https://pastebin.com/p5jDiVhx

Now, having std::vector <Timer> Timers(NUM_OF_OBJECTS) worked perfectly in my main source file, so I'm guessing this has a lot to do with scope. For example, if I used

void Object::RotateOnTimer(float x, float y, float z, float time)
{
if (x)
Rotate(x + rot, 0);
if (y)
Rotate(y + rot, 1);
if (z)
Rotate(z + rot, 2);
if (Timers[iIndex].WaitMilliSeconds(time))
rot += 0.005f;
}

It worked perfectly. But by putting my class into its own cpp and header, I this is no longer possible. Only one object will rotate. It will only rotate Timers[0] of my vector and nothing after it. Look in the pastebin comment I made at the very bottom, I can get the others to rotate by simply using if(!Timers[iIndex].WaitMilliSeconds(..)) then they will rotate as well, which means it's returning false with every function in my Timer class within the vector except for the very first(0).

I can compensate and make my all objects rotate by literally copy and pasting the identical classes, Timer, Timer1, Timer2, Timer3, literally make different classes that are identical. Then it works. But that's hardly a solution.

What's going on here?
Timer::WaitMilliSeconds() is so bad, man. What were you thinking?
1. Do you realize that your class has no state? The function could have simply been a global function.
2. Since every variable used by the function is static, all the state is shared among all instances of Timer. Every static variable in the function should have been non-static members of the class.
3. Compared to the previous points this is basically nitpicking, but it's still important: GetTickCount() doesn't have sufficient resolution for use in a game engine. The documentation states that it's 10-15 ms. At 60 frames per second, that's between 60% and 90% of a frame. You need 1 ms resolution or better.

Why is ObjectTimers a global? In fact, is there any point in putting all the Timers in a vector and then having each object reference a Timer by a unique ID? Why not just have the Timer be a member of an Object?
Last edited on
The timers are global because they're within the small MapObjects.cpp file.

Making the members non-static makes it so no objects rotate whatsoever. And yes, I've literally made a Timer member within the Object class, to no effect--Only the very first object rotates. This is why I'm so confused, if I have a Timer class member within each 3D Object class, every unique ID i use for those objects(iIndex) *still* makes it so only the very first 3D object I have (which is std::vector <Object> MapObjects(NUM_OF_OBJECTS);) only MapObjects[0].RotateOnTimer(..) will actually rotate.

I've tried everything you suggested prior to coming here. I've made a vector of my Timer class in the same manner, std::vector <Timer> Timers(NUM_OF_OBJECTS), and if you look at the bottom of my pastebin code, you'll see how I implemented it: if(Timers[iIndex].WaitMilliseconds(time)) {.. }
and it still yields the exact same result. ONLY the first object rotates.

This is why I'm so baffled.
Last edited on
I don't know about those other attempts, I'm talking about the code you've shown here. As it is right now, your code is obviously broken. Fix everything I mention in my previous post (you can ignore GetTickCount() for now) and then we'll talk. Trust me, it will objectively be an improvement.
Last edited on
After reading your posts I realized that since they were static members, I couldn't differentiate the spinning rates between 3D Objects, so that was just bad design overall.

This is my new attempt: https://pastebin.com/VVjsptLz

Now, Objects[x].RotateOnTimer(..) is called in my DrawScene() function, which is being called in a loop a fast as the CPU can. Now I can finally set down different speeds for different objects, however, it seems with this implementation that if I want a nice fluid streamlined rotation of an object, I use rot += 0.005f, and calling that with time passed as just 1, or even 0.00001f, it rotates nicely, but not quickly. To compensate for this, I can use a different increment for rot, such as rot += 0.05f, which will make one object spin very nice and fast/fluid, but the others that are set on a lower timer setting look very very choppy due to the larger increment. Is this due to how fast my functions are being called, or am I using this new stopwatch class improperly somehow--I need to be able to rotate objects fluidly at both a fast and slow rate, so I want the increment to be as small as possible. But at 0.005f, it only goes so fast, and it seems that's the maximum spin rate I can obtain.

How can I go about this implementation differently to make sure everything spins fluidly?

Here's a picture of one of the objects that is rotating in a choppy manner: https://cdn.discordapp.com/attachments/642116703114887201/701586447114698762/unknown.png
Last edited on
Nevermind! Someone (alldayeveryday) on freenode's ##C++-general told me, why aren't you just using your delta variable that you're using for your speed? Why I didn't make this connection myself I'll never know. The brainfarts of brainfarts. Now I'm 100% good.
Topic archived. No new replies allowed.