A Bit Of Helpful Maths To Make Universal Builds Simpler!

Hi Guys,

With the introduction to Universal Builds, a lot of us are having to add additional code into our games. There is a simple way to set a single rule to cater for the several options, at the moment most of us are doing the following,

(KEY: ssh = game.Screen.Size.Height)

if ssh = 480
....

if ssh = 568
....

if ssh = 1024
...

We can eliminate the need for placing all of these rules with a single behaviour! Say we wished to do the following,

if ssh = 480
change positiony to 50

if ssh = 568
change positiony to 70

if ssh = 1024
change positiony to 100

We can use the following instead, first we need to set the default screen size, I prefer to make games for phones and then compensate for the other sizes, and since the future is 4" I set my default size to 568, so we want two mathematical expressions with the following criteria,

1. Sets the equation to zero for either 568 or 1024, and 1 for 480
2. Sets the expression to zero for either 568 or 480, and 1 for 1024.

The expressions I use are as follows,

1. ((568 - ssh)*(1024 - ssh))/47872

if ssh = 568, eq1. = 0
if ssh = 1024, eq1. = 0
if ssh = 480, eq1. = 1

2. ((568 - ssh)*(480 - ssh))/248064

if ssh = 568, eq2. = 0
if ssh = 480, eq2. = 0
if ssh = 1024, eq2. = 1

Now we have our 2 expressions, we use these in the following single behaviour to replace the above rules,

change positiony to 70 + (-20)*(((568 - ssh)*(1024 - ssh))/47872) + (30)*(((568 - ssh)*(480 - ssh))/248064)

The reason I have used -20 is to set the expression to 50 when the screen size is 480, and the reason I've used 30 is to set the expression to 100 when the screen size is 1024, and lastly the reason why I've used 70 alone is because this is the default screen size.

Hope this helps someone!

Comments

Sign In or Register to comment.