SetHandle and SetOrigin

BlitzMax Forums/BlitzMax Programming/SetHandle and SetOrigin

Wiebo(Posted 2005) [#1]
Hi all,
I'm having trouble grasping these two commands. Can someone explain to me how they work?
I've been trying to create a rotating triangle, but after rotation the lines are not drawn correctly. Note: I want to be able to draw the triangle where ever i want.
Strict

Graphics 640,480,0

Local a:tObject = New tObject
ListAddLast a.sourcelines, tLine.Create( 200,200,100,100 )
ListAddLast a.sourcelines, tLine.Create( 100,100,300,100 )
ListAddLast a.sourcelines, tLine.Create( 300,100,200,200 )

a.Draw()
Flip
WaitKey()
End

Type tObject
	Field sourcelines:tList
	Field angle:Float
	Field scale:Float	
	
	Method New()
		sourcelines = CreateList()
	End Method
	
	Method Draw()
		SetOrigin 100,100
		SetScale 1,1
		SetRotation 45
'		SetHandle 100,100
		
		For Local l:tLine = EachIn sourcelines
			DrawLine l.x1,l.y1, l.x2,l.y2
		Next
	End Method
	
End Type


Type tLine
	Field x1:Float, y1:Float
	Field x2:Float, y2:Float
	
	Function Create:tLine( x1:Float,y1:Float, x2:Float,y2:Float )
		Local l:tLine = New tLine
		l.x1 = x1
		l.x2 = x2
		l.y1 = y1
		l.y2 = y2
		Return l
	End Function
End Type



Beaker(Posted 2005) [#2]
I had a play after we spoke on IRC today. SetOrigin moves the screen origin (from 0,0) to wherever you want. SetHandle moves the drawing handle, which is in screen coordinate space I think.

I don't think these commands are the solution to your problem, or indeed creating the new problem. I think the problem lies with SetRotation. You should drop the SetRotation command, and roll your own rotations.

This gives a clearer idea of why its a problem to use SetRotation:
Strict
Local ang# = 0
Graphics 640,480,0
While Not KeyDown(KEY_ESCAPE)
	ang:+1
	SetRotation ang
	DrawLine 0,0,300,0
	Flip
	Cls
Wend
End



Wiebo(Posted 2005) [#3]
Thanks, I get ya, I'll rotate the points myself. But I still don't get or am sure (and neither are you) what SetHandle does. The docs are in need of more examples.


teamonkey(Posted 2005) [#4]
I think I've worked out SetHandle now. In the Thrust-like thing I'm working on, I need to draw an image or rectangle (a thick line) representing a tractor beam from my ship to the target object.

I had this:
SetRotation(ATan2(pos.x-p.pos.x, p.pos.y-pos.y)+90#)
DrawRect(pos.x, pos.y, distance(p), 8.0#)

(pos.x,pos.y) are the co-ordinates of the ship, (p.pos.x,p.pos.y) are the co-ordinates of the target, distance(p) is the distance from the ship to the target. So SetRotation rotates the drawing axis in the direction of the target, so I just draw a rectangle 8 pixels thick with the correct length.

The problem is that the rectangle is rotated around the top-left corner, and not around a point halfway down the left-hand side. DrawRect (and the other drawing commands, I imagine) works as though it draws a rectangle width x height in size starting at the origin, then transforms it by whatever SetRotation/SetScale/SetTransform says, and finally moves it to (x,y). So, after much experimentation, I did this:
SetRotation(ATan2(pos.x-p.pos.x, p.pos.y-pos.y)+90#)
SetHandle(0.0#,4.0#)
DrawRect(pos.x, pos.y, distance(p), 8.0#)

So although the draw and rotate commands are the same, the rectangle is rotated around a different point.

Like SetRotation, SetHandle applies to everything else you draw so you have to reset it to 0,0 before you draw anything else.


Wiebo(Posted 2005) [#5]
teamonkey, thats great information. I'll try this tonight!!


Wiebo(Posted 2005) [#6]
Well, I get the idea now. SetHandle will set the rotation and scale point, applied at 'local' space. Spintext.bmx in the examples folder does a good job of showing what it's for... Thanks everyone.