The Nature of Code for Monkey X - MOJO2
Monkey Forums/Monkey Code/The Nature of Code for Monkey X - MOJO2| 
 | ||
| The Nature of Code is a book written by Daniel Shiffman, it addresses math concepts like Vectors, Forces, Particles and more. All the examples in his book was written for JAVA and I translated it to Monkey MOJO2.  thank Daniel for given his permission to publish the code for Monkey. You can read his book here http://natureofcode.com/ to understand the concepts. These examples use MOJO2, so make sure you have MonkeyXPro85e or earlier. I recommend that you create one directory per example, for instance vector1_1, vector1_2 and so on, then save your examples in the directories, that is because some of the files will have the same name but the code may be different. You can get the zipped files here http://apd-games.com/NatureOfCode/vectors_MOJO2.zip 1-Vectors Example 1.1 Strict '************************************************* 'The Nature of Code 'Daniel Shiffman 'http://natureofcode.com 'Example 1-1: Bouncing Ball, no vectors 'Translated to Monnkey Mojo 2 by Aroldo Carvalho 'April 2016 '************************************************* Import mojo2 Class Vectors1_1 Extends App Field canvas:Canvas Field width:Float = 640 '= DeviceWidth() Field height:Float = 360 '= DeviceHeight() Field x:float = 100 Field y:float = 100 Field xspeed:float = 2.5 Field yspeed:float = 2 Method OnCreate:Int() SetUpdateRate(60) canvas = New Canvas width = DeviceWidth() height = DeviceHeight() If width = 0 width = 640 End If height = 0 height = 360 End Return True End Method OnRender:Int() canvas.Clear(1.0, 1.0, 1.0) canvas.SetAlpha(0.8) Local r:Float = 127 / 255.0, g:Float = 127 / 255.0, b:Float = 127 / 255.0 canvas.SetColor(0.5, 0.5, 0.5) Circle(x, y, 16, 16) canvas.SetAlpha(0.7) r = 127 / 255.0; g = 127 / 255.0; b = 127 / 255.0 canvas.SetColor(r, g, b) canvas.DrawEllipse(x, y, 16, 16) r = 127 / 255.0; g = 127 / 255.0; b = 64 / 255.0 canvas.SetColor(127, 127, 64) canvas.SetAlpha(1.0) canvas.Flush Return True End Method OnUpdate:Int() 'Add the current speed To the location. x = x + xspeed y = y + yspeed If ( (x > width) or (x < 0)) xspeed = xspeed * -1 EndIf If ( (y > height) or (y < 0)) yspeed = yspeed * -1 EndIf Return True End Method Circle:Void(x:Float, y:Float, w:Float, h:Float) For Local phi:Float = 0.0 Until 360.0 Step 10 canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h) Next End End Function Main:Int() New Vectors1_1() Return true End | 
| 
 | ||
| Vectors Example 1-2 Strict '************************************************* 'The Nature of Code 'Daniel Shiffman 'http://natureofcode.com 'Example 1-2: Bouncing Ball, with PVector! 'Translated to Monnkey Mojo 2 by Aroldo Carvalho 'April 2016 '************************************************* Import mojo2 Import pvector Class Vectors1_2 Extends App Field canvas:Canvas Field location:PVector Field velocity:PVector Field width:Float = 0 Field height:Float = 0 Method OnCreate:Int() SetUpdateRate(60) canvas = New Canvas width = DeviceWidth() height = DeviceHeight() If width = 0 width = 640 End If height = 0 height = 360 End location = New PVector(100, 100) velocity = New PVector(2.5, 5) Return True End Method OnRender:Int() canvas.Clear(1.0, 1.0, 1.0) 'canvas.DrawRect(0, 0, width, height) canvas.SetAlpha(0.8) Local r:Float = 127 / 255.0, g:Float = 127 / 255.0, b:Float = 127 / 255.0 canvas.SetColor(0.5, 0.5, 0.5) Circle(Self.location.x, Self.location.y, 16, 16) canvas.SetAlpha(0.7) r = 127 / 255.0; g = 127 / 255.0; b = 127 / 255.0 canvas.SetColor(r, g, b) canvas.DrawEllipse(Self.location.x, Self.location.y, 16, 16) r = 127 / 255.0; g = 127 / 255.0; b = 64 / 255.0 canvas.SetColor(127, 127, 64) canvas.SetAlpha(1.0) canvas.Flush Return True End Method OnUpdate:Int() 'Add the current speed to the location. location.add(velocity) If ( (location.x > width) or (location.x < 0)) velocity.x = velocity.x * -1 EndIf If ( (location.y > height) or (location.y < 0)) velocity.y = velocity.y * -1 EndIf Return True End Method Circle:Void(x:Float, y:Float, w:Float, h:Float) For Local phi:Float = 0.0 Until 360.0 Step 10 canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h) Next End End Function Main:Int() New Vectors1_2() Return true End 
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-3 Strict '************************************************* 'The Nature of Code 'Daniel Shiffman 'http://natureofcode.com 'Example 1-3: Vector subtraction 'Translated to Monnkey Mojo 2 by Aroldo Carvalho 'April 2016 '************************************************* Import mojo2 Import pvector Class Vectors1_3 Extends App Field canvas:Canvas Field location:PVector Field velocity:PVector Field width:Float = 0 Field height:Float = 0 Method OnCreate:Int() SetUpdateRate(60) canvas = New Canvas width = DeviceWidth() height = DeviceHeight() If width = 0 width = 640 End If height = 0 height = 360 End location = New PVector(100, 100) velocity = New PVector(2.5, 5) Return True End Method OnRender:Int() canvas.Clear(1.0, 1.0, 1.0) 'canvas.DrawRect(0, 0, width, height) canvas.SetAlpha(0.8) Local r:Float = 127 / 255.0, g:Float = 127 / 255.0, b:Float = 127 / 255.0 Circle(Self.location.x, Self.location.y, 16, 16) canvas.SetAlpha(0.7) r = 127 / 255.0; g = 127 / 255.0; b = 127 / 255.0 canvas.SetColor(r, g, b) canvas.DrawEllipse(Self.location.x, Self.location.y, 16, 16) r = 127 / 255.0; g = 127 / 255.0; b = 64 / 255.0 canvas.SetColor(127, 127, 64) canvas.SetAlpha(1.0) canvas.Flush Return True End Method OnUpdate:Int() 'Add the current speed to the location. location.add(velocity) If ( (location.x > width) or (location.x < 0)) velocity.x = velocity.x * -1 EndIf If ( (location.y > height) or (location.y < 0)) velocity.y = velocity.y * -1 EndIf Return True End Method Circle:Void(x:Float, y:Float, w:Float, h:Float) For Local phi:Float = 0.0 Until 360.0 Step 10 canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h) Next End End Function Main:Int() New Vectors1_3() Return true End 
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-4 
Strict
'*************************************************
'The Nature of Code
'Daniel Shiffman
'http://natureofcode.com
'Example 1-4: Vector multiplication
'Translated to Monnkey Mojo 2 by Aroldo Carvalho
'April 2016
'*************************************************
Import mojo2
Import pvector
Class Vectors1_4 Extends App
	Field canvas:Canvas
	
	Field mouse:PVector
	Field center:PVector
	Field width:Float = 0
	Field height:Float = 0
	
	Method OnCreate:Int()
		SetUpdateRate(60)
		canvas = New Canvas
		width = DeviceWidth()
		height = DeviceHeight()
		If width = 0
			width = 640
		End
		If height = 0
			height = 360
		End
		mouse = New PVector(MouseX(), MouseY())
		center = New PVector(width / 2, height / 2)
		Return True
	End
	Method OnRender:Int()
		canvas.Clear(1.0, 1.0, 1.0)
		'canvas.DrawRect(0, 0, width, height)
		canvas.SetColor(0.5, 0.5, 0.5)
