Movement problem

BlitzMax Forums/BlitzMax Programming/Movement problem

Hi guys/gals,

I'm trying to create a pseudo camera zoom in a bmx game I'm making at the moment. I have a number of objects at certain coords and myself, at certain coords always displayed at the centre of the screen. I want to change the "zoom" of the "camera" to achieve an effect similar to this:

The problem with this example is, if you move anywhere, the effect is broken somehow. Any chance anyone knows how to fix it?

Global objList:TList=CreateList()

Graphics 640,480,0,60

Global s#=1.0

For Local i=1 To 25
	Local o:obj=New obj
	o.x=Rand(-GraphicsWidth(),GraphicsWidth())
	o.y=Rand(-GraphicsHeight(),GraphicsHeight())
Next

Global youX#,youY#


While KeyHit(KEY_ESCAPE)=0
Cls	
	If KeyDown(KEY_NUMADD) Then s:+0.1
	If KeyDown(KEY_NUMSUBTRACT) Then s:-0.1
	If s<0.1 Then s=0.1
	If s>4 Then s=4	
	
	
	If KeyDown(KEY_UP) Then youY:-1*s
	If KeyDown(KEY_DOWN) Then youY:+1*s
	If KeyDown(KEY_LEFT) Then youX:-1*s
	If KeyDown(KEY_RIGHT) Then youX:+1*s
	
	
	updateObjs()

	SetColor 0,255,0
	DrawRect (GraphicsWidth()/2)-3,(GraphicsHeight()/2)-3,7,7
	
	SetColor 255,255,255;DrawText s,0,0
	DrawText "Press +/- on the numpad to zoom. Cursors to move",0,10
Flip
Wend
End


Type obj
	Field x,y
	
	Method New() ListAddLast(objList,Self); End Method
	Method update()
		Local dx=youX-GraphicsWidth()/2
		Local dy=youY-GraphicsHeight()/2
		SetScale 1,1
		SetColor 255,0,0
		DrawRect (x*s-3)-dx,(y*s-3)-dy,7,7
	End Method
End Type	

Function updateObjs()
	For Local i:obj=EachIn objList
		i.update()
	Next
End Function


Here is a redo of your code that works:

Strict

Global ViewX:Float, ViewY:Float
Global ViewZoom:Float = 1.0

Global EntityList:TList = CreateList()
Type TEntity
	Field X:Float
	Field Y:Float
	
	Function Create:TEntity (_X:Float, _Y:Float)
		Local Entity:TEntity = New TEntity
		Entity.X = _X
		Entity.Y = _Y
		EntityList.AddLast(Entity)
		Return Entity
	End Function

	Method Render()
		Local ScreenX:Float = (X * ViewZoom) + ViewX
		Local ScreenY:Float = (Y * ViewZoom) + ViewY
				
		DrawRect ScreenX-(3*ViewZoom), ScreenY-(3*ViewZoom),(6*ViewZoom),(6*ViewZoom)
	End Method
End Type

Graphics 640,480

For Local I:Int = 1 To 50
	TEntity.Create(Rnd(-256,GraphicsWidth()+256),Rnd(-256,GraphicsHeight()+256))
Next

Local Player:TEntity = TEntity.Create(640,480)

While Not KeyHit(KEY_ESCAPE)
	Cls
	
	ViewZoom :+ (KeyDown(KEY_NUMADD)-KeyDown(KEY_NUMSUBTRACT)) * 0.1
	
	Player.X :+ (KeyDown(KEY_RIGHT)-KeyDown(KEY_LEFT))
	Player.Y :+ (KeyDown(KEY_DOWN)-KeyDown(KEY_UP))
	
	ViewX = -Player.X * ViewZoom + (GraphicsWidth() / 2)
	ViewY = -Player.Y * ViewZoom + (GraphicsHeight() / 2)
	
	For Local Entity:TEntity = EachIn EntityList
		SetColor 255,0,0
		If Entity = Player SetColor 0,255,0
		Entity.Render()
	Next
	
	Flip
	FlushMem
Wend


