Ooops
November 28, 2020, 11:05:24 AM

Author Topic: [bmx] 2D Camera MouseLook by UnderwoodNullium [ 1+ years ago ]  (Read 384 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 2D Camera MouseLook
Author : UnderwoodNullium
Posted : 1+ years ago

Description : Using this, you can set cameras up around your world, and use the mouse to look around (up to 4x screen area).  There is also a screen shake funtion too.  Useful for Soldat type games...  Included also is:

SetCameraSnap()  ...which sets the camera to a new point.
ShakeCam()       ...which shakes the camera around a certain area.

Also you can set the mouse sensitivity, and the 'zoom' level.


Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Graphics(1024,768,1)
  4. SeedRnd MilliSecs()
  5. HideMouse()
  6.  
  7.  
  8. Global CAMX#,CAMY#
  9.  
  10. Global cursorx#,cursory#
  11. Global cursorox#,cursoroy#
  12. Global mx#,my#
  13. Global mox#,moy#
  14.  
  15. Global sensitivity# = 2.0
  16. Global cursorradiusx# = 1024
  17. Global cursorradiusy# = 768
  18.  
  19. Global zoom# = 1.0
  20.  
  21. Global playerx# = 512,playery# = 384
  22. Global playerox#,playeroy#
  23.  
  24.  
  25.  
  26. SetCamSnap(playerx,playery)
  27.  
  28.  
  29. While Not KeyHit(key_escape)
  30. Cls
  31.  
  32.         UpdatePlayer()
  33.  
  34.                 UpdateCursor(playerx,playery)
  35.                       If MouseDown(1) ShakeCam(5)
  36.  
  37.                 SetDrawCam()
  38.  
  39.         SetColor(255,0,0)
  40.                 DrawOval(playerx-4,playery-4,9,9)
  41.         SetColor(0,0,255)
  42.                 DrawOval(CAMX-4,CAMY-4,9,9)
  43.         SetColor(0,255,0)
  44.                 DrawOval(cursorx-4,cursory-4,9,9)      
  45.         SetColor(255,255,255)
  46.                 DrawRect(512,384,15,67)
  47.  
  48.                 SetDrawHUD()
  49.  
  50.         DrawText("CamX = " + Int CAMX,10,10)
  51.         DrawText("CamY = " + Int CAMY,10,25)
  52.  
  53. Flip
  54. Wend
  55. End
  56.  
  57.  
  58.  
  59.  
  60.  
  61. '==================================================================================================
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. Function UpdateCursor(px:Float,py:Float)
  70.  
  71.         mx = MouseX()
  72.         my = MouseY()
  73.  
  74.         If mx > 812                                                                             ' create a infinite bounding box for the mouse
  75.                 MoveMouse(512,my)
  76.                 mox = (mox - 300)
  77.         EndIf
  78.  
  79.         If mx < 212
  80.                 MoveMouse(512,my)
  81.                 mox = (mox + 300)
  82.         EndIf
  83.  
  84.         If my > 684
  85.                 MoveMouse(mx,384)
  86.                 moy = (moy - 300)
  87.         EndIf
  88.  
  89.         If my < 84
  90.                 MoveMouse(mx,384)
  91.                 moy = (moy + 300)
  92.         EndIf
  93.  
  94.                 cursorx = (cursorx + (mousexspeed() * sensitivity))                     ' create a cursor that acts like a mouse
  95.                 cursory = (cursory + (mouseyspeed() * sensitivity))
  96.  
  97.         While Not PointInOval(cursorx,cursory,px,py,cursorradiusx*zoom,cursorradiusy*zoom)
  98.                 cursorx = (cursorx - Cos(GetAngle(px,py,cursorx,cursory)))
  99.                 cursory = (cursory - Sin(GetAngle(px,py,cursorx,cursory)))
  100.         Wend
  101.  
  102.         CAMX = ((px + cursorx) / 2)                                                     ' when using UpdateCursor, move cam...
  103.         CAMY = ((py + cursory) / 2)
  104.  
  105. End Function
  106.  
  107.  
  108.  
  109. Function UpdatePlayer()
  110.  
  111.         If KeyDown(key_right)
  112.                 playerx = (playerx + 1)
  113.         EndIf
  114.  
  115.         If KeyDown(key_left)
  116.                 playerx = (playerx - 1)
  117.         EndIf
  118.  
  119.         cursorx = (cursorx + PlayerXVel())                                              ' have to add differences for cursor as well...  it's attached.
  120.         cursory = (cursory + PlayerYVel())
  121.  
  122. End Function
  123.  
  124.  
  125.  
  126. Function MouseXSpeed:Int()
  127.  
  128.         Local result:Int = (MouseX() - mox)
  129.         mox = MouseX()
  130.  
  131.         Return(result)
  132.  
  133. End Function
  134.  
  135.  
  136.  
  137. Function MouseYSpeed:Int()
  138.  
  139.         Local result:Int = (MouseY() - moy)
  140.         moy = MouseY()
  141.  
  142.         Return(result)
  143.  
  144. End Function
  145.  
  146.  
  147.  
  148. Function PlayerXVel:Float()
  149.  
  150.         Local result:Int = (playerx - playerox)
  151.         playerox = playerx
  152.  
  153.         Return(result)
  154.  
  155. End Function
  156.  
  157.  
  158.  
  159. Function PlayerYVel:Float()
  160.  
  161.         Local result:Int = (playery - playeroy)
  162.         playeroy = playery
  163.  
  164.         Return(result)
  165.  
  166. End Function
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. '==================================================================================================
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. Function SetDrawHUD()
  186.  
  187.         SetOrigin(0,0)                                                                                                  ' for non-camera related drawing
  188.  
  189. EndFunction
  190.  
  191.  
  192.  
  193. Function SetDrawCam()
  194.  
  195.         SetOrigin(((GraphicsWidth() / 2) - CAMX),((GraphicsHeight() / 2) - CAMY))                       ' draw the camera
  196.  
  197. End Function
  198.  
  199.  
  200.  
  201. Function SetCamSnap(px:Float,py:Float)                                                                          ' auto-set the camera
  202.  
  203.         CAMX = px
  204.         CAMY = py
  205.  
  206. End Function
  207.  
  208.  
  209.  
  210. Function GetCamX:Float()
  211.  
  212.         Return(CAMX)
  213.  
  214. End Function
  215.  
  216.  
  217.  
  218. Function GetCamY:Float()
  219.  
  220.         Return(CAMY)
  221.  
  222. End Function
  223.  
  224.  
  225.  
  226. Function ShakeCam(radius:Float)
  227.  
  228.         CAMX = (CAMX + Rnd(-radius,radius))
  229.         CAMY = (CAMY + Rnd(-radius,radius))
  230.  
  231. End Function
  232.  
  233.  
  234.  
  235.  
  236.  
  237. '==================================================================================================
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. ' the distance formula
  245.  
  246. Function GetDistance:Float(x1:Float,y1:Float,x2:Float,y2:Float)
  247.  
  248.    Return(Sqr(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))))
  249.  
  250. End Function
  251.  
  252.  
  253.  
  254. Function GetAngle:Float(x1:Float,y1:Float,x2:Float,y2:Float)
  255.  
  256.    Return((ATan2((y2 - y1),(x2 - x1)) + 360) Mod 360)
  257.  
  258. End Function
  259.  
  260.  
  261.  
  262. Function PointInOval:Int(px#,py#,ox#,oy#,width#,height#)
  263.  
  264.         Return((px - ox)^2 / width^2 + (py - oy)^2 / height^2 < 1)
  265.  
  266. End Function


Comments :


UnderwoodNullium(Posted 1+ years ago)

 *also bounds the mouse cursor to the oval created by the screen height and width.


plash(Posted 1+ years ago)

 Interesting code. How/where do we use ShakeCam?If I do it after the update calls it negates the changes on the next update.


UnderwoodNullium(Posted 1+ years ago)

 Thanks Plash!  Sorry, I forgot to add that...It should be between the UpdateCursor() and SetDrawCam() functions in the main loop.
Code: [Select]
UpdateCursor(playerx,playery)

If MouseDown(1)
     ShakeCam(5)
EndIf

SetDrawCam()
So when you hold the mouse down, the camera shakes, useful for explosions and the like.  ;)


plash(Posted 1+ years ago)

 Ah, kewl!The player rendering seems to be inverted (right key seems to move left, and left key seems to move right).The cursor also does not follow the player (rectangle).


UnderwoodNullium(Posted 1+ years ago)

 Sorry for the late response.  The Rectangle is suppose to represent "The environment", like a trashcan or something...  :PThe red circle is the "player", and the green one is the cursor...  If I remember...


N(Posted 1+ years ago)

 Bwahahahaha.


UnderwoodNullium(Posted 1+ years ago)

 :D  Nice name by the way...  Good choice. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal