Mulscale: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
No edit summary
Fox (talk | contribs)
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 11: Line 11:
The main difference is that the calculation uses 64 bits to prevent overflow, while using gamevars for intermediate values would limit it to 31 bits.
The main difference is that the calculation uses 64 bits to prevent overflow, while using gamevars for intermediate values would limit it to 31 bits.


One example of where this limitation takes places is with [[rotatesprite]] scaling, where 320*65536 scaled with [[statusbarscale]] is dangerously close to 31 bits.
For example, [[sin]] or [[cos]] use a scaling factor of 1:16384, corresponding to a 14 bit shift. So if you wanted to scale the result with a certain length, you would do:


Another example is how the trigonometric commands [[sin]] and [[cos]] use a fixed-point fractional scaling factor of 1:16384, corresponding to a 14 bit shift. So, if you wanted to multiply some ''length'' by the cosine of the current sprite's angle, you could write:
  sin ''Sine'' sprite[].ang
  '''mulscale''' ''Result'' ''Length'' ''Sine'' 14


  cos ''cosang'' sprite[].ang
See also [[divscale]] and [[scalevar]].
  '''mulscale''' ''result'' ''length'' ''cosang'' 14


[[Category:EDuke32 specific commands]]
[[Category:EDuke32 specific commands]]
[[Category:Gamevar manipulation]]
[[Category:Gamevar manipulation]]

Latest revision as of 12:09, 14 November 2019

mulscale <Result> <Factor 1> <Factor 2> <Right Shift>

In C terms, this command performs roughly the expression <Result> = (<Factor 1> * <Factor 2>) >> <Right Shift>.

In CON terms, it is very similar to the following series of commands:

set <Result> <Factor 1>
mul <Result> <Factor 2>
shiftr <Result> <Right Shift>

The main difference is that the calculation uses 64 bits to prevent overflow, while using gamevars for intermediate values would limit it to 31 bits.

For example, sin or cos use a scaling factor of 1:16384, corresponding to a 14 bit shift. So if you wanted to scale the result with a certain length, you would do:

 sin Sine sprite[].ang
 mulscale Result Length Sine 14

See also divscale and scalevar.