Results 1 to 8 of 8

Thread: Moving a cursor around a sphere

  1. #1

    Moving a cursor around a sphere

    System: Windows XP SP 2 (Pentium D 805, 2 GB RAM, GeForce 7600 GS)
    Compiler/IDE: Delphi 2007 for Win32
    Libraries/API: None
    -------------

    Hello there!

    Let's assume I've got something like this:



    In the picture you can see a plane, a sphere and a reticle placed on the sphere. I'd like to know how to move the reticle around the sphere? Also, when I move the reticle, the plane should direct at the reticle. Additionaly, the reticle should have something like a max and min angle (like 90 degress up and 90 degress down).

    Do you know what I mean and could give me any useful hints/code snippets, please?

    Thanks in advance!

  2. #2

    Moving a cursor around a sphere

    It depends, where do you get the angle from, you can solve that with trigometry or vectors depending on the application.

    If trig then it's something like
    Code:
      Const DEG_TO_RAD = PI / 180;
    
      Angle:= * Some angle * 
    
      Reticle.X:= Plane.X + CIRCLE_RADIUS * Sin(Angle * DEG_TO_RAD);
      Reticle.Y:= Plane.Y + CIRCLE_RADIUS * Sin(Angle * DEG_TO_RAD);
    
      // Depends on api etc
      Plane.DrawRotated(X,Y, Angle);
    And the vector approach (using mouse for the angle)
    Code:
      var Direction: TVector3f;
    
      Direction:= Vector3f(Mouse.X - Plane.X, Mouse.Y - Plane.Y);
      Direction:= Normalise(Direction);
      
      Reticle.X:= Plane.X + CIRCLE_RADIUS * Direction.X;
      Reticle.Y:= Plane.Y  + CIRCLE_RADIUS * Direction.Y;
    
      Angle:= Arctan2(Direction.X, Direction.Y);
    
      Plane.DrawRotated(X,Y, Angle);
    Not tested through, but something like that

    This snaps the plane to the direction, if you want smooth movements you have to interpolate the planes direction instread
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  3. #3

    Moving a cursor around a sphere

    Uhm, I don't really understand what you wrote. :?
    I'd try it out, but I'm not sure I can do it with PaintBox component. Can someone show me a demo or explain the problem better to me?

    I'd really appreciate it...

  4. #4

    Moving a cursor around a sphere

    Based on Andreaz code
    [pascal]
    const
    DEG_TO_RAD = PI / 180;
    Circle_Radius = 50;

    var
    angle : Integer;
    // another stuffs

    procedure DrawScene;
    begin
    engine.clear;

    Inc(angle);
    if angle >= 360 then angle := 0;

    CircleCX := plane.X + plane.W / 2;
    CircleCY := plane.Y + plane.H / 2;

    reticleCX := CircleCX + Circle_Radius * Cos(angle * DEG_TO_RAD);
    reticleCY := CircleCY + Circle_Radius * Sin(Angle * DEG_TO_RAD);

    plane.draw(plane.x,plane.y);
    reticle.draw(reticleCX - reticle.W / 2, reticleCY - reticle.H / 2);

    engine.flip;
    end;[/pascal]
    Works nice here, a ball moving in circles around something.
    I have adapted from my engine code, hope you understand.
    From brazil (:

    Pascal pownz!

  5. #5

    Moving a cursor around a sphere

    Thanks. I've got another question. What should I do to test it out? I mean can I test it out with TCanvas object or have I got to use OpenGL?

  6. #6

    Moving a cursor around a sphere

    For the Canvas be sure to round your values, but it'll work there too.

  7. #7

    Moving a cursor around a sphere

    Thanks! It works!

  8. #8

    Moving a cursor around a sphere

    Quote Originally Posted by Brainer
    Thanks! It works!
    From brazil (:

    Pascal pownz!

Bookmarks

Posting Permissions

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