'		canvas.DrawText("  " + mouse.x + " " + mouse.y, MouseX(), MouseY())
		canvas.PushMatrix
		canvas.Translate(width / 2, height / 2)
		canvas.DrawLine(0, 0, mouse.x, mouse.y)
		canvas.PopMatrix
		canvas.Flush
		Return True
	End
	Method OnUpdate:Int()
		mouse = New PVector(MouseX(), MouseY())
'		center = New PVector(width / 2, height / 2)
		mouse.sub(center)
		
		'Multiplying a vector ! The vector is now half its original size(multiplied by 0.5).
		mouse.mult(0.5)
		
		Return True
	End
	Method Circle:Void(x:Float, y:Float, w:Float, h:Float)
		For Local phi:Float = 0.0 Until 360.0 Step 10
			canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h)
		Next
	End
End
Function Main:Int()
	New Vectors1_4()
	Return true
End
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-5 
Strict
'*************************************************
'The Nature of Code
'Daniel Shiffman
'http://natureofcode.com
'Example 1-5: Vector magnitude
'Translated to Monnkey Mojo 2 by Aroldo Carvalho
'April 2016
'*************************************************
Import mojo2
Import pvector
Class Vectors1_5 Extends App
	Field canvas:Canvas
	
	Field width:Float = 0
	Field height:Float = 0 
	
	Field mouse:PVector
	Field center:PVector
	
	Field ellwidth:Int
	Field ellheight:Int
	
	Field touching:Int
	Field touchhit:Int
	Field touchX:Float
	Field touchY:Float
	
	Field accX:Float
	Field accY:Float
	Field accZ:Float
	Method OnCreate:Int()
		SetUpdateRate(60)
		
		canvas = New Canvas
		width = DeviceWidth()
		height = DeviceHeight()
		If width = 0
			width = 640
		End 
		If height = 0
			height = 360
		End
		Return True
	End
	Method OnRender:Int()
		canvas.Clear(1.0, 1.0, 1.0)
		
		'Two PVectors, one for the mouse location and one for the center of the window
	  	mouse = New PVector(MouseX(), MouseY())
	  	center = New PVector(width / 2, height / 2)
  		mouse.sub(center)
		canvas.SetColor(0.0, 0.0, 0.0)
		'The magnitude (i.e. length) of a vector can be accessed via the mag() function. Here it is used as the width of a rectangle drawn at the top of the window.
  		Local m:Float = mouse.mag()
  		'fill(0);
  		canvas.DrawRect(0, 0, m, 10)
		canvas.PushMatrix
		'Draw a line to represent the vector.
  		canvas.Translate(width / 2, height / 2)
		'DrawText("x:" + mx + " y:" + my, 0, 0)
  		canvas.DrawLine(0, 0, mouse.x, mouse.y)
		canvas.PopMatrix
		canvas.Flush
		Return True
	End
	Method OnUpdate:Int()
		input()	' get User Touch screen or mouse
		Return True
	End
	Method input:Int()
		'touching=0
		touchhit = -1
	
		For Local i:Int = 0 Until 64
			If TouchDown(i) touching+=1
		Next
		touchhit = TouchHit()
		touchX = TouchX(0)
		touchY = TouchY(0)
		accX = AccelX()
		accY = AccelY()
		accZ = AccelZ()
		Return True
	End	
End
Function Main:Int()
	New Vectors1_5()
	Return true
