Advertisement

How do game programmers visualize the Maths on their game?

Started by March 14, 2015 01:37 PM
12 comments, last by Buster2000 9 years, 9 months ago

I am currently messing up with the menu systems code I downloaded from MSD page.

It was built on XNA.

HUD.jpg

this is the code the makes it pulsate


public virtual void Draw(MenueScreen menuscScreen, bool isSelected, GameTime gameTime)
        {
            Color color = isSelected ? Color.Yellow : Color.White;

            double time = gameTime.TotalGameTime.TotalSeconds;
            
            float pulsate = (float) Math.Sin(time*2) + 1; 
            float scale = 1 + pulsate*0.05f*selectionFade;  
            
            ScreenManager screenManager = menuscScreen.ScreenManager;
            
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Font;
            Vector2 origin = new Vector2(0,font.LineSpacing);
            spriteBatch.DrawString(font,text,position,color,0,origin,scale,SpriteEffects.None, 0);

        }

This is the math part i dont get on why did the author use,


float pulsate = (float) Math.Sin(time*2) + 1; 

How did the programmer know if he is going to use Sin or Cos? I tried replacing it with cos still the same effects.

Sin and cos and tan will be on trigonometry part of math but where will you visualize it on screen? Even If i think that the screen is made up of many triangles or two triangles like this

triangle.jpg

or like this

triangle_2.jpg

I still dont know what triangle did the author use cause theres a lot of triangle that a rectangle is compose of.

and also for the effects i cant figure out what is the connection of using trigonometry on a text to make it smaller and bigger when selected. its really confusing.

Is that some random things that the original author use to confuse people instead of just putting a constant number?

ooookkkkkaaaayyy


This is the math part i dont get on why did the author use,
float pulsate = (float) Math.Sin(time*2) + 1;

Although it's a trigonometric function, it's just a convenient way to get a smoothly varying value over time from the available math library. It has nothing to do with the geometry on-screen. Using sine vs. cosine? For that purpose, it makes no difference. Both functions vary over time in essentially identical ways; i.e., they both vary from -1 to +1.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Advertisement

This is the math part i dont get on why did the author use,


float pulsate = (float) Math.Sin(time*2) + 1; 

How did the programmer know if he is going to use Sin or Cos? I tried replacing it with cos still the same effects.

Sin and Cos will always return a value between -1 and 1. This behavior is cyclical based on the fundamental property of the sine and cosine functions. The main difference between the two is the horizontal offset. The following graph will illustrate this:

450px-Sine_cosine_plot.svg.png

By passing an ever increasing value (in this case time*2), the result of either Sin or Cos will return a cyclical value moving between -1 and 1. And this results in a smooth pulse.


How did the programmer know if he is going to use Sin or Cos?

In this case it wouldn't matter indeed but the idea here is to output a value that continually oscillates between 0 and 1 smoothly.

Both sin & cos do this but dephased by 90°

The question a programmer would've asked himself here is "what function will perpetually output values that oscillate between 0 & 1 and back down like this:

http://upload.wikimedia.org/wikipedia/commons/0/02/Simple_sine_wave.svg

Oops it's Saturday morning: I mean -1 & 1, not 0 & 1


How did the programmer know if he is going to use Sin or Cos? I tried replacing it with cos still the same effects.
Sin and cos and tan will be on trigonometry part of math but where will you visualize it on screen? Even If i think that the screen is made up of many triangles or two triangles like this

He is using sin only to generate a simple oscilation in function of the time (independent variable). It could be generated with a cos evaluation in the same way, basically.

Both sin and cos (that are trig functions; transcendental) are used to help modeling some sword of phenomena in mathematics and help solving a wide range of problems that are related to circunferences. While there are a big concept behind it, this is called mathematical modeling.
But in games there's no rule to follow in this simple specific case (UI). Certain areas such graphics, physics needs to follow a more mature mathematical approach.
How do a programmer visualize math?
What makes a ball oscilates it is the code that is behind the big picture.
Graphics programmers recognizes certain type of lighting model because they know how to apply these models and of course knows the results it generates.
Physics programmers can recognize if a game is using a physics engine to simulate the objects behaviour in function of time because they do physics.
I think your question is: How to a programmer applies certain behaviour to a game using mathematics? The answer is simple: using what mathematics supplies for you (applying the mathematics), most of the time using mathematical functions.
The game programmer then look at the x and y values of the cartesian coordinate space and he see that he want to generate a smooth behaviour of something. Then he sees that such simple tasks could be generated using such trig functions or related for its simplicity. Then he passes a value to the function uses that value to position/orient/ something on the screen.

How did the programmer know if he is going to use Sin or Cos?


Try graphing it Then reason through it looking at possible inputs to the function and see how the effect the output.
My current game project Platform RPG
Advertisement

This is the math part i dont get on why did the author use,


float pulsate = (float) Math.Sin(time*2) + 1; 

How did the programmer know if he is going to use Sin or Cos? I tried replacing it with cos still the same effects.

Sin and Cos will always return a value between -1 and 1. This behavior is cyclical based on the fundamental property of the sine and cosine functions. The main difference between the two is the horizontal offset. The following graph will illustrate this:

450px-Sine_cosine_plot.svg.png

By passing an ever increasing value (in this case time*2), the result of either Sin or Cos will return a cyclical value moving between -1 and 1. And this results in a smooth pulse.

So this more about the unit circle?

ooookkkkkaaaayyy


So this more about the unit circle?

Yes, kind of. Imagine your origin at 0,0 and a given angle a. You want the coordinate on the circumference of the unit circle centred at the origin, at angle a.

sin(a) is the amount to move across the x axis to the point.

cos(a) is the amount to move across the y axis to the point.

Simple as that really, but the possible uses for this are endless.

[EDIT] I should clarify, this isn't really what sine and cosine are as such, just the way I find them easiest to visualise. What they actually are are graphing functions as shown above.


I should clarify, this isn't really what sine and cosine are as such, just the way I find them easiest to visualise.

You could actually say that is exactly what sine and cos "are", since it is one of the definitions.

http://math2.org/math/algebra/functions/sincos/definition.htm

When playing with functions Quilez's GraphToy is quite handy.

This topic is closed to new replies.

Advertisement