Jump to content
Banner by ~ Wizard
  • entries
    21
  • comments
    131
  • views
    2,911

The Maths Return!


Rikifive

1,889 views

And the blog starts with maths... :icwudt:

Shush, I have no idea how to blog! :D 

*ahem*

Welcome class! It's time for your favorite thing! MATHS! Isn't that awesome!? It's like the number one thing! :lol: Get it? Number...? ONE..? Maths??? No? :adorkable: Alright then, let's get to the lesson! ^_^ Run away while you can.

 

Joking aside~~

So yeah, some time ago I've posted a status update mentioning some maths to achieve this effect:
8HRdafz.gif

Long story short;
I wanted a formula, that raises as gets closer to a certain point, then decreases along with the distance.
The first thing, that came to my mind was a quadratic function, because their graphs seemed to be similar to what I was looking for. You may have seen these parabolas, where it was raising to some point, then going down. Just by that image my mind suggested me something...

"Hey, what if the top of the parabola would be the center and then the value would be decreasing the further it goes from that point? Wouldn't be that what you're looking for?"

And I was like
"Yeah! That should work!" :love: 

 

Even though I was thinking of some other possibilities, that would be even better (we'll get to this later), I decided to stick to my very first thought. (silly me, told ya I tend to unnecessarily make things more complex than they should :P )

So I've started thinking of a formula, that would do what I want and after some time I came up with this:

jsucCC1.png
>>> Putting into game...
WTtzCGj.png

 

What this formula does is to get the "Y" value from the function based on the icon position.
So point x=38 on the graph is where I want to put icons. Now I'd like to give them a smooth floating animation. To do that, I've decided to move them up and down by up to 6 pixels away from the center. It could be achieved in a really simple way, like move it up with a constant speed, then bounce it back and forth, with the same speed. Heh, that's actually what I did in my previous design ages ago...
IhWutqx.gif

That doesn't look bad, that's true. Thanks to very low distance (6 pixels in total; 3 from center), it's not really noticeable. It's just few pixels up/down after all.

 

Now that I'm working with a larger resolution, it would be nice to make it actually smooth and that's what the formula does. As you can see, the further the icons move from the center (x=38 on graph) the lower the value gets, which is what I needed.
So I've used that fancy calculation then moved the icons by the returned amount, eventually changing their direction when getting far enough.

That was a success.
The formula did exactly what I wanted.

However, there was a much simpler and more automatic way to achieve this. There is a similar graph, but instead of parabola, there's a wave.
Now since the wave doesn't end and goes up/down through positive and negative values all by its own, it could be easily used to replace a pretty big chunk of code.

Now guess what that wave is... Have you ever heard of sines?
W0kUd3w.png

Sounds like another silly thing to learn during math classes, but turns out these actually have some usage! :D 

It pretty much follows the same idea-
The horizontal line showing point 0 would be the center. Now as seen in the graph, it goes up and down with time.
At this point I could forget about the previous 'if' statements and all that stuff and just use the time and some maths of course.

I wanted to see how it all behaves in the software I use, so for some testing I've created a thingie, that draws the waves based on thrown formula, so brace yourselves, I present you the SINE SIMULATOR 2018! (also supports other functions, but who cares) :D 

Let's try the simplest one- the one shown in the image above.
F5ZgeOf.gif

Oh that's sooooooo coooool! It lacks of ponies though, I should have put some...  Everything works.  :twi: 

Also some other example (suggested by fellow developer in chat)
9IZAaBx.gif

:sunbutt: -{ Uuuuuh fancy! )

I know, right? :D Still lacks of ponies though, perhaps in future updates of that useless program-thingie...

 

Okay, so all I had to do was to follow the damn train, CJ - not this again, my bad! :D --- Was to use these values for the icons.
For that, I came up with a pretty much the same formula as before, but using sines this time.

icon[i].y = 38 + 6 * sin(icon[i].timer);
icon[i].timer += 0.05

So basically:
38 - is the center
6 - is the amplitude - the maximum distance they can travel from the center
and then the timer (0.05) - basically represents the x position increment in each frame - the higher the value, the faster it goes.

 

So yeah, the code has been shrunk to only two lines and it gives an extremely accurate result! :yay: 

gtxQNG1.gif

 

The only one question is... Why did I waste the whole weekend on toying with this! :derp: Such a relatively simple thing, a tiny piece of code, yet I had to do science for hours anyways... :P 

 

Well, either way... :icwudt:

jqpdJjI.png

 

And with that being said, you're free to go now! ^_^ If you haven't run away already. :D 

  • Brohoof 6

9 Comments


Recommended Comments

That's an awfully high amount of math you had to go through for the whole weekend, but surprisingly I think it was worth it! The end results look really nice, you did a very good job!

  • Brohoof 2
Link to comment

I see what you mean about getting an idea stuck in your head - I was nodding along until you got to the sine functions, then I felt really stupid for not thinking of them instantly. And at least you thought of a sensible function; even though I knew that it wasn't the objective, I kept thinking of f(x) = { 1/ |x|  | x =/= 0; [icon removed] | x = 0 } and seeing the ponies whizzing skywards in my head (suitable for a gang of Pinkie Pie clones perhaps.)

That final piece of code is a work of beauty though - concise, effective and efficient. 

  • Brohoof 2
Link to comment
1 hour ago, Once In A Blue Moon said:

I see what you mean about getting an idea stuck in your head - I was nodding along until you got to the sine functions, then I felt really stupid for not thinking of them instantly.

Heh, actually I felt stupid as well. I've bumped into sines before getting to the quadratic function, yet I ignored that and continued with unnecessarily complex stuff. :P 
 

1 hour ago, Once In A Blue Moon said:

And at least you thought of a sensible function; even though I knew that it wasn't the objective, I kept thinking of f(x) = { 1/ |x|  | x =/= 0; [icon removed] | x = 0 } and seeing the ponies whizzing skywards in my head (suitable for a gang of Pinkie Pie clones perhaps.)

It took me ages, but yeah. I was manually solving things on paper, breaking things into parts trying to figure out what where should be put, until I eventually came up with something, that worked. It would take years if not the help from an online tool, that was drawing the graphs for me, so I had a preview whenever adjusted anything. :P
That's funny you mention that, because when I tried to put the function in the game, it actually gave funny results at first, due to wrong translation of the formula into code - kinda like typos, but in math. Sometimes interesting things happen when I'm making mistakes. :D

1 hour ago, Once In A Blue Moon said:

That final piece of code is a work of beauty though - concise, effective and efficient. 

Initially I didn't even know it will be that neat, but that's a nice surprise indeed. :) 