End
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-6 
Strict
'*************************************************
'The Nature of Code
'Daniel Shiffman
'http://natureofcode.com
'Example 1-5: Demonstration of normalizing a vector.
'             Normalizing a vector sets its length to 1.
'Translated to Monnkey Mojo 2 by Aroldo Carvalho
'April 2016
'*************************************************
Import mojo2
Import pvector
Class Vectors1_6 Extends App
	Field canvas:Canvas
	
	Field width:Float = 0 '= DeviceWidth()
	Field height:Float = 0 '= DeviceHeight()
	
	Field mouse:PVector
	Field center:PVector
	
	Field ellwidth:Int
	Field ellheight:Int
	
	Field touching:Int
	Field touchhit:Int
	Field touchX:Float
	Field touchY:Float
	
	Field accX:Float
	Field accY:Float
	Field accZ:Float
	Method OnCreate:Int()
		SetUpdateRate(60)
		
		canvas = New Canvas
		width = DeviceWidth()
		height = DeviceHeight()
		If width = 0
			width = 640
		End 
		If height = 0
			height = 360
		End
		Return True
	End
	Method OnRender:Int()
		canvas.Clear(1.0, 1.0, 1.0)
		
		'Two PVectors, one for the mouse location and one for the center of the window
	  	mouse = New PVector(MouseX(), MouseY())
	  	center = New PVector(width / 2, height / 2)
  		mouse.sub(center)
		
		mouse.normalize()	'In this example, after the vector is normalized,
							'it is multiplied by 50 so that it is viewable onscreen. 
							'Note that no matter where the mouse is, the vector will 
							'have the same length (50) due to the normalization process.
		
		mouse.mult(50)
		canvas.SetColor(0, 0, 0)
		canvas.PushMatrix
		'Draw a line to represent the vector.
  		canvas.Translate(width / 2, height / 2)
		'DrawText("x:" + mx + " y:" + my, 0, 0)
	
  		canvas.DrawLine(0, 0, mouse.x, mouse.y)
		canvas.PopMatrix
		canvas.Flush
		Return True
	End
	Method OnUpdate:Int()
		input()	' get User Touch screen or mouse
		Return True
	End
	Method input:Int()
		'touching=0
		touchhit = -1
	
		For Local i:Int = 0 Until 64
			If TouchDown(i) touching+=1
		Next
		touchhit = TouchHit()
		touchX = TouchX(0)
		touchY = TouchY(0)
		accX = AccelX()
		accY = AccelY()
		accZ = AccelZ()
		Return True
	End	
End
Function Main:Int()
	New Vectors1_6()
	Return true
End
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-7 Strict '************************************************* 'The Nature of Code 'Daniel Shiffman 'http://natureofcode.com 'Example 1-7: Motion 101. 'Translated to Monkey Mojo 2 by Aroldo Carvalho 'April 2016 '************************************************* Import mojo2 Import pvector Import mover Class Vectors1_7 Extends App Field width:Float = 0 Field height:Float = 0 Field mover:Mover Field touching:Int Field touchhit:Int Field touchX:Float Field touchY:Float Field accX:Float Field accY:Float Field accZ:Float Method OnCreate:Int() SetUpdateRate(60) width = DeviceWidth() height = DeviceHeight() If width = 0 width = 640 End If height = 0 height = 360 End mover = New Mover(width, height) Return True End Method OnRender:Int() mover.display() Return True End Method OnUpdate:Int() mover.update() mover.checkEdges() 'input() ' get User Touch screen or mouse Return True End Method input:Int() 'touching=0 touchhit = -1 For Local i:Int = 0 Until 8'64 If TouchDown(i) touching+=1 Next touchhit = TouchHit(0) touchX = TouchX(0) touchY = TouchY(0) accX = AccelX() accY = AccelY() accZ = AccelZ() Return True End End Function Main:Int() New Vectors1_7() Return true End 
Strict
Import mojo2
Import pvector
Class Mover
 	'Our object has two PVectors: location and velocity.
	Field location:PVector
	Field velocity:PVector
	Field acceleration:PVector
	Field topspeed:Float
	
	Field mouse:PVector
	
	Field width:Float
	Field height:Float
	
	Field canvas:Canvas
 
'	Method New(w:Float, h:Float)
'		'location = New PVector(w / 2, h / 2)
'		location = New PVector(Rnd(w), Rnd(h))
'	    velocity = New PVector(0, 0)
'	    acceleration = New PVector(-0.01, 0.01)
'	    topspeed = 5	' 10
'	End
	Method New(w:Float, h:Float)
		Self.width = w
		Self.height = h
	    location = New PVector(Rnd(Self.width), Rnd(Self.height))
	    velocity = New PVector(Rnd(-2, 2), Rnd(-2, 2))
		location = New PVector(width / 2, height / 2)
		canvas = New Canvas
	End
	Method update:Void()
