[bmx] Camera Pan Effect by Cruis.In [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Camera Pan Effect
Author : Cruis.In
Posted : 1+ years ago

Description : 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.


Code :
Code (blitzmax) Select
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 :


dw817(Posted 1+ years ago)

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


Guy Fawkes(Posted 1+ years ago)

 A better Blitz3D version with AUTOMATIC 2D-Camera Panning ::
Graphics3D 800, 600, 0, 2

Type TPlayer

Field X#
Field Y#
Field W#
Field H#

End Type

Global tx
Global ty

Global X1#
Global Y1#

Global X2#
Global Y2#

Global newplayer.tplayer = New tplayer

newplayerW# = 32.0
newplayerH# = 32.0

Global starsx% [ 5000 ]
Global starsy% [ 5000 ]

For I = 0 To 4999

starsx [ i ] = Rnd ( -1600, 1600 )

starsy [ i ] = Rnd ( -1600, 1600 )

Next

While Not KeyHit ( 1 )

Cls

Color 102, 102, 255

Rect newplayerX#, newplayerY#, newplayerW#, newplayerH#, 0

Color 255, 255, 255

For I% = 0 To 4999
Oval starsx [ i ], starsy [ i ], 1, 1, 1
Next

GetInput ( )
Render ( )

UpdateWorld ( )
RenderWorld ( )

PXString$ = "Player X :: " + ( newplayerX# )
PYString$ = "Player Y :: " + ( newplayerY# )
PWString$ = "Player W :: " + ( newplayerW# )
PHString$ = "Player H :: " + ( newplayerH# )

X1# =   ( ( newPlayerX#  - newplayerW# ) - ( (  StringWidth ( PXString$ ) / 2.0 ) ) )
Y1# = ( ( ( newPlayerY#  - newplayerH# ) + ( ( StringHeight ( PYString$ ) / 2.0 ) ) ) - ( newplayerH# ) + 20 )
X2# =   ( ( newPlayerX#  - newplayerW# ) - ( (  StringWidth ( PWString$ ) / 2.0 ) ) )
Y2# = ( ( ( newPlayerY#  - newplayerH# ) + ( ( StringHeight ( PHString$ ) / 2.0 ) ) ) - ( newplayerH# ) + 20 )

Color 102, 102, 255
Text X1#, Y1#-70, PXString$
Text X1#, Y1#-50, PYString$
Text X2#, Y2#-30, PWString$
Text X2#, Y2#-10, PHString$

Flip

Wend

End

Function Render()

Color 102, 102, 255

tx = ( (  GraphicsWidth ( ) / 2.0 ) - ( newplayerX# / 2.0 ) )
ty = ( ( GraphicsHeight ( ) / 2.0 ) - ( newplayerY# / 2.0 ) )

If   ( newplayerX# < ( - (  GraphicsWidth ( ) - ( 2.0 * newplayerW#        ) ) ) ) Then newplayerX# = ( - (  GraphicsWidth ( ) - ( 2.0 * newplayerW#       ) ) )
If   ( newplayerX# > (   (  GraphicsWidth ( ) - ( 2.0 * newplayerW#        ) ) ) ) Then newplayerX# = (   (  GraphicsWidth ( ) - ( 2.0 * newplayerW#       ) ) )
If   ( newplayerY# < ( - ( GraphicsHeight ( ) - ( 3.0 + newplayerH# / 2.0  ) ) ) ) Then newplayerY# = ( - ( GraphicsHeight ( ) - ( 3.0 + newplayerH# / 2.0 ) ) )
If   ( newplayerY# > (   ( GraphicsHeight ( ) - ( 3.0 + newplayerH# / 2.0  ) ) ) ) Then newplayerY# = (   ( GraphicsHeight ( ) - ( 3.0 + newplayerH# / 2.0 ) ) )

Origin tx, ty

End Function

Function GetInput()

If ( KeyDown(42) Or KeyDown(54 ) )  = 0 Then player_speed# = 1.0
If ( KeyDown(42) Or KeyDown(54 ) ) <> 0 Then player_speed# = 3.0

If ( KeyDown(30) Or KeyDown(203) ) <> 0 Then newplayerX#  = newplayerX# - player_speed#
If ( KeyDown(32) Or KeyDown(205) ) <> 0 Then newplayerX#  = newplayerX# + player_speed#
If ( KeyDown(17) Or KeyDown(200) ) <> 0 Then newplayerY#  = newplayerY# - player_speed#
If ( KeyDown(31) Or KeyDown(208) ) <> 0 Then newplayerY#  = newplayerY# + player_speed#

End Function
Not too entirely sure where I screwed up on the map-edge "collision" math. Perhaps someone can help! :)Anyways, Enjoy!~GF [/i]