PDA

View Full Version : Moving a cursor around a sphere



Brainer
10-11-2007, 04:50 AM
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:

http://img250.imageshack.us/img250/7858/39585211yf4.jpg

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:

Thanks in advance! :)

Andreaz
10-11-2007, 08:47 AM
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


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)


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

Brainer
10-11-2007, 01:24 PM
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...

arthurprs
10-11-2007, 02:50 PM
Based on Andreaz code

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;
Works nice here, a ball moving in circles around something.
I have adapted from my engine code, hope you understand.

Brainer
10-11-2007, 08:25 PM
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?

Robert Kosek
10-11-2007, 08:53 PM
For the Canvas be sure to round your values, but it'll work there too. :)

Brainer
10-11-2007, 10:06 PM
Thanks! :D It works! :D

arthurprs
10-11-2007, 10:18 PM
Thanks! :D It works! :D

:wink: