PDA

View Full Version : Math problem in menu



iCore
27th July 2013, 20:51
Heya!

Now it's the third day that I can't solve a menu problem, so it's time to ask for some pro mathematician and menu editors' help.
So now I'm working on a Call of Duty 4 mod (called APB Mod (http://www.moddb.com/mods/apb-mod)), and I'm making a 2D map in menu (if anyone is playing APB, it will be something like the respawn-slection menu); and well, I want players to be able to zoom on it, and zoom out. The menu is working fine, except that the zooming is not done linearly. Here is a video about it: Xfire video (http://www.xfire.com/video/608d8d). The zoom is controlled by a variable, between 0 and 1. The X coordinate is changing based on the percentage of the zoom variable this way:

100 percent: -99.2
90 percent: -79.7090
80 percent: -54.9333
70 percent: -26
60 percent: 5.94
50 percent: 40.5
40 percent: 77.2
30 percent: 115.57
20 percent: 155.37
10 percent: 196.37
0 percent: 238.4

As you can see, the smaller the zoom variable is, the bigger steps will be in the X coordinate.

So I'd need some help in figuring out, why the hell is it happening.
Here are the definitions and the map item itself:


#define ZOOM dvarFloat("zoom")

#define placeX(o, q) dvarInt("width") / 2 - (dvarInt("respawn_x") - o) / (20 - ZOOM * 10) - (q)
#define placeY(o, q) dvarInt("height") / 2 + (dvarInt("respawn_y") - o) / (20 - ZOOM * 10) - (q)

itemDef
{
style WINDOW_STYLE_SHADER
rect 0 0 0 0 HORIZONTAL_ALIGN_NOSCALE VERTICAL_ALIGN_NOSCALE
exp rect X(placeX(0, 512 + ZOOM * 512))
exp rect Y(placeY(0, 256 + ZOOM * 256))
exp rect W(1024 + ZOOM * 1024)
exp rect H(512 + ZOOM * 512)
exp material("lookout_" + dvarString("mapname"))
visible 1
decoration
}

width and height are the resolution of the game (ie 1024 and 768), respawn_x and respawn_y are the origin. Basically it should be broken even if you write 1000 for all the 4 values, since I think the problem is somewhere in the usage of ZOOM. The starting, and the end points are perfect.
I have tried millions of combinations, but I couldn't get it work. Any ideas are welcome.

Thank you,
iCore

EDIT (07-27 23:13): Viking also checked, and according to him, calculations are fine; so maybe there is an other problem, but still no idea what can be :/

IzNoGoD
27th July 2013, 23:49
i just put all those values in matlab, looks like this:
358

Looks like a linear function with some non-linear parts at the end, so i tried a second-order fit, which resulted in this:
359

Looks alright, doesnt it? Well, when you look at the residue (how much the fit differs from the original) you can see this:
360
Obviously, there is still a trend in the difference. This means a second-order fit is not enough to describe this function. Looking at a 3rd order didnt change much, but 4th order looks like this:
361

As you might not want to do 4th order calculations (2nd order was not off by that much), you might wanna stick with 2nd order. Values:
value = 0.0116*percent^2 - 4.5959 * percent + 240.91

Good luck :)

iCore
28th July 2013, 17:09
Hey, thank you! The diagrams look very meaningful, however I think my math knowledge is under the level, which would be required to understand the 'order calculations' and that 'value' at the end. Can you please make it a little clearer, what is wrong in my definitions? :P

Thanks your help and your time and sorry for being dumb for it :)

IzNoGoD
28th July 2013, 18:14
Order:
zero order: y = A
first order: y = B*x + A
second: y = C*x^2 + B*x + A
third: y = D*x^3 + C*x^2 + B*x + A
etc.

value is the x-coordinate, so:
x-coordinate = 0.0116*zoomlevel^2 - 4.5959 * zoomlevel + 240.91

megazor
30th July 2013, 04:16
Why do you divide by 'zoom'?


(dvarInt("respawn_x") - o) / (20 - ZOOM * 10)

y = 1/x is not a linear function. That's why your zooming is not linear.

iCore
30th July 2013, 18:37
Thanks for you both!
IzNoGoD that's probably still a little high for me, but thanks megazor, that's a good idea! Any ideas, how can I make it linear? :P

IzNoGoD
30th July 2013, 19:54
Try height = something * (1 - zoom) + something_else.
Same for width.

iCore
30th July 2013, 21:51
I can hardly believe it, but it is working!
Thanks for both of you
*.*
If anyone would have a problem like this, I have solved it this way now: (dvarInt("respawn_x") - o) * (0.05 + (ZOOM * 0.05)).

megazor
31st July 2013, 02:40
Now that's a True Sexy Linear Form! Nice one, iCore :)