Tuesday, June 20, 2017

Monkey-X - Closest Point in Line Segment - code example


This can be turned into a collision function. Circle to line segment. Use a 
distance function for that.


' Example on how to get the closest point in a line segment
' to another point.
'
'

Import mojo

Class point
    Field x:Float
    Field y:Float
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(30)        
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255        
        DrawText "Mouse the mouse around to see the closest point",0,0
        DrawText "of the line segment..",0,20
        Local mypoint:point = New point(0,0)
        ' the mouse location for point x and y closest to line point
        Local cx:Float=MouseX(),cy:Float=MouseY()
        ' line coordinates
        Local lx1:Float=100
        Local ly1:Float=100
        Local lx2:Float=200
        Local ly2:Float=200
        ' draw the line
        SetColor 100,100,100
        DrawLine lx1,ly1,lx2,ly2
        ' get the closest point on the line segment
        mypoint = getclosestpointonsegment(lx1,ly1,lx2,ly2,cx,cy)
        ' draw this point
           SetColor 255,255,0
           DrawCircle mypoint.x,mypoint.y,10

    End Method
End Class

Function getclosestpointonsegment:point(sx1:Int, sy1:Int, sx2:Int, sy2:Int, px:Int, py:Int)
    Local xDelta:Float = sx2 - sx1
    Local yDelta:Float = sy2 - sy1
    Local u:Float

    If ((xDelta = 0) And (yDelta = 0))    
      Error("Segment start equals segment end")
    End If

    u = ((px - sx1) * xDelta + (py - sy1) * yDelta) / (xDelta * xDelta + yDelta * yDelta)

       Local closestPoint:point = New point(0,0)
    If (u < 0)
      closestPoint = New point(sx1, sy1)
    Else If (u > 1)
      closestPoint = New point(sx2, sy2)
    Else
      closestPoint = New point(Int(Floor(sx1 + u * xDelta)), Int(Floor(sy1 + u * yDelta)))
    End If
    
    Return closestPoint
End Function


Function Main()
    New MyGame()
End Function

Friday, June 9, 2017

Monkey-X - Interface Data in array - code example


'
' When building an interface on the screen it might
' be useful to put the data of it and store it elsewhere.
' Sometimes you might use the coordinates more then once
' and for other things like collision(mouse click) 
'
  
Import mojo

' Here we create 1 array with the coordinates 
' and width and height and 1 string for the
' text. Ends with c and s.
Global label1c:Int[] = [10,10,50,15] ' coordinates x,y,w,h
Global label1s:String = "Label1" ' text
Global label2c:Int[] = [10,30,50,15]
Global label2s:String = "Label2"

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(1)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        ' Here we call the drawlabel function.
        ' We put one string and a array in it.
        drawlabel(label1s,label1c)
        drawlabel(label2s,label2c)
    End Method
End Class

' this function takes a string and a int array
' into the function.
Function drawlabel:Void(a:String,b:Int[])
    SetColor 255,55,55
    ' draw a rect x,y,w,h
    DrawRect b[0],b[1],b[2],b[3]
    SetColor 255,255,255
    DrawText a,b[0]+b[2]/2,b[1]+b[3]/2,.5,.5
End Function

Function Main()
    New MyGame()
End Function