I am messing around with some Javascript game development, trying to create a simple Framework for drawing a playing field (for a sports game).
I am horrible at math and really need help with this algorithm.
Say I have these variables:
GAME_WIDTH = 1024;
FIELD_LENGTH = 170;
PLAYER_SPEED = 4.72;
update_duration = 30;
The first 3 in caps are variable, meaning they can be set by the user. The last is essentially how often my “update” code is called in 1 second.
GAME_WIDTH is pixels, FIELD_LENGTH is meters, PLAYER_SPEED is how many seconds a player can cover 40 meters.
What I want to do, is take those 4 variables and translate that into how many pixels a player can move in 1 “update”. I have never really done any Game Development and I never payed attention to Geometry or Trigonometry in school so I am completely at a loss here.
Can anyone help?
EDIT
This is a 2D top-down game so this algorithm is only dealing with X (right-to-left) coordinates.
You need to embrace the fine art of “Units Conversion”.
http://oakroadsystems.com/math/convert.htm
For example, 12 inches = 1 foot. So you can convert inches to feet like so:
48 inches * (1 foot / 12 inches) = 4 feet. The “inches” cancel out, leaving just feet.
So the answer we want is pixels per update. (Is update_duration in milliseconds? assuming so…)
or if the 30 is updates/second, same idea:
(I may have misinterpreted the meaning of your constants… but that’s the technique. The concept is that each of those fractions, a / b, is equivalent to “1”.
This looks better when you write it in full classroom fraction style, all the canceling becomes chalkboard-perfect. Very useful technique to embrace.