'		mouse = New PVector(MouseX(), MouseY())
'		'Print "x:"+mouse.x + " y:"+mouse.y
'		acceleration = acceleration.sub(mouse, location)
'    	'Set magnitude of acceleration
'    	acceleration.normalize();
'   		acceleration.mult(0.2);
'		'Velocity changes according to acceleration
'    	velocity.add(acceleration);
'    	'Limit the velocity by topspeed
'    	velocity.limit(topspeed);
'    	'Location changes by velocity
    	location.add(velocity);
	End
	Method display:Void()
		canvas.Clear(1.0, 1.0, 1.0)
		canvas.SetAlpha(0.8)
		Local r:Float = 127 / 255.0, g:Float = 127 / 255.0, b:Float = 127 / 255.0
		canvas.SetColor(0.5, 0.5, 0.5)
		Circle(Self.location.x, Self.location.y, 16, 16)
		canvas.SetAlpha(0.7)
		r = 127 / 255.0; g = 127 / 255.0; b = 127 / 255.0
		canvas.SetColor(r, g, b)
	    canvas.DrawEllipse(Self.location.x, Self.location.y, 16, 16)
	    r = 127 / 255.0; g = 127 / 255.0; b = 64 / 255.0
		canvas.SetColor(127, 127, 64)
		canvas.SetAlpha(1.0)
		canvas.Flush
	End
	Method Circle:Void(x:Float, y:Float, w:Float, h:Float)
		For Local phi:Float = 0.0 Until 360.0 Step 10
			canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h)
			'DrawPoint(x + Cos(phi) * w, y - Sin(phi) * h)
		Next
	End
	Method checkEdges:Void()
	    If (location.x > Self.width)
	      Self.location.x = 0
	    Else If (Self.location.x < 0)
	      Self.location.x = Self.width
	    End
 
	    If (location.y > Self.height)
	      Self.location.y = 0
	    Else If (location.y < 0)
	      Self.location.y = Self.height
	    End
	End
End
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-8 Strict '************************************************* 'The Nature of Code 'Daniel Shiffman 'http://natureofcode.com 'Example 1-8: Motion 101 Acelleration. 'Translated to Monkey Mojo 2 by Aroldo Carvalho 'April 2016 '************************************************* Import mojo Import pvector Import mover Class Vectors1_8 Extends App Field width:Float = 0 Field height:Float = 0 Field mover:Mover Field touching:Int Field touchhit:Int Field touchX:Float Field touchY:Float Field accX:Float Field accY:Float Field accZ:Float Method OnCreate:Int() SetUpdateRate(60) width = DeviceWidth() height = DeviceHeight() If width = 0 width = 640 End If height = 0 height = 480 End mover = New Mover(width, height) Return True End Method OnRender:Int() mover.display() Return True End Method OnUpdate:Int() mover.update() mover.checkEdges() 'input() ' get Use r Touch screen or mouse Return True End Method input:Int() 'touching=0 touchhit = -1 For Local i:Int = 0 Until 64 If TouchDown(i) touching+=1 Next touchhit = TouchHit() touchX = TouchX(0) touchY = TouchY(0) accX = AccelX() accY = AccelY() accZ = AccelZ() Return True End End Function Main:Int() New Vectors1_8() Return true End 
Strict
Import mojo2
Import pvector
Class Mover
 	'Our object has two PVectors: location and velocity.
	Field location:PVector
	Field velocity:PVector
	Field acceleration:PVector
	Field topspeed:Float
	
	Field mouse:PVector
	
	Field width:Float
	Field height:Float
	
	Field canvas:Canvas
 
	Method New(w:Float, h:Float)
		width = w
		height = h
		location = New PVector(w / 2, h / 2)
	    velocity = New PVector(0, 0)
	    acceleration = New PVector(-0.001, 0.01)
	    topspeed =  10.0
		canvas = New Canvas
	End
