Really simple thing....

Blitz3D Forums/Blitz3D Beginners Area/Really simple thing....

Apollonius(Posted 2004) [#1]
I'm trying to make a code to make a rectangle go to where ever I right click, but not instantly I want it slowly at a certain speed to go there, anyone got an example I doubt my code is that good...

Graphics 800,600,32

SetBuffer BackBuffer()

Global Rx% = 10
Global Ry% = 10
Global goX%, goY%

Repeat
Cls
Mx% = MouseX()
My% = MouseY()

Rect Rx%, Ry%, 100, 100, 0

If MouseHit(2) Then
	goX% = Mx%
	goY% = My%
EndIf
If Not Rx%=goX% And Ry%=goY% Then
; // Do this to get to the cordination?
EndIf
Flip
Until KeyHit(1)
End




electronin(Posted 2004) [#2]
Try this:
Graphics 800,600,32

SetBuffer BackBuffer()

Global Rx% = 10
Global Ry% = 10
Global goX%, goY%

Repeat
Cls
Mx% = MouseX()
My% = MouseY()

Rect Rx%, Ry%, 100, 100, 0

If MouseHit(2) Then
	goX% = Mx%
	goY% = My%
EndIf
If Rx < goX Then Rx=Rx+1
If Rx > goX Then Rx=Rx-1
If Ry < goY Then Ry=Ry+1
If Ry > goY Then Ry=Ry-1
Flip
Until KeyHit(1)
End

Hope that helps!


Apollonius(Posted 2004) [#3]
Thanks! So my approch wasnt too bad at all! :)


Jeppe Nielsen(Posted 2004) [#4]
This lets it go at any angle, instead of just 0,45,90 etc:
Graphics 800,600,32,2

SetBuffer BackBuffer()

Global Rx# = 10
Global Ry# = 10
Global goX#, goY#
;alter this to change movement speed
Global speed#=2
Repeat
Cls
Mx% = MouseX()
My% = MouseY()
Rect Rx-50, Ry-50, 100, 100, 0
If MouseHit(2) Then
	goX = Mx
	goY = My
EndIf
dx#=(goX-rx)
dy#=(goY-ry)
l#=Sqr(dx*dx+dy*dy)
If l>1

	dx=dx/l
	dy=dy/l
	rx=rx+dx * speed#
	ry=ry+dy * speed#
	
Else

	Text 400,300,"Target reached!",1,1

EndIf
Oval goX-3,goY-3,6,6,0
Flip
Until KeyHit(1)
End