I didn't plan on doing any dev stuff tonight but saw this and thought it would be interesting.
I think I ended up finding the same tutorial you had maybe.... C code using OpenGL? Anyway, I converted that but it also does not work... I suspect it probably works as well as the original source though which was probably also flawed. lol
Still here is the code for what it's worth.
#constant TRUE 1
#constant FALSE 0
#constant KEY_ESC 27
#constant SPEED 1.5
x as integer
y as integer
oldX as integer
oldY as integer
rotate as integer
theta as float
phi as float
xdist as float
ydist as float
objX as float
objY as float
objZ as float
camX as float
camY as float
camZ as float
radius as float
theta = 0
phi = 0
radius = 500
// create some spheres and position them randomly
for i = 1 to 1000
CreateObjectSphere(i,2,10,10)
SetObjectPosition(i, random(0,1000), random(0,1000), random(0,1000))
SetObjectColor(i,0,255,0,255)
next i
objBox = CreateObjectBox(20,20,20)
SetObjectPosition(objBox, 500, 500, 500)
SetCameraPosition(1,500, radius, 500)
SetCameraLookAt(1, 500, 0, 500, 0)
repeat
if GetPointerPressed()
oldX = GetPointerX()
oldY = GetPointerY()
elseif GetPointerState()
x = GetPointerX()
y = GetPointerY()
xdist = x-oldX
ydist = y-oldY
inc theta, xdist * SPEED
inc phi, ydist * SPEED
oldX = GetPointerX()
oldY = GetPointerY()
endif
if GetRawMouseWheelDelta()>0 then inc radius, 100
if GetRawMouseWheelDelta()<0 then dec radius, 100
ObjX=GetObjectX(objBox)
ObjY=GetObjectY(objBox)
ObjZ=GetObjectZ(objBox)
camX = objX + radius * cos(phi) * sin(theta)
camY = objY + radius * sin(phi) * cos(theta)
camZ = objZ + radius * cos(theta)
SetCameraPosition(1, camX, camY, camZ)
SetCameraLookAt(1, objX, objY, objZ, 0)
sync()
until GetRawKeyPressed(KEY_ESC)
I think the main thing different over yours is I tweaked it to set the start point (oldX, oldY) when the mouse button is first clicked. So just clicking around on the screen in isolation does not cause rotation and otherwise mess things up.
The issue is clearly in the formulas to convert 2D screen positions and movement into 3D rotations and seems mainly to be an issue with the calculation of the Z coordinate (I *think*) because I notice sometimes it basically just zooms in and out.
Might be worth dropping back to implementing it as a auto flyby orbit. Get that working for orbiting around X & Z and for orbiting around Y & Z.
Then maybe focus on trying to manage it with mouse movement controlling it.
It might be better to use Quaternions. I think AGK2 supports it.
Also I thought about approaching it from rotating the object (cube) itself then having that cast out a ray in direction facing a set distance (radius) or more likely just moving the cube (or a dummy object) forward a set distance and positioning the camera there. That might be simpler.
I think that is actually how I would probably approach it but again the issue still comes in from converting 2D mouse position and movement into 3D rotation of the cube. It is just not something I've ever had a need to do before so never experimented or researched it even until just now. Otherwise I would gladly share a working solution.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)