1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

Maths .-.

Discussion in 'Modding Help' started by Monkey_Feats, Jul 27, 2016.

  1. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    Can someone please explain to me what the math does?

    angle = Maths::get360DegreesFrom256(this.get_u8("angle"));

    Cheers
     
  2. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    Maths is actually a namespace.
    Click me for more information about what namespaces are and what they're used for.
    If you have a look on the global functions inside the Functions.txt ("..\KAG\Manual\interface") you'll find a bunch of other namespaces such as:
    You can find other namespaces declared in some scripts as well, here is a tiny example hoping that it could help you understand what I mean.
    upload_2016-7-27_14-31-56.png
    which had been used later in the line 77, this way.
    upload_2016-7-27_14-33-32.png
    as you can see shielding is actually a part of the namespace KnightStates.
    PS: The script we used as an example was "KnightCommon.as" (inside "..\KAG\Base\Entities\Characters\Knight"), feel free to have a look on the other classes' common scripts since they use lots of other namespaces.
     
  3. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    Uh Sorry think I wrote it wrong, let me rephrase it. What does this math equation solve?
    the "get360DegreesFrom256" bit I meant, nice links bro ::D:
     
  4. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    lol, ok.
    So it basically gives you the angle (360 degrees) from a uint8 (Unsigned 8 bits integer).
    as you know a uint8 would have 256 values (0..255) and thus we can't use it to get angles (0..359) [360 values] so the solution is using that.
    Example: 360/256 = 1.40625 (think about it like this, 256*number=360, so what is that number supposed to be?).
    Hence, Maths::get360DegreesFrom256(1) is actually equal to 1.40625.
    Maths::get360DegreesFrom256(2) is actually equal to 2.8125.
    and Maths::get360DegreesFrom256(255) would be equal to 358.594 (almost 359 ).
    tl;dr it helps converting from (0..255) to (0..359) by multiplying a uint8 by 1.40625 to give a float type result.
    PS: Keep in mind that Maths::get256DegreesFrom360(x) does the opposite, where x is a float type.
    Hope this helps.
     
    Last edited: Jul 27, 2016
    Osmal, cincoscuencas and makmoud98 like this.
  5. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    Simple enough, so it converts a float to a value that is an acceptable 8bit value?
    Why does one need to convert a number to an 8bit integer? To work with the engine?
     
  6. cincoscuencas

    cincoscuencas Ballista Bolt Thrower Staff Alumni
    1. Angels of Death - [AoD]

    Messages:
    41
    By the looks of it, the conversion is needed so the thing can understand it. If not, then there wouldn't have a need for conversion.
     
  7. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    Cinc you ball bag.
     
  8. cincoscuencas

    cincoscuencas Ballista Bolt Thrower Staff Alumni
    1. Angels of Death - [AoD]

    Messages:
    41
    why are you such a dangleberry?
     
  9. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    And vice versa.
    Performance-wise reasons mainly, I believe.
    To simplify the explanation, let's look at the situation below:
    Monkey and Cinc chat with each other on a daily basis, they thought about two possible (old-fashioned) ways of communication:
    1. Exchanging their daily message on a new tiny piece of paper (a new piece of paper every time they have a new message to send).
    2. Exchanging their daily message on a new big, heavy book (a new book every time they have a message).
    Obviously, the best way would be using the light-weighted paper instead of the heavy book.
    Same goes for KAG, the server and the clients need to synchronize the angle value at times. It would be much better to send (and receive) it as a uint8 value instead of a float one, simply because the size of a float type is far more bigger than a uint8 type; and as a matter of fact, smaller variables are transmitted faster than bigger ones (not to mention saving network data) and hence having better performances.
     
  10. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    Gotcha, computers can pull numbers faster in bits :o Thanks for the answers champ!
     
  11. Verrazano

    Verrazano Flat Chested Haggy Old Souless Witchy Witch Witch THD Team Global Moderator Forum Moderator Tester
    1. Practitioners of War Extreme Revolution - POWER

    Messages:
    477
    All things on computers are stored in bits. It's just storage size that's more convient. 8bits verse 32bits
     
    Asu, Monkey_Feats and blackjoker77777 like this.
  12. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    I think this is going to be one of them, the more questions you ask, the more questions you have scenarios.
    Just one more...
    Is it just the number that is bigger, resulting in a loss of computer processing? or is there some other magic going on?
    why is 1.40625 faster to call than 1.0?
     
  13. SlayerSean

    SlayerSean FYI: it's pronounced "seen"

    Messages:
    191
    Given how computers work, the explicit value "1.0" would also be of type float, since there is an explicit decimal point there, and as such the two numbers would be the same size and take the same time to transmit, both being 32 bit floating point numbers.

    The difference between 1.40625 and 1 though (no decimal point), is that 1.40625 must be stored as a float and so takes 32 bits of space, but "1" can be stored as an 8 bit unsigned integer, which takes 4x less space (8 vs 32). As such, it's the difference between sending 32 bits of information over a network and 8 bits of information, clearly 8 bits is less and so would take less time to transmit. This difference is more important if you consider how often that information is transmitted and how many are transmitted. i.e. Ten 8 bit numbers ten times a second, or ten 32 bit numbers ten times a second - 800 bits vs 3200 bits in that scenario.
     
  14. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    Nothing's wrong about asking questions.
    No, it's not actually that.. and to be frank I couldn't have said it better than Verra:
    Let's say you have 2 pieces of paper, the same size and the same weight.
    Even If you write on the first one "9999" and write on the second one "0", their weight and size won't change, thus they're basically the same when it comes to interacting with them (carrying them?).
    Having a small piece of paper in which is written "90" and a big heavy one with "1" written on it, still means that the second one is harder to deal with than the first one (see: size and weight).
    everything in a computer is stored in bits, a bit value could either be equal to 1 or 0.
    a uint8 is composed of 8 bits, hence we get 8 cases reserved for that variable.
    upload_2016-7-29_0-44-16.png
    a float type requires 32 bits, which means 32 cases that get reserved for that variable.
    upload_2016-7-29_0-44-41.png
    As you can easily notice the float type requires 4 times the amount of cases required by a uint8 variable.
    So since it's an available solution we could use a uint8 type to reserve the least possible amount of cases without actually affecting the way our variable would work. By doing so, we can reduce the amount of data transferred to assure the synchronization of server and a client in a fast efficient way.
    @Edit: Haha, it looks like SlayerSean ninja'd me. :^)
     
    Last edited: Jul 29, 2016
    SlayerSean likes this.
  15. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    Ahh, I was thinking the number 1.40625 was being used in it's entirety, but it's actually being seen as a 1?
     
  16. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    Just chiming in on the actual reasoning here from dev perspective;

    Red herrings - these 2 are NOT why we are storing angles as a u8 in this case:
    • Performance
      Actually worse as you have to do quite a bit of work pulling the u8 value out, converting it to a float in the range we want, doing the actual work on it, then converting it back, and then storing it in the u8 property again.
      Properties themselves are very slow and that work will most likely dominate in this case.
      Sidenote: these days computers have a much easier time working with floats than they used to as well, the "conventional wisdom" that seems to be driven a bit hard on new programmers that "floats are slow" is at least 10 years out of date. We've had super-scalar single cycle floating point ops for a very long time on desktop.
    • Memory usage
      "very slightly better" I guess but we're not talking huge size savings here unless these were stored in some huge array (which they aren't in this example) - properties have an enormous amount of separate memory overhead that is often forgotten.
    Actual reason:
    • Networking Bandwidth saved (as Sean touched on)
      We want to send as little as possible over the network. The size saving of 4 bytes to 1 byte might seem big, but it's actually 8 bytes to 5 bytes, because we also have to send the property ID as well, and that isn't even counting all the overhead involved in setting up a property change packet.
      The real bandwidth savings come from not sending the change for every bit changed in the float - because we don't really care about that, it could be a change from 2.00000 to 2.00001.
      Because Sync() only sends anything if the value has changed, we only send changes for anything a little bit over one degree. This can be the difference between sending the change every single frame, to sending it only when it matters.
    Bonus Round:
    • This is done incredibly inconsistently throughout the codebase; in many cases we sync a whole lot more data a whole lot more often than we need. This is a case of micro-optimisation but apparently it was considered worth doing at the time.


    Glad to see some collaboration within the modding scene in any case :)
     
  17. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    I have a new maths question. How would I calculate a blob's x and y velocities relative from it's angle?
    triggermeomitry?
     
  18. cincoscuencas

    cincoscuencas Ballista Bolt Thrower Staff Alumni
    1. Angels of Death - [AoD]

    Messages:
    41
    *ba dum tss*
     
    Nicuwins likes this.
  19. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    Do you mean like, relative to its angle?
    You actually very rarely need to directly touch trig when working with vectors.

    So, here's a picture
    [​IMG]
    You've got the blob's angle relative to "normal orientation" right? And you've got the velocity in "normal orientation".
    To get the velocity in "blob orientation", we need to reverse the blob orientation. We can do this just but rotating the vector by the negative of the blob's angle.

    As such, if we have the blob rotated like so (minus 20 degrees)
    [​IMG]
    We can get the vector (1,0) relative to that blob's orientation like so:
    [​IMG]

    (Note I'm not giving you copy-able code so you have to think about it at least a little bit)
     
  20. cincoscuencas

    cincoscuencas Ballista Bolt Thrower Staff Alumni
    1. Angels of Death - [AoD]

    Messages:
    41
    Once the velocity vector is already orientated to the correct angle, then you can now break the velocity vector into x and y velocity vector. This is now where you use trigonometry if doing it by hand knowing the velocity vector and the angle of orientation.