1 hour ago, BronyNumber42licious said:

Math gives me the feels. :please:

It sometimes does. :icwudt:

Link to comment

That was actually a fun read, why can't math class in school be this interesting? :P I guess everything is better with ponies! :D 

Edited by SparklingSwirls
  • Brohoof 1
Link to comment

@SparklingSwirls It's surprising how having interest in the superficial 'skin' applied to a problem can make it more engaging - one need only look at the game 'Kerbal Space Program' to see just how many people ended up doing literal rocket science because they slapped some amusing Minon-like creatures in the game. 

Still, if you liked that, have a look at this:

Spoiler

eKStb.png

I was actually explaining confidence intervals to someone, in the context of yoghurt production as it happens, and to refresh my memory I had a quick look through this as it was on my tablet at the time. Thankfully they didn't ask to see the notes I was reading!

 

  • Brohoof 2
Link to comment
18 hours ago, SparklingSwirls said:

That was actually a fun read, why can't math class in school be this interesting? :P I guess everything is better with ponies! :D 

I agree! ^_^ 

11 hours ago, Once In A Blue Moon said:

It's surprising how having interest in the superficial 'skin' applied to a problem can make it more engaging - one need only look at the game 'Kerbal Space Program' to see just how many people ended up doing literal rocket science because they slapped some amusing Minon-like creatures in the game. 

Visual presentation always makes things more interesting, as it entertains our imagination I suppose, especially if things are explained in a way we like and can relate to. ;) 

KSP is indeed a great example.

  • Brohoof 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Join the herd!

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...