Angle Question...

BlitzPlus Forums/BlitzPlus Programming/Angle Question...

Hiya all, I have a little problem :)

In the code below, I have a small dot that follows the mouse, and when I hit the mouse button, I want the dot to travel from the point of the mouse, to another point on the screen!

But, it's not working! Well, it is (ish), so... the question is, how do I get it working properly? :/

Graphics 640,480

;Scores position on the screen
Global scoreX = 400,scoreY = 100


Global dot = CreateImage(2,2)
SetBuffer ImageBuffer(dot)
Rect 0,0,1,1

SetBuffer BackBuffer()
Repeat
Cls
 
mx = MouseX()
my = MouseY()

If MouseHit(1)
	CreateMovingScore(mx,my,20)
End If

UpdateMovingScore()
DrawBlock dot,mx,my
Flip
Until KeyDown(1)

Type MoveScore
	Field x#,y#,speed#
	Field startX#,startY#
	Field finishX#,finishY#
	Field score
End Type

Function CreateMovingScore(x,y,score)
	createScore.MoveScore = New MoveScore
	createScore\startX = x
	createScore\startY = y
	createScore\speed = 1
	createScore\finishX = scoreX
	createScore\finishY = scoreY
	createScore\score = score
End Function

Function UpdateMovingScore()
	For updateMovement.MoveScore = Each MoveScore
		angle=ATan2((updateMovement\finishY + updateMovement\startY),(updateMovement\finishX + updateMovement\startX))
		updateMovement\x=(updateMovement\x + Cos( angle )*updateMovement\speed) 
		updateMovement\y=(updateMovement\y + Sin( angle )*updateMovement\speed) 
		DrawBlock dot,updateMovement\x,updateMovement\y
		Text 10,10,updateMovement\x+":"+updateMovement\y
		If updateMovement\x > scoreX And updateMovement\y > startY Then Delete updateMovement
	Next
End Function



Thanks in advance

Dabz

P.S. If this eventually works, it should go straight in the archives, because it's a right bugger of a problem, especially for someone who got a D in maths! :D

*EDIT*
Just noticed, the dot travels through the two points exactly half way... mmmm

Dabz starts scratching his head!!!!

I found this anyway:-

http://www.blitzbasic.com/codearcs/codearcs.php?code=940

Dabz

Not perfect, but it works:-


Graphics 800,600
SetBuffer BackBuffer()

Type moveObject
	Field x,y
	Field px,py
End Type


While Not KeyHit(1)
	Cls
	
	If MouseHit(1) Then
			setpath(MouseX(),MouseY(),400,100)
	End If

	move()
	Color 200,200,200
	Rect MouseX()-1,MouseY()-1,2,2
	Rect 400,100,2,2
	Flip
Wend
End


Function move()
	For loopObjects.moveObject = Each moveObject
	ang#=0
		ang=ATan2(loopObjects\py-loopObjects\y,loopObjects\px-loopObjects\x)
		loopObjects\x=loopObjects\x+Cos(ang)*3
		loopObjects\y=loopObjects\y+Sin(ang)*3
		Color 100,200,100
		Rect loopObjects\x-3,loopObjects\y-3,6,6 
		If Abs(loopObjects\x-loopObjects\px)<2 And Abs(loopObjects\y-loopObjects\py)<2 Then
			Delete loopObjects
		End If
	Next 
End Function


Function setPath(x,y,futureX,futureY)
	createPath.moveObject = New moveObject
	createPath\x = x
	createPath\y = y
	createPath\px = futureX
	createPath\py = futureY
End Function



I'll sleep better tonight knowing that's sorted! :)

Dabz

Function Angle(basex, basey, targetx, targety)
	If targety>basey-1 Then
		angle=ACos((targetx-basex)/Sqr((targetx-basex)*(targetx-basex)+(targety-basey)*(targety-basey)))
	Else
		If targetx>basex-1 Then
			angle=270+90-ACos((targetx-basex)/Sqr((targetx-basex)*(targetx-basex)+(targety-basey)*(targety-basey)))
		Else
			angle=360-ACos((targetx-basex)/Sqr((targetx-basex)*(targetx-basex)+(targety-basey)*(targety-basey)))
		EndIf
	EndIf

	If targetx=basex And targety=basey Then Return 0 Else Return angle
End Function

Function Distance#(x1#, y1#, x2#, y2#)
	Return Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
End Function


Thanks Andres, nice functions them! :)

Dabz