Image shakes?

BlitzMax Forums/BlitzMax Programming/Image shakes?

Arska(Posted 2015) [#1]
I got some strange shaking in some images of my project when player is moving. UI is not shaking, but these are:


They are positioned this way:
ScreenX = myplayerX - VirtualResolutionWidth() / 2
ScreenY = myplayerY - VirtualResolutionHeight() / 2


Save item positions:
itemX = (ScreenX - 30) + ix
itemY = ScreenY + VirtualResolutionHeight() - 30


Drawing:
DrawImage image, itemX,itemY
DrawText itemSlot, itemX, itemY
DrawRect itemX-20, itemY-30,PercentFrom:Float(cooldown, weapCooldown)/2.5,10



How can i prevent this shaking when player is moving?


Derron(Posted 2015) [#2]
you draw the images at "itemx, itemy"

itemX is defined as "screenX + ix" (i do not know "ix")
if "ix" is not constant, then of course it moves according to the "ix" value

same for itemY - it is defined as "screenY + constant value"

So ... seems ScreenX and ScreenY change each time ... seems "myplayerX" and "myplayerY" are something which changes during updates (eg. player coordinates)


Conclusion: you are drawing at coordinates which are relative to player position - which leads to "movement".

If you place objects at fixed positions, it wont "shake".


bye
Ron


Arska(Posted 2015) [#3]
'ix' is just value is this:
For Local i=1 To 24
	ix = 40 * i + 40



Arska(Posted 2015) [#4]
Also minimap has shaking 'effect'
Function DrawMapBox(mapx:Int, mapy:Int, mapSizeX = 200, mapSizeY = 200)
	Local mapSX:Float = mapSizeX
	Local mapSY:Float = mapSizeY
	
	
	playermapx:Float = (mapSX / worldSizeX) * myplayerX
	playermapy:Float = (mapSY / worldSizeY) * myplayerY
	
	
	SetAlpha 0.6
	'DrawRect mapx , mapy, mapSX, mapSY
	
	
	
	If TRock.List Then
		SetColor 50,50,50
		For Local MRock:TRock = EachIn TRock.List
			
			If MRock.x > 0 And MRock.y > 0 And MRock.x < worldSizeX And MRock.y < worldSizeY And Distance( myplayerX, myplayerY , MRock.x, MRock.y ) < maxViewDistance And MRock.destroyed = False
				rockmapx:Float = (mapSX / worldSizeX) * MRock.x
				rockmapy:Float = (mapSY / worldSizeY) * MRock.y
		
				DrawRect rockmapx + mapx, rockmapy + mapy, 2, 2
			EndIf
	
		Next
	EndIf
	
	
	
	
	
	SetColor 0, 255, 0
	If myplayerX > 0 And myplayerY > 0 And myplayerX < worldSizeX And myplayerY < worldSizeY
		DrawRect playermapx + mapx, playermapy + mapy, 5, 5
	EndIf
End Function



Derron(Posted 2015) [#5]
Ok first drawrect:
DrawRect rockmapx + mapx, rockmapy + mapy, 2, 2

rockmapX + mapX
-> rockmapX = unknown value
-> mapX = unknown param

So I cannot recognize the source of the "shaking".


Shaking means: your coordinates are changine during the render calls. Eg. they are recalculated and one of the factors is another coordinate of a moving entity.


bye
Ron


Arska(Posted 2015) [#6]
Local rockmapx:Float = (mapSX / worldSizeX) * MRock.x
Local rockmapy:Float = (mapSY / worldSizeY) * MRock.y
		
DrawRect rockmapx + mapx, rockmapy + mapy, 2, 2


Calling function:
DrawMapBox(ScreenX + VirtualResolutionWidth() - 350, ScreenY + VirtualResolutionHeight() - 293, 280, 240)


And this isn't shaking at all so i don't think it's screenX or Y:
DrawText "FPS: " + fps, ScreenX + VirtualResolutionWidth() - 150, ScreenY + 20


I have tried everything, but i just can't get rid of this.


Derron(Posted 2015) [#7]
Local rockmapx:Float = (mapSX / worldSizeX) * MRock.x
Local rockmapy:Float = (mapSY / worldSizeY) * MRock.y


Both values contain of an assumingly constant "mapS*/WorldSize*" and a changing "MRock.x"

Do these rocks move? If yes, they move of course also the "rockmapX"

If that is ok (because this part draws the small rocks on the map) I assume that you somewhere change the origin of everything.

Do you use "SetOrigin" ? Maybe you do not reset it correctly when finishing your work.


bye
Ron


Arska(Posted 2015) [#8]
Yes i use setorigin:
SetOrigin - myplayerX + VirtualResolutionWidth() / 2, - myplayerY + VirtualResolutionHeight() / 2


What do you mean? Reset?


Derron(Posted 2015) [#9]
SetOrigin(-10,-10) means that if you Draw something at "0,0" it is offset to -10,-10.

Now you set your Origin each time to "playerX + center, playerY + center".

If that player moves, the "Origin" moves.


If you want to to draw things at a fixed spot on the SCREEN, not on the "virtual canvas" then you will have to:
a) set origin back to "0,0" before doing it
'backup current origin
local ox:int, oy:int
GetOrigin(ox, oy)

'reset origin
SetOrigin(0,0)

'do my absolute positioned drawings
...

'set backup origin again
SetOrigin(ox, oy)



or
b) subtract the origin from the coordinates you use
local ox:int, oy:int
GetOrigin(ox, oy)

DrawRect x - ox, y - oy, 100, 100


bye
Ron


Arska(Posted 2015) [#10]
And i did something wrong... Map items are floating around the screen and out of it. (option a)


Derron(Posted 2015) [#11]
Do you mean: you found the error.
Or did you mean: After changing to "a)" items are now floating around ?


bye
Ron


Arska(Posted 2015) [#12]
Yes items are drawn to wrong place and not inside the "map". if i move player enough, those items go off screen.


Derron(Posted 2015) [#13]
I like it if people answer an "OR"-question with "yes..." :p

I asked: did you find the error - OR - did you just examined the error to be of "type a" (which normaly implies that you found the error ...)


All in all: make yourself clear what values you use, if they are "absolute" (0,0 based) or if you have to adjust them to some origin.

Then do something in the likes of:
DrawRect(100,100,100,100) before drawing your minimap.

If that Rect is "shaking" then something is moving your origin. Play with the DrawRect-coordinates to make the DrawRect "fixed".

As you do not know what changes the origin (so it seems) your best bet is to backup the origin, reset it to 0,0 and after doing your "0,0"-based drawings, you set it back to what it was before (which is approach "a)").

I personally never used "SetOrigin" on a global scale ("world wide") but have eg. a "TWorld" having a x,y. This world is then the parent of its children and offsetting things. The "Origin" thing works too ... if you eg. have a world which sets the origin before drawing/handling everything in this world, and afterwards resets the origin (like you do with colors, alpha ...).

After you have drawn your "world" the "hud" will get drawn - and will find a nicely "default" origin (0,0).

A global "origin" is only useful if the whole screen has to "shake" (explosion).


bye
Ron


Arska(Posted 2015) [#14]
Alright now i tried to do this way item placement in "inventory"
Function DrawInventory()
		Local ox:Float, oy:Float
		GetOrigin(ox, oy)
		
		setorigin 0, 0
		If TItem.list
			For Local NDItem:TItem = EachIn List
				NDItem.Draw()
			Next
		EndIf
		
		setorigin ox, oy

	End Function


Now shaking is gone with those items, but all ships and stuff are in wrong place althought i resumed back to old origin.


Arska(Posted 2015) [#15]
hmm... if i put set origin to 0,0 before drawing to screen and after drawing like this:
SetOrigin - myplayerX + VirtualResolutionWidth() / 2, - myplayerY + VirtualResolutionHeight() / 2


Everything seems to be in order.


Derron(Posted 2015) [#16]
Because you normally do not know the formula of the last SetOrigin-Call, I suggested to use the backup-portion I wrote for "a)"

It has to work exactly the same way as your code.


So if it makes "everything to be in order" then you know the source of that bug: the origin is adjusted, but your the coordinates you use for drawing the "hud elements" do not take this into consideration.

so instead of resetting the origin you even might subtract the origin-coordinates from the ones you use to draw the hud-elements.


There are plenty ways to rome - some are long, some are difficult but short.


bye
Ron


Arska(Posted 2015) [#17]
I got it finally working! Thank you so much again! :D Now i understand setorigin.