Code archives/Graphics/Camera Pan Effect

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

Download source code

Camera Pan Effect by Cruis.In2013
The camera pans based on the location of your mouse pointer. E.g. Move the mouse left, to 'peek' into areas outside the viewing area.

Hard to explain, try it.
Strict

Graphics 800,600

Global newplayer:tplayer = New tplayer
Global starsx%[500]
Global starsy%[500]

For Local I= 0 To 499
	starsx[i]=Rnd(-1600, 1600)
	starsy[i]=Rnd(-1600, 1600)
Next

Type TPlayer

	Field X:Float 
	Field Y:Float 
	
	
	Method Render()
		SetColor(235,110,214)
		SetRotation (0)
		SetAlpha 1.0
		
		Rem
		variables To hold the value of MOUSE X & Y POSITION. Since my intention is To focus the 'cam' on the 
		'player at the centre of the screen, I want To start out at the centre, hence the division of Graphics
		with resolution
		End Rem
		
		Local tx = GraphicsWidth()/2 - x 
		Local ty = GraphicsHeight()/2 - y
		
		Local PanX:Float = MouseX() - GraphicsWidth()/2
		Local panY:Float = MouseY() - GraphicsHeight()/2
		
			
		'setting the origin
		SetOrigin(tx - panx , ty - pany)
		
		
	End Method
	
	'standard input	
	Method GetInput()
		If KeyDown(Key_left)
			x:-1
		End If
		
		If KeyDown(key_right)
			x:+1
		End If
		
		If KeyDown(key_UP)
			y:-1
		End If
		
		If KeyDown(Key_Down)	
			y:+1
		End If
	End Method

End Type

	
	While Not KeyHit(key_ESCAPE)
		Cls
	
		
			
		newplayer.getInput()
		newplayer.render()
		
		DrawRect(newplayer.x, newplayer.y, 20,20)
		
		DrawText("X= "+newplayer.x, 100,100)
		DrawText("Y= "+newplayer.y, 100,120)
	
	
		For Local I% = 0 To 499
			DrawOval starsx[i], starsy[i], 3, 3
		Next
		
		Flip
	Wend

Comments

dw8172015
Pretty cool code, Cruis-In. Think you can do it in 3-D space now ?


Guy Fawkes2016
A better Blitz3D version with AUTOMATIC 2D-Camera Panning ::



Not too entirely sure where I screwed up on the map-edge "collision" math. Perhaps someone can help! :)

Anyways,

Enjoy!

~GF


Code Archives Forum