Code archives/Miscellaneous/2D Camera MouseLook

This code has been declared by its author to be Public Domain code.

Download source code

2D Camera MouseLook by UnderwoodNullium2008
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.
SuperStrict

Graphics(1024,768,1)
SeedRnd MilliSecs()
HideMouse()


Global CAMX#,CAMY#

Global cursorx#,cursory#
Global cursorox#,cursoroy#
Global mx#,my#
Global mox#,moy#

Global sensitivity# = 2.0
Global cursorradiusx# = 1024
Global cursorradiusy# = 768

Global zoom# = 1.0

Global playerx# = 512,playery# = 384
Global playerox#,playeroy#



SetCamSnap(playerx,playery)


While Not KeyHit(key_escape)
Cls

	UpdatePlayer()

		UpdateCursor(playerx,playery)
                      If MouseDown(1) ShakeCam(5)

		SetDrawCam()

	SetColor(255,0,0)
		DrawOval(playerx-4,playery-4,9,9)
	SetColor(0,0,255)
		DrawOval(CAMX-4,CAMY-4,9,9)
	SetColor(0,255,0)
		DrawOval(cursorx-4,cursory-4,9,9)	
	SetColor(255,255,255)
		DrawRect(512,384,15,67)

		SetDrawHUD()

	DrawText("CamX = " + Int CAMX,10,10)
	DrawText("CamY = " + Int CAMY,10,25)

Flip
Wend
End





'==================================================================================================







Function UpdateCursor(px:Float,py:Float)

	mx = MouseX()
	my = MouseY()

	If mx > 812										' create a infinite bounding box for the mouse
		MoveMouse(512,my)
		mox = (mox - 300)
	EndIf

	If mx < 212
		MoveMouse(512,my)
		mox = (mox + 300)
	EndIf

	If my > 684
		MoveMouse(mx,384)
		moy = (moy - 300)
	EndIf

	If my < 84
		MoveMouse(mx,384)
		moy = (moy + 300)
	EndIf

		cursorx = (cursorx + (mousexspeed() * sensitivity))			' create a cursor that acts like a mouse
		cursory = (cursory + (mouseyspeed() * sensitivity))

	While Not PointInOval(cursorx,cursory,px,py,cursorradiusx*zoom,cursorradiusy*zoom)
		cursorx = (cursorx - Cos(GetAngle(px,py,cursorx,cursory)))
		cursory = (cursory - Sin(GetAngle(px,py,cursorx,cursory)))
	Wend

	CAMX = ((px + cursorx) / 2)							' when using UpdateCursor, move cam...
	CAMY = ((py + cursory) / 2)

End Function



Function UpdatePlayer()

	If KeyDown(key_right)
		playerx = (playerx + 1)
	EndIf

	If KeyDown(key_left)
		playerx = (playerx - 1)
	EndIf

	cursorx = (cursorx + PlayerXVel())						' have to add differences for cursor as well...  it's attached.
	cursory = (cursory + PlayerYVel())

End Function



Function MouseXSpeed:Int()

	Local result:Int = (MouseX() - mox)
	mox = MouseX()

	Return(result)

End Function



Function MouseYSpeed:Int()

	Local result:Int = (MouseY() - moy)
	moy = MouseY()

	Return(result)

End Function



Function PlayerXVel:Float()

	Local result:Int = (playerx - playerox)
	playerox = playerx

	Return(result)

End Function



Function PlayerYVel:Float()

	Local result:Int = (playery - playeroy)
	playeroy = playery

	Return(result)

End Function









'==================================================================================================








Function SetDrawHUD()

	SetOrigin(0,0)													' for non-camera related drawing

EndFunction



Function SetDrawCam()

	SetOrigin(((GraphicsWidth() / 2) - CAMX),((GraphicsHeight() / 2) - CAMY))			' draw the camera

End Function



Function SetCamSnap(px:Float,py:Float)										' auto-set the camera

	CAMX = px
	CAMY = py

End Function



Function GetCamX:Float()

	Return(CAMX)

End Function



Function GetCamY:Float()

	Return(CAMY)

End Function



Function ShakeCam(radius:Float)

	CAMX = (CAMX + Rnd(-radius,radius))
	CAMY = (CAMY + Rnd(-radius,radius))

End Function





'==================================================================================================






' the distance formula

Function GetDistance:Float(x1:Float,y1:Float,x2:Float,y2:Float)

   Return(Sqr(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))))

End Function



Function GetAngle:Float(x1:Float,y1:Float,x2:Float,y2:Float)

   Return((ATan2((y2 - y1),(x2 - x1)) + 360) Mod 360)

End Function



Function PointInOval:Int(px#,py#,ox#,oy#,width#,height#)

	Return((px - ox)^2 / width^2 + (py - oy)^2 / height^2 < 1)

End Function

Comments

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


plash2008
Interesting code. How/where do we use ShakeCam?

If I do it after the update calls it negates the changes on the next update.


UnderwoodNullium2008
Thanks Plash! Sorry, I forgot to add that...

It should be between the UpdateCursor() and SetDrawCam() functions in the main loop.

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. ;)


plash2008
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).


UnderwoodNullium2009
Sorry for the late response. The Rectangle is suppose to represent "The environment", like a trashcan or something... :P

The red circle is the "player", and the green one is the cursor...

If I remember...


N2009
Bwahahahaha.


UnderwoodNullium2009
:D Nice name by the way... Good choice.


Code Archives Forum