In my shoot'em up type game I just want to add a homing missle type movement.
I'm sure I managed this in BlitzBasic years ago, but I fear age has addled my brain.
I quickly got to the 'missile points at target and goes straight to it' stage but then wanted to add the smoother movement/rotations rather than sudden changes of direction. I nearly managed it once but it insisted on freaking out on me when it was directly below the target.
I have searched endlessly, finding Unity demos a plenty, a few Java based solutions, mentions of Curveangle(?) in DBP, a very old Blitz code archive demo which I could not get to run. Having spent all yesterday trying to solve this 'simple' thing I ended up on a heap in the sofa reading a Wikipedia article on Slerps, dribbling slightly whilst banging my head against a cushion …
Today I started a fresh with just simple code to get a sprite to follow the mouse pointer
// Project: follow
// Created: 2018-12-04
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "follow" )
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
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
type follower
x as float
y as float
dx as float
dy as float
ang as float
endtype
global chaser as follower
chaser.x=500
chaser.y=500
global speed as float
speed=10
global mx as float
global my as float
global dist as float
global ang as float
global dang as float
chaser.ang=90
CreateSprite(1,0)
SetSpriteScale(1,.5,3)
do
mx=getrawmousex()
my=getrawmousey()
getposition()
chaser.x=chaser.x+chaser.dx
chaser.y=chaser.y+chaser.dy
SetSpritePosition(1,chaser.x,chaser.y)
chaser.ang=ATanFull(chaser.dx,chaser.dy)
SetSpriteAngle(1,chaser.ang)
Sync()
loop
function getposition()
dist=getdistance(chaser.x,chaser.y,mx,my)
chaser.dx=getmovex(chaser.x,chaser.y,mx,my,speed)
chaser.dy=getmovey(chaser.x,chaser.y,mx,my,speed)
endfunction
function getdistance(x1#,y1#,x2#,y2#)
dx#=x2#-x1#
dy#=y2#-y1#
ang=ATanFull(dx#,dy#)
d#=Sqrt(dx#*dx#+dy#*dy#)
endfunction d#
function getmovex(ox#,oy#,dx#,dy#,spd#)
x#=ox#-dx#
y#=oy#-dy#
ang#=ATanFull(x#,y#)
mx#=spd#*sin(ang#)
endfunction -mx#
function getmovey(ox#,oy#,dx#,dy#,spd#)
x#=ox#-dx#
y#=oy#-dy#
ang#=ATanFull(x#,y#)
my#=spd#*cos(ang#)
endfunction my#
, with the intention of modding that to get the movement I was after, then inserting it into my game. Another half a day and I'm still no closer.
Can anyone point me in the right direction? (no pun intended) I have been trying to pick an angle part way to the angle of the target from the 'chaser' and move towards that, but every way I try just does not work. I fear I am defeated by what should be a simple thing ..
Cheers
Graham