Thread: Moving a cursor around a sphere

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? :wink:

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&#58;= * Some angle *

// Depends on api etc
Plane.DrawRotated&#40;X,Y, Angle&#41;;```
And the vector approach (using mouse for the angle)
Code:
```  var Direction&#58; TVector3f;

Direction&#58;= Vector3f&#40;Mouse.X - Plane.X, Mouse.Y - Plane.Y&#41;;
Direction&#58;= Normalise&#40;Direction&#41;;

Reticle.X&#58;= Plane.X + CIRCLE_RADIUS * Direction.X;
Reticle.Y&#58;= Plane.Y  + CIRCLE_RADIUS * Direction.Y;

Angle&#58;= Arctan2&#40;Direction.X, Direction.Y&#41;;

Plane.DrawRotated&#40;X,Y, Angle&#41;;```
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

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. Moving a cursor around a sphere

Based on Andreaz code
[pascal]
const

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;

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.

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. Moving a cursor around a sphere

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

7. Moving a cursor around a sphere

Thanks! It works!

8. Moving a cursor around a sphere

Originally Posted by Brainer
Thanks! It works!
:wink:

Posting Permissions

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