Math help anyone?

This is a discussion on Math help anyone? within the C++ Programming forums, part of the General Programming Boards category; I am makeing a library to deal with all my opengl general stuff, and i need help with my mouse ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Math help anyone?

    I am makeing a library to deal with all my opengl general stuff, and i need help with my mouse interface.

    See windows assumes that 0,0 is the upper left corner, and OpenGL assumes its the lower left. so i need to flip the y axis value.

    it is stored here

    loc.cur.y

    ive been trying, and have this, but it dosent work

    int tempy;
    if(loc.cur.y > (glh.height/2)) tempy = (glh.height/2) - (loc.cur.y - (glh.height/2));
    if(loc.cur.x < (glh.height/2)) tempy = (glh.height/2) + (loc.cur.y - (glh.height/2));

    I have attached a simple program to demonstrate the problem.
    just click on a sector to turn the floating box that color.

    Given a 600 y resolution, if the mouse is at 500, it needs to be at 100.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Re: Math help anyone?

    Originally posted by Eber Kain
    I am makeing a library to deal with all my opengl general stuff, and i need help with my mouse interface.

    See windows assumes that 0,0 is the upper left corner, and OpenGL assumes its the lower left. so i need to flip the y axis value.

    it is stored here

    loc.cur.y

    ive been trying, and have this, but it dosent work

    int tempy;
    if(loc.cur.y > (glh.height/2)) tempy = (glh.height/2) - (loc.cur.y - (glh.height/2));
    if(loc.cur.x < (glh.height/2)) tempy = (glh.height/2) + (loc.cur.y - (glh.height/2));

    I have attached a simple program to demonstrate the problem.
    just click on a sector to turn the floating box that color.

    Given a 600 y resolution, if the mouse is at 500, it needs to be at 100.
    Well, if you want to go from (maxheight-1)..0 instead of 0..maxheight-1, just do:

    (i'm assuming tempy is where you want the result, glh.height is the height, and loc.cur.y is the y-value you want to convert.

    tempy = glh.height - loc.cur.y - 1;

    To test this:

    if loc.cur.y = 0; tempy = glh.height - 1 (good!)
    if loc.cur.y = glh.height - 1; tempy = 0 (good!)

    Because this is a linear system of one indep. variable, if two points map correctly, all points map correctly, so this will work unless I misunderstand the problem.
    Last edited by The V.; 09-26-2001 at 09:17 PM.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    You are talking about wrapping i think, i need to invert the mouses current y position.

    if its 500 make it 100, 400 = 200, 110 = 490.

    run the program so you can see what im talking about please

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Eber Kain
    You are talking about wrapping i think, i need to invert the mouses current y position.

    if its 500 make it 100, 400 = 200, 110 = 490.

    run the program so you can see what im talking about please
    Try that function -- it DOES invert it.

    In fact, plug in your examples, and you'll see they differ from the function I gave by only 1 (which is your mistake, I believe, because the lines would be numbered 0...599 not 0..600, so 0 maps to 599, 100 maps to 499, etc.)


    my function, evaluated at your examples:

    tempy = glh.height - loc.cur.y - 1;

    loc.curr.y = 500, then tempy = 600 - 500 - 1 = 99;
    loc.curr.y = 400, then tempy = 600 - 400 - 1 = 199;
    loc.curr.y = 110, then tempy = 600 - 110 - 1 = 489;

    No matter which approach I take to your problem, I always get tempy = glh.height - loc.curr.y - 1 as the answer.

    Another approach is to look at how you want tempy to change for a given change in loc.curr.y. We can prove that an inversion mapping like this is a linear equation of the form tempy = m * loc.curr.y + b. I'll leave out this proof, but from this, we can analyze the system.

    If loc.curr.y increases by one, tempy should decrease by one. This means the slope (change in output for unit change of input) is equal in magnitude (one pixel) but opposite in direction, so m = -1. Once we have this, we can substitute any point whose values we know -- for example, if loc.curr.y = 0, we want tempy = glh.height - 1. From this we can compute b, and we'll get the same equation as above.

    I know this method works, because I, too, have needed to invert y-values, for reading certain types of images. And this will invert the y-values -- test it if you don't believe me.
    Last edited by The V.; 09-27-2001 at 12:27 AM.

  5. #5
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    The code I use (and it may help you, it may not) is ->

    x = ((mouseX*2/640 - 1) * (z/2));
    y = ((mouseY*2/480 - 1) * -(z/2));

    mouseX is your windows mouse x co-ordinate
    mouseY is your windows mouse y co-ordinate

    z is your openGL depth value

    x should give you the openGL x co-ordinate
    y should give you the openGL y co-ordinate

    Hope this helps
    My site to register for all my other websites!
    'Clifton Bazaar'

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    well ill be damned, that works perfectly, it still dosent look right, but works great, thanks alot man.

  7. #7
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    :)

    my theory is "whatever works", it took my brother and I about 6 hours to work that out. Glad it helped
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 06:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 07:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21