If the missile tracking works couldn't you just copy that?
Anyway - this is simple turret tracking without working out the angles - the bases rotate around y axis only where as the turrets fully face the flying object
// Project: Turret
// Created: 2018-08-31
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Turrets" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
//SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetVSync(1)
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// Create a type to hold a turret
type Turret
base as integer
gun as integer
endtype
// create 10 turrets
global Turrets as Turret[50]
// Create all the turrets
for i=0 to Turrets.length
// Create the gun and the base
Turrets[i].base = CreateObjectBox(2,1,2)
Turrets[i].gun = createObjectBox(0.5,0.5,4)
SetObjectColor(Turrets[i].gun,170,170,170,255)
SetObjectPosition(Turrets[i].gun,0,0,2)
FixObjectPivot(Turrets[i].gun)
// Set the position of them both
SetObjectPosition(Turrets[i].base,random2(0,40)-20,0,random2(0,40)-20)
SetObjectPosition(Turrets[i].gun,getobjectx(Turrets[i].base),0.5,GetObjectz(Turrets[i].base))
next i
// create a cone to aim at
global cone
cone = CreateObjectCone(2,1,10)
SetObjectColor(cone,255,0,0,255)
// create a cylinder to mark the position on the ground below the cone
cylinder = CreateObjectCylinder(0.2,1,10)
SetObjectColor(cylinder,10,10,10,120)
SetObjectTransparency(cylinder,1)
//Set the camera to look into the scene
global CamAngle# = 45
// Create the ground
ground = CreateObjectBox(45,2,45)
SetObjectColor(ground,0,160,0,255)
SetObjectPosition(ground,0,-1.5,0)
// Main Loop
do
//Move the cone
SetObjectPosition(cone,20*sin(timer()*70),15,20*cos(timer()*92))
// move the cylinder on the ground to show where the cone is above it
SetObjectPosition(cylinder,GetObjectX(cone),0,Getobjectz(cone))
// update the turrets to point at the cone
UpdateTurrets()
// Update the camera
UpdateCamera()
// update the screen
Print( "Use 'A' and 'D' to rotate the camera around the scene")
Print( "FPS: " +str(ScreenFPS()) )
Sync()
loop
function UpdateTurrets()
// loop through the turrets and point them towards the cone
for i=0 to Turrets.length
// Point the turret at the flying cone
SetObjectLookAt(Turrets[i].gun,GetObjectX(Cone),GetObjectY(Cone),GetObjectZ(Cone),0)
// Point the turret base at the flying cone
SetObjectLookAt(Turrets[i].base,GetObjectX(Cone),GetObjectY(Turrets[i].base),GetObjectZ(Cone),0)
next i
endfunction
function UpdateCamera()
// Move the camera with A and D
if GetRawKeyState(65) then dec CamAngle#,100*GetFrameTime()
if GetRawKeyState(68) then inc CamAngle#,100*GetFrameTime()
// Update the camera to rotate around the scene
SetCameraPosition(1,35*sin(CamAngle#),28,35*cos(CamAngle#))
SetCameraLookAt(1,0,0,0,0)
endfunction
Your angle function gives a positive angle when x<0 and y>0??? Thats a positive angle for an anticlockwise rotation? AppGameKit is the opposite coordinate system. You pass the x coords into the y value of Atan which is why its giving you the opposite angle direction.
I'm really not sure what your trying to do now so good luck with it.