2
\$\begingroup\$

I have a game object (player), and with device screen width and average FPS, I'm able to calculate exact velocity for the player to reach whole screen from left to right in a specified time (fx. for 1 second). Objective of this is to move player on different devices still same (reach for 1 second their screen width).

I can calculate this in runtime. But is it somehow possible to calculate it before, for example in surfaceCreated? Because in my game, I need that velocity to be constant from start (I can't change it in runtime). Is possible to approximately calculate it?

\$\endgroup\$
7
  • \$\begingroup\$ One clarification question: why couldn't you just calculate it when the game is initialized, i.e. at the Start()? \$\endgroup\$
    – MAnd
    Nov 23 '15 at 20:04
  • \$\begingroup\$ Because there are some things which requires it earlier, and they are used already before Start() (yes, I've messed up code when I was writing it, but now it's no way back). \$\endgroup\$
    – johnyX
    Nov 23 '15 at 20:07
  • \$\begingroup\$ And why don't u use 'delta' time? \$\endgroup\$ Nov 23 '15 at 20:11
  • \$\begingroup\$ I can calculate delta from main thread, but only when it is already running. Edit: I have to somehow get velocity before game graphics are drawed. \$\endgroup\$
    – johnyX
    Nov 23 '15 at 20:15
  • \$\begingroup\$ @AhmetZambak Delta time is irrelevant here because the OP wants to calculate the future velocity/speed that is needed for the movement, and wants to do that before frames start to update. \$\endgroup\$
    – MAnd
    Nov 23 '15 at 20:46
4
\$\begingroup\$

First, a side note: you seem to need the speed, not the velocity. And since it's constant, you don't need the average speed, since that's the same as the speed.

Second, an advice: per your question and responses to comments, it really seems that you should be re-factoring the code of your game. There is almost always a way back from a messy setting, and not being able to due what you want simply in the Awake() or Start() parts of the game seems like the whole thing will give you future troubles nevertheless.

Third: besides the option of loading into your game a previously baked data, there is no such a thing like a calculation being really done before run-time. Run-time means time during which processor is running your game, and processor running is a necessity for you to make a calculation.

The problem, in your case, with loading a previously calculated data with information when the game scene Awakes(), is that you would have to be sure that you cover all and any screen resolution of all and any devices possible that could be running your game.

Fourth: one workaround that you could use is the following. Create an additional scene for your game (like an additional level) that will become the very first one. Then, you make the calculation in it normally, in the Start(). After the calculation, you just end such scene/level and call the next, which will be the real one you already have. Of course, keeping the result from the calculation from first scene/level to the second, as a global variable or class. That would take milliseconds, since the calculation you seem to need looks simple and inexpensive.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.