Graphics Adventure type movement

BlitzMax Forums/BlitzMax Programming/Graphics Adventure type movement

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

nah, forget all that noise.


Try to move the character a fraction of the distance to his target every frame. This way, as he gets closer, he moves slower.

When he's really far away, though, he'll move quite quickly, so scale the distance moved down to a constant number if it's too fast.

E.g.:

Graphics 800,600,0
Global px#,py#,tx#,ty#	'character is at (px,py), wants to move to (tx,ty)

'start character in middle of screen
px=400
py=300
tx=400
ty=300

Const maxspeed#=2	'the fastest you want the character to move

While 1
	
	If MouseHit(1)	'left click changes where we want to move to
		tx=MouseX()
		ty=MouseY()
	EndIf
	
	'find the difference between where we are and where want to be
	dx#=tx-px
	dy#=ty-py
	
	'move at most a tenth of the distance during this frame
	dx:*.1
	dy:*.1
	
	If Sqr(dx*dx+dy*dy)>maxspeed	'if this movement is bigger than the maximum we want, scale it down
		scale#=maxspeed/Sqr(dx*dx+dy*dy)
		dx:*scale
		dy:*scale
	EndIf
	
	'move the character
	px:+dx
	py:+dy
	
	
	DrawOval px-2,py-2,4,4
	
	DrawRect tx-2,ty-2,4,4

	Flip
	Cls
	
	If KeyHit(KEY_ESCAPE) Or AppTerminate()
		End
	EndIf
Wend


I've tried to make it as maths-free as possible! Just copy this out and use it if you want.