CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Mathematical concept behind 3D-Trackball

    the following code comes from a file named trackball.c (http://scv.bu.edu/documentation/pres...gl/trackball.c)


    What I'd like to know is the mathematical concept behind this function. Why are this two constants being used here and what does it mean "inside sphere" and "on hyperbola". I was not able to find any website explaining why this works. I can use this function and the trackball in my OpenGL app works but I don't know why and it drives me crazy ;]

    Code:
    /*
     * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
     * if we are away from the center of the sphere.
     */
    inline float
    tb_project_to_sphere(float r, float x, float y)
    {
        float d, t, z;
    
        d = sqrt(x*x + y*y);
    	
        if (d < r * 0.70710678118654752440) 
        { 
    	/* Inside sphere */
            z = sqrt(r*r - d*d);
        } 
        else
        {
    	/* On hyperbola */
            t = r / 1.41421356237309504880;
            z = t*t / d;
        }
        return z;
    }
    (this function is used to find the z-coordinate on an imagined sphere from mouse coordinates to further calculate a quaternion rotation for the camera/scene)
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Mathematical concept behind 3D-Trackball

    0.707... is 0.5 * sqrt(2.) or sqrt(0.5)
    1.42... is sqrt(2.)
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mathematical concept behind 3D-Trackball

    @D_Drmmr: that's clear to me...

    but for example why do we take a hyperbola when d is greater then r * 0.707?

    I don't remember such calculations from school... I wish I could do the same on a sheet of paper and get the same result but I don't know how, there must be some math-website that illustrates this projection of points on a sphere and how it works.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Join Date
    Apr 2004
    Posts
    102

    Re: Mathematical concept behind 3D-Trackball

    After perusing that .c file, the comments especially, I get the impression that they are using a sphere-proper for generating the z-coordinate when the (x,y) is close to the center of the sphere, but use an arbitary function (hyperbola-based in this case), for a better "feel" when the (x,y) is farther from center.

    This appears to be a user interface decision, rather than a mathematically proper one.

    If you can track down the article mentioned in the code comments, it would give a more detailed explanation, I'm betting.
    Code:
     * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
     *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured