bearings

BlitzMax Forums/BlitzMax Beginners Area/bearings

how would I find the bearing of object B, from object A
without using a protractor. :)
I mean code in blitz. Do I have to find the sin cosin of the angle of that line between A and B?


A


B

yes.

what you need to learn about is vectors.

I'll post a little demo program that might help you out. it is fairly straightforward.

first, when you say 'bearing' i take that to mean
" what angle would Object A be rotated to if Object A were Pointing at Object B"

give me 5 mins... maybe 10 :)

yep that is what I mean.

so find the sin, cosin of that imaginary line between a and b then take it away from 360?

this might be what you need:

blitzmax angles are -180 to 180 (360 degrees)


EDIT:
Strict

Graphics 800,600
Local midx:Int = GraphicsWidth()/2
Local midy:Int = GraphicsHeight()/2
Local mx:Int
Local my:Int

Repeat
		
	mx = MouseX()
	my = MouseY()
	
	DrawLine (midx,midy,mx,my)
	
	Local myangle = ANGLE(mx,my,midx,midy)
	
	DrawText "B",mx+16,my
	DrawText "angle="+myangle,mx+20,my+20
	
	
	
	SetRotation myangle
		DrawText "A",midx,midy
	SetRotation 0	
	Flip ; Cls 

Until KeyDown(KEY_ESCAPE)

 Function ANGLE:Float(x1,y1,x2,y2)
		Local angle = ATan2(x2-x1,y1-y2)+180 ' Thanks Skidracer
		' Inverse Tangent
	Return angle
End Function




ah yes, excellent, except that. it should calculate the A as zero all the time. because I want to get the bearing of B from A.

So when i move the pointer exact to the left of A it should read 270 degrees. Exactly above = zero, to the right of A = 90.

your code there is seeming to do it in half circles, as in from the right of A all the way to the left side of A = 180. But it should include the 90 degrees from the top of A until it reached the right of A.

so 90 + 180 = 270.

for that result modify line 31:
		Local angle = ATan2(x2-x1,y1-y2)+180


The problem is computer graphics have y axis going down the screen where as usual cartesian graphs have the positive y axis pointing up.

Thanks skid. I updated it in the codebox above.

thanks guys :)

darn gurus... :P

God I love it when folks post complete code examples!!! Thanks Skid and bradford6!!