'	Method New(w:Float, h:Float)
'		Self.width = w
'		Self.height = h
'	    location = New PVector(Rnd(Self.width), Rnd(Self.height))
'	    velocity = New PVector(Rnd(-2, 2), Rnd(-2, 2))
'		location = New PVector(width / 2, height / 2)
'		
'	End
	Method update:Void()
		'Velocity changes according to acceleration
    	velocity.add(acceleration)
    	'Limit the velocity by topspeed
    	velocity.limit(topspeed)
    	'Location changes by velocity
    	location.add(velocity)
	End
	Method display:Void()
		canvas.Clear(1.0, 1.0, 1.0)
		canvas.SetAlpha(0.8)
		Local r:Float = 127 / 255.0, g:Float = 127 / 255.0, b:Float = 127 / 255.0
		canvas.SetColor(0.5, 0.5, 0.5)
'		Print "vel x:" + Self.velocity.x + " vel y:" + Self.velocity.y
'		Print "Loc x:" + Self.location.x + " Loc y:" + Self.location.y
		Circle(Self.location.x, Self.location.y, 16, 16)
		canvas.SetAlpha(0.7)
		r = 127 / 255.0; g = 127 / 255.0; b = 127 / 255.0
		canvas.SetColor(r, g, b)
	    canvas.DrawEllipse(Self.location.x, Self.location.y, 16, 16)
	    r = 127 / 255.0; g = 127 / 255.0; b = 64 / 255.0
		canvas.SetColor(127, 127, 64)
		canvas.SetAlpha(1.0)
		canvas.Flush
	End
	Method Circle:Void(x:Float, y:Float, w:Float, h:Float)
		For Local phi:Float = 0.0 Until 360.0 Step 10
			canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h)
			'DrawPoint(x + Cos(phi) * w, y - Sin(phi) * h)
		Next
	End
	Method checkEdges:Void()
	    If (location.x > width)
	      location.x = 0
	    Else If (location.x < 0)
	      location.x = width
	    End
 
	    If (location.y > height)
	      location.y = 0
	    Else If (location.y < 0)
	      location.y = height
	    End
	End
