Drawline() and SetRotation() question

BlitzMax Forums/BlitzMax Beginners Area/Drawline() and SetRotation() question

PoliteProgrammer(Posted 2009) [#1]
Hi, apologies if this has been asked before - I did a search but couldn't find much relevant. Try running the following code and you'll spot the problem instantly; drawing lines gets screwed up when using setrotation (I want the rectangle to rotate). The drawing and rotating operations are performed in my drawObjects() function - any suggestions?


SuperStrict

Graphics(640,480,0,60)

Global gx:Float = GraphicsWidth() 
Global gy:Float = GraphicsHeight()
Global dt:Float = 1.0 / 60.0      
Global player:Int = -1 

Type objType
    Field alive:Int
    Field x:Float, y:Float
    Field ang:Float
End Type

Global objArray:objType[]

player = createObj(0.0, 0.0, 0)
createObj(200.0, 200.0, 90)

' Main program loop
While Not KeyDown(KEY_ESCAPE)
    Cls

    objArray[1].ang = objArray[1].ang + 1

    drawObjects()

    Flip
Wend
End


' Creates an object in the game world
Function createObj:Int(x:Float, y:Float, ang:Float)

    ' Set up necessary integers
    Local i:Int
    Local j:Int = -1

    ' Find the first object in objArray that is not alive
    If Len(objArray) > 0
        For i = 0 To Len(objArray) - 1
            If objArray[i].alive = 0
                j = i
                Exit
            EndIf
        Next
    EndIf

    ' If such an object does not exist, extend the array
    If j = -1
        objArray = objArray[..Len(objArray) + 1]
        j = Len(objArray) - 1
        objArray[j] = New objType
    EndIf

    ' Set the necessary properties of the object
    objArray[j].alive = 1
    objArray[j].x     = x
    objArray[j].y     = y
    objArray[j].ang   = ang

    Return j
End Function




' Perform all drawing operations for objects
Function drawObjects:Int()

    ' Set up required integer counters
    Local i:Int

    ' If the player is defined, use it as the origin, otherwise (0,0)
    Local ox:Float = 0.0
    Local oy:Float = 0.0
    If Not ( player = -1 )
        ox = objArray[player].x - gx/2.0
        oy = objArray[player].y - gy/2.0
    EndIf

    ' Loop over all objects
    If Len(objArray) > 0
        For i = 0 To Len(objArray) - 1
            If objArray[i].alive = 1

                Local x:Float = objArray[i].x  ;  Local y:Float = objArray[i].y

                Local x1:Float = -4.0  ;  Local y1:Float = +8.0
                Local x2:Float = +4.0  ;  Local y2:Float = +8.0
                Local x3:Float = +4.0  ;  Local y3:Float = -8.0
                Local x4:Float = -4.0  ;  Local y4:Float = -8.0

                Local s:Float = Sin(objArray[i].ang)
                Local c:Float = Cos(objArray[i].ang)

                SetRotation(objArray[i].ang)
                DrawLine(x + x1 - ox, y - y1 - oy, x + x2 - ox, y - y2 - oy)
                DrawLine(x + x2 - ox, y - y2 - oy, x + x3 - ox, y - y3 - oy)
                DrawLine(x + x3 - ox, y - y3 - oy, x + x4 - ox, y - y4 - oy)
                DrawLine(x + x4 - ox, y - y4 - oy, x + x1 - ox, y - y1 - oy)
                SetRotation(0)
            EndIf
        Next
    EndIf

    Return 0
End Function




_Skully(Posted 2009) [#2]
You would be better off storing your polygon objects as vectors and rotating the points and use a non-rotated drawline


PoliteProgrammer(Posted 2009) [#3]
I actually considered that, but I found that the line gets rather pixelated, so I wanted to use setrotation to get a more continuous-looking line; is there a way to do that without using setrotation?


Brucey(Posted 2009) [#4]
I found that the line gets rather pixelated

It doesn't mean you need to draw the line pixel by pixel.

Given a vector and a starting point, you have available the two end points, which you can then call DrawLine with.


_Skully(Posted 2009) [#5]
Alpha the tangent for 1 pixel and shade it... should look pretty good.