@Image All
Hello,
Sergey has the right idea with the easy way. You find the position a few frames ahead - move the enemy and move him back then point the turret at that position and shoot.
If you want a more mathy way without too much overhead:
I think the base angle to start with is the slope between the enemy speed and the projectile speed. This will give you an angle that will always be true when the enemy moves at 90 degrees to the turret. Add the slope to the angle between the turret and the enemy and that is the predicted angle.
So, if the enemy moves at a speed of 5 and the projectile moves at a speed of 20 then the angle to lead the enemy when the enemy is moving at 90 degrees to the turret is
5/20 = atan(.25) = 14.04 degrees
This will be true no matter what distance the enemy is at.
What if the enemy isn't going straight sideways but stepping backward or stepping forward at some other angle to the turret?
If the enemy is stepping backwards or forwards, the distance is changing and the angle is changing so the slope won't be the same.
Subtract the angle between the predicted enemy position and the enemy actual position from the lead angle.
Here's the basics:
sync on
sync rate 60
autocam off
hide mouse
_main:
gosub _init
position camera 500,50,100
do
gosub _move_target
gosub _shoot
sync
loop
end
_init:
rem make a matrix for reference
make matrix 1,1000,1000,25,25
rem make something to hit
make object cube 1,50
color object 1,0
rem make a gun
make object cylinder 2,6
scale object 2,100,300,100
xrotate object 2,90
lock object on 2
position object 2,0,-5,14
rem make a projectile
make object sphere 3,5
set object rotation zyx 2
rem target direction
xdir=5
x#=500
z#=800
y#=25
rem projectyle speed
speed#=20
return
_move_target:
inc x#,xdir
if x# > 900 or x# < 100 then xdir=0-xdir
position object 1,x#,y#,z#
return
_shoot:
rem get the slope between the proectile speed and the enemy speed
rem this can be pre calculated but will do it here for the example
leadang#=atanfull(xdir,speed#)
rem find angle between camera and target
cang#=wrapvalue(atanfull(x#-camera position x(),z#-camera position z())+leadang#)
rem rotate camera
yrotate camera cang#
rem shoot
if shot=0
color object 1,0
position object 3, camera position x(),camera position y(),camera position z()
set object to camera orientation 3
shot=1
endif
if shot and object position z(3) < 1000
move object 3,speed#
bang = object collision(3,1)
else
shot=0
endif
text 0,0,str$(cang#)
if bang
color object 1,rgb(255,255,0)
endif
return
function dist(obj)
dx#=object position x(obj)-camera position x()
dz#=object position z(obj)-camera position z()
dist#=sqrt((dx#*dx#)+(dz#*dz#))
endfunction dist#
Enjoy your day.