End
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-10 Move the mouse '************************************************* 'The Nature of Code 'Daniel Shiffman 'http://natureofcode.com 'Example 1-10: Motion 101 Acelleration. 'Translated to Monkey Mojo 2 by Aroldo Carvalho 'April 2016 '************************************************* Import mojo2 Import pvector ' !!!!! ------ Import classes need lowercase names ------ !!!!!! Import mover Class Vectors1_10 Extends App Field width:Float = 0 '= DeviceWidth() Field height:Float = 0 '= DeviceHeight() Field mover:Mover Field touching:Int Field touchhit:Int Field touchX:Float Field touchY:Float Field accX:Float Field accY:Float Field accZ:Float Method OnCreate:Int() SetUpdateRate(60) width = DeviceWidth() height = DeviceHeight() If width = 0 width = 640 End If height = 0 height = 360 End mover = New Mover(width, height) Return True End Method OnRender:Int() mover.display() Return True End Method OnUpdate:Int() mover.update() 'mover.checkEdges() ' input() ' get Use r Touch screen or mouse Return True End Method input:Int() 'touching=0 touchhit = -1 For Local i:Int = 0 Until 64 If TouchDown(i) touching+=1 Next touchhit = TouchHit() touchX = TouchX(0) touchY = TouchY(0) accX = AccelX() accY = AccelY() accZ = AccelZ() Return True End End Function Main:Int() New Vectors1_10() Return true End 
Strict
Import mojo2
Import pvector
Class Mover
 	'Our object has two PVectors: location and velocity.
	Field location:PVector
	Field velocity:PVector
	Field acceleration:PVector
	Field topspeed:Float
	
	Field mouse:PVector
	
	Field width:Float
	Field height:Float
	
	Field canvas:Canvas
 
	Method New(w:Float, h:Float)
		canvas = New Canvas
		Self.width = w
		Self.height = h
	    location = New PVector(w / 2, h / 2)
	    velocity = New PVector(0, 0)
		topspeed = 5
	End
	Method update:Void()
		mouse = New PVector(MouseX(), MouseY())
		acceleration = New PVector(0, 0)
		acceleration = acceleration.sub(mouse, location)
    	'Set magnitude of acceleration
    	acceleration.normalize()
   		acceleration.mult(0.2)
		'Velocity changes according to acceleration
    	velocity.add(acceleration);
    	'Limit the velocity by topspeed
    	velocity.limit(topspeed);
    	'Location changes by velocity
    	location.add(velocity);
	End
	Method display:Void()
		canvas.Clear(1.0, 1.0, 1.0)
		canvas.SetAlpha(0.8)
		Local r:Float = 127 / 255.0, g:Float = 127 / 255.0, b:Float = 127 / 255.0
		canvas.SetColor(0.5, 0.5, 0.5)
		Circle(Self.location.x, Self.location.y, 16, 16)
		canvas.SetAlpha(0.7)
		r = 127 / 255.0; g = 127 / 255.0; b = 127 / 255.0
		canvas.SetColor(r, g, b)
	    canvas.DrawEllipse(Self.location.x, Self.location.y, 16, 16)
	    r = 127 / 255.0; g = 127 / 255.0; b = 64 / 255.0
		canvas.SetColor(127, 127, 64)
		canvas.SetAlpha(1.0)
		canvas.Flush
	End
	Method Circle:Void(x:Float, y:Float, w:Float, h:Float)
		For Local phi:Float = 0.0 Until 360.0 Step 10
			canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h)
			'DrawPoint(x + Cos(phi) * w, y - Sin(phi) * h)
		Next
	End
	Method checkEdges:Void()
	    If (location.x > Self.width)
	      Self.location.x = 0
	    Else If (Self.location.x < 0)
	      Self.location.x = Self.width
	    End
 
	    If (location.y > Self.height)
	      Self.location.y = 0
	    Else If (location.y < 0)
	      Self.location.y = Self.height
	    End
	End
End
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Method sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
	'	ret.x = v.x - vec.x
    '	ret.y = v.y - vec.y
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| Vectors Example 1-11 Move the mouse Strict '************************************************* 'The Nature of Code 'Daniel Shiffman 'http://natureofcode.com 'Example 1-11: Motion 101 Acelleration. 'Translated to Monkey Mojo 2 by Aroldo Carvalho 'April 2016 '************************************************* Import mojo2 Import pvector ' !!!!! ------ Import classes need lowercase names ------ !!!!!! Import mover Class Vectors1_11 Extends App Field width:Float = 0 Field height:Float = 0 Field canvas:Canvas Field movers:Mover[20] Field touching:Int Field touchhit:Int Field touchX:Float Field touchY:Float Field accX:Float Field accY:Float Field accZ:Float Method OnCreate:Int() SetUpdateRate(60) canvas = New Canvas Local r:= 12 Local g:= 10 Local b:= 5 For Local i:= 0 Until movers.Length movers[i] = New Mover() movers[i].setColor(r, g, b) r *= 2; g *= 4; b *= 2 End Return True End Method OnRender:Int() canvas.Clear(1.0, 1.0, 1.0) For Local i:= 0 Until movers.Length movers[i].update() movers[i].display(canvas) 'Print "i:"+i+ " x:"+ movers[i].location.x + " y:"+ movers[i].location.y End canvas.Flush Return True End Method OnUpdate:Int() ' input() ' get Use r Touch screen or mouse Return True End Method input:Int() 'touching=0 touchhit = -1 For Local i:Int = 0 Until 64 If TouchDown(i) touching+=1 Next touchhit = TouchHit() touchX = TouchX(0) touchY = TouchY(0) accX = AccelX() accY = AccelY() accZ = AccelZ() Return True End End Function Main:Int() New Vectors1_11() Return true End Strict Import mojo2 Import pvector Class Mover 'Our object has two PVectors: location and velocity. Field location:PVector Field velocity:PVector Field topspeed:Float Field w:Float Field h:Float Field R:Float Field G:Float Field B:Float Method New() w = DeviceWidth() h = DeviceHeight() If w = 0 w = 640 End If h = 0 h = 360 End location = New PVector(Rnd(w), Rnd(h)) velocity = New PVector(0, 0) topspeed = 5 End Method update:Void() 'Compute a vector that points from location to mouse Local mouse:PVector = New PVector(MouseX(), MouseY()) Local acceleration:PVector = PVector.sub(mouse, Self.location) 'Set magnitude of acceleration acceleration.normalize() acceleration.mult(0.2) 'Velocity changes according to acceleration velocity.add(acceleration) 'Limit the velocity by topspeed velocity.limit(topspeed) 'Location changes by velocity location.add(velocity) End Method display:Void(canvas:Canvas) canvas.SetAlpha(0.8) Local r:Float = 127 / 255.0, g:Float = 127 / 255.0, b:Float = 127 / 255.0 canvas.SetColor(0.5, 0.5, 0.5) Circle(location.x, location.y, 16, 16, canvas) canvas.SetAlpha(0.7) canvas.SetColor(R, G, B) canvas.DrawEllipse(location.x, location.y, 16, 16) r = 127 / 255.0; g = 127 / 255.0; b = 64 / 255.0 canvas.SetColor(127, 127, 64) canvas.SetAlpha(1.0) End Method Circle:Void(x:Float, y:Float, w:Float, h:Float, canvas:Canvas) For Local phi:Float = 0.0 Until 360.0 Step 10 canvas.DrawLine(x + Cos(phi - 10) * w, y - Sin(phi - 10) * h, x + Cos(phi) * w, y - Sin(phi) * h) Next End Method checkEdges:Void() If (location.x > w) location.x = 0 Else If (location.x < 0) location.x = w End If (location.y > h) location.y = 0 Else If (location.y < 0) location.y = h End End Method setColor:Void(r:Float, g:Float, b:Float) R = r / 255.0; G = g / 255.0; B = b / 255.0 End End 
Strict
Class PVector
 	Field x:Float
	Field y:Float
	
	Method New(x_:Float = 0, y_:Float = 0)
		x = x_
    	y = y_
	End
'	Method New(v:PVector, vec:PVector)
'		x = x_
'    	y = y_
'	End
	
 	'A function to add another PVector to this PVector. Simply add the x components and the y components together.
	Method add:Int(v:PVector)
	    y = y + v.y
	    x = x + v.x
		Return True
	End
	Function add:PVector(v1:PVector, v2:PVector)
		Local v3:PVector = New PVector(v1.x + v2.x, v1.y + v2.y)
		Return v3
	End
	Method sub:Int(v:PVector)
   		x = x - v.x
    	y = y - v.y
		Return True
	End
	Function sub:PVector(v:PVector, vec:PVector)
   		Local ret:PVector = New PVector(v.x - vec.x, v.y - vec.y)
		Return ret
	End
	Method mult:Int(n:float)
		x = x * n
		y = y * n
		Return True
	End
	Method div:Int(n:float)
		x = x / n
		y = y / n
		Return True
	End
	Method mag:Float()
  		Return Sqrt(x * x + y * y)
	End
	Method normalize:Void()
		Local m:Float = mag()
  		If (m <> 0)
			div(m)
		End
	End
	Method limit:Void(high:Float)
		If (Self.mag() > high)
          normalize()
          mult(high)
		End
	End
End
 | 
| 
 | ||
| would be nice if this was all in a single easy to grab zip file as well. | 
| 
 | ||
| Paul - Taiphoz, You can get the zipped files here http://apd-games.com/NatureOfCode/vectors_MOJO2.zip. |