As it seems, that you have only want to change the distance between the Items but not change their size, I have corrected your code:

Global objList:TList=CreateList()

Graphics 640,480,0,60

Global s#=1.0

For Local i=1 To 25
	Local o:obj=New obj
	o.x=Rand(-GraphicsWidth(),GraphicsWidth())
	o.y=Rand(-GraphicsHeight(),GraphicsHeight())
Next

Global youX#,youY#


While KeyHit(KEY_ESCAPE)=0
Cls	
	If KeyDown(KEY_NUMADD) Then s:+0.1
	If KeyDown(KEY_NUMSUBTRACT) Then s:-0.1
	If s<0.1 Then s=0.1
	If s>4 Then s=4	
	
	
	If KeyDown(KEY_UP) Then youY:-1.0*s
	If KeyDown(KEY_DOWN) Then youY:+1.0*s
	If KeyDown(KEY_LEFT) Then youX:-1.0*s
	If KeyDown(KEY_RIGHT) Then youX:+1.0*s
	
	
	updateObjs()

	SetColor 0,255,0
	DrawRect (GraphicsWidth()/2)-3,(GraphicsHeight()/2)-3,7,7
	
	SetColor 255,255,255;DrawText s,0,0
	DrawText "Press +/- on the numpad to zoom. Cursors to move",0,10
Flip
Wend
End


Type obj
	Field x,y
	
	Method New() ListAddLast(objList,Self); End Method
	Method update()
		Local dx=(youX*s)-GraphicsWidth()/2  'here you have forgotten the scalefactor
		Local dy=(youY*s)-GraphicsHeight()/2 'here you have forgotten the scalefactor
		SetScale 1,1
		SetColor 255,0,0
		DrawRect (x*s-3)-dx,(y*s-3)-dy,7,7
	End Method
End Type	

Function updateObjs()
	For Local i:obj=EachIn objList
		i.update()
	Next
End Function


Take a look to obj.update() method.

That's fantastic! thank you both for taking the time to help me. I really appreciate it. You'll be in the credits.

Thanks to ALL of you guys! I was looking to re-write code for EXACTLY this same routine. I always put it off, and here you guys have written up for all of us. THANKS!!!

Very nice and easy to change into zoom-in/out...
Global objList:TList=CreateList()

Graphics 640,480,0,60

Global s#=1.0

For Local i=1 To 25
	Local o:obj=New obj
	o.x=Rand(-GraphicsWidth(),GraphicsWidth())
	o.y=Rand(-GraphicsHeight(),GraphicsHeight())
Next

Global youX#,youY#


While KeyHit(KEY_ESCAPE)=0
Cls	
	If KeyDown(KEY_NUMADD) Then s:+0.1
	If KeyDown(KEY_NUMSUBTRACT) Then s:-0.1
	If s<0.1 Then s=0.1
	If s>4 Then s=4	
	
	
	If KeyDown(KEY_UP) Then youY:-1.0*s
	If KeyDown(KEY_DOWN) Then youY:+1.0*s
	If KeyDown(KEY_LEFT) Then youX:-1.0*s
	If KeyDown(KEY_RIGHT) Then youX:+1.0*s
	
	
	updateObjs()

	SetColor 0,255,0
	DrawRect (GraphicsWidth()/2)-3,(GraphicsHeight()/2)-3,7*s,7*s
	
	SetColor 255,255,255;DrawText s,0,0
	DrawText "Press +/- on the numpad to zoom. Cursors to move",0,10
Flip
Wend
End


Type obj
	Field x,y
	
	Method New() ListAddLast(objList,Self); End Method
	Method update()
		Local dx=(youX*s)-GraphicsWidth()/2  'here you have forgotten the scalefactor
		Local dy=(youY*s)-GraphicsHeight()/2 'here you have forgotten the scalefactor
		SetScale 1,1
		SetColor 255,0,0
		DrawRect (x*s-3)-dx,(y*s-3)-dy,7*s,7*s
	End Method
End Type	

Function updateObjs()
	For Local i:obj=EachIn objList
		i.update()
	Next
End Function


Thanks some more!!! ;)