Crop image to increase performance

BlitzMax Forums/BlitzMax Beginners Area/Crop image to increase performance

hub(Posted 2010) [#1]
Hi !

When i produce image strip for my game i only use 32x32,64x64,256x256 or 512x512px for each frame cell. (it's easy to do this because i produce the animated sprite imagestrip with lightwave). In fact i put the sprite image at the frame center. i choose the square image size that fit well with the original sprite size.

but to increase performance , do you thing that is better to crop the strip image ? (next when i use collideimage, drawimage, loadimage, ...)

Many Thanks
]

Last edited 2010


Rick_72(Posted 2010) [#2]
Using image sizes of 2^n is always a good choice. Though I dont know the internal mechanisms of Blitz reg. its image handling. Maybe you could make a performance check with two seperate routines: one is drawing say 10^7 images of size 100*200 and the other using size 256*256 (not sure if areas should be of same size).

If you really want to increase performance don't use ImageCollide. As far as I understood you use square sprites (even if you dont). It's better to check the sprite's distance to another. You could simply use a circular distance around the sprite to check wether it's collided or not. Let sprite be a type containing all your stuff like center and radius:

If Sqr(Sq(sprite1.x-sprite2.x)+Sq(sprite1.y-sprite2.y))<sprite1.radius+sprite2.radius Then Collision(sprite1,sprite2)


Loadimage is only relevant at startup.
DrawImage should best work with sizes of 2^n since I guess Blitz is using DX in the background. Im not sure.



But: better ask the real pros! Someone here does know how this stuff works exactly.


ImaginaryHuman(Posted 2010) [#3]
You ideally want to put many images onto ONE image and then draw portions of that image instead of just DrawImage, I forget what that new command was, DrawImageRect? You want to minimize the number of times that the hardware has to switch to a different image. That's more of a penalty than the squareness.


hub(Posted 2011) [#4]
Thanks for the help. I already use a box collsion system into my game. But need to optimize it. The problem with the rick_72 method is that the sqr is very slow (sphere collision). I know that there is somewhere here a good code for collision but i forgot where it is.

Is it possible to rotate a collision box and check if an x,y point is inside it ? (as the ennemies shop rotate into my game).

Thanks ! And happy new year !


jhans0n(Posted 2011) [#5]
Regarding the sphere collision method, if Sqr is too slow, how about just leaving the first part of the equation as is and squaring the second part?

As for the sizes of the images, if you put a whole bunch of images into a single power of 2 sized image, and use DrawSubImageRect to draw them, I would expect that it wouldn't matter what size the smaller images are.

Last edited 2011


hub(Posted 2011) [#6]
Thanks

is it possible to rotate a collision box and check if an x,y point is inside it ? So i could add to my game level editor the possiblity to draw a bounding collision box for each ennemy model.Inside the game, compute if the player shoot is inside this collision box. This is classic, My problem is to know how to check if a point is inside this box when the ennemy - ie the box - is rotated.


hub(Posted 2011) [#7]
Perhaps this :
http://mathcentral.uregina.ca/QQ/database/QQ.09.06/h/graham1.html

but i don't know how to adapt this to bmax..


jhans0n(Posted 2011) [#8]
I'm not aware of a way to rotate the collision box if you're trying to do bounding box collisions. I would think that if you need that level of accuracy in your collisions, you might as well just be doing pixel perfect collision.

Could you just test the unrotated box, and if it's a hit, then do a pixel perfect check? That way, the bulk of your tests would be using the faster method, and it only uses the slower one when needed.


hub(Posted 2011) [#9]

Could you just test the unrotated box, and if it's a hit, then do a pixel perfect check? That way, the bulk of your tests would be using the faster method, and it only uses the slower one when needed.


i go for this method too. Perhaps somebody here could help me later to check the point inside a rotated collision box !


Jesse(Posted 2011) [#10]


Last edited 2011

Last edited 2011


jhans0n(Posted 2011) [#11]
deleted

Last edited 2011


Jesse(Posted 2011) [#12]
I doubt that very very much but you can always try to prove me wrong. ;)
I think you should look at the module source for collideImage or imagescollide before you even consider making a test to prove me wrong. you will be surprised.

Last edited 2011


hub(Posted 2011) [#13]
many thanks for your help.


hub(Posted 2011) [#14]
how to trace our own rotated box (with drawline inside ;-) ?

function Trace_rotated_box (x1,y1,x2,y2, angle, OriginX, OriginY) ...

cos sin ???? and without setrotation inside !

Thanks ! i'm crap with maths !

Last edited 2011

Last edited 2011


Jesse(Posted 2011) [#15]
I kind of thought you were going to do that but I figured you would be able to figure it out from there. guess not :)

here it is then:


this equation :

		px = x*Cos(angle) - y*Sin(angle) 
		py = y*Cos(angle) + x*Sin(angle)


rotates point (x,y) around vector (0,0) angle number of degrees from its current location.


hub(Posted 2011) [#16]


To explain more what i'm doing.
Yesterday : i test if the ship or ship shoot is inside the yellow box before use a imagescollide test. Works fine and fast.

Today : For the animated beam (aligned to the player position) i only use a imagescollide test. now to improve this I only need to test if the player is inside the white bouding box. Note also how could be a rotated bounding box arround the ennemy arm.

i want to trace the rotated bounding box just to know if it is the good position into the game.

Last edited 2011


hub(Posted 2011) [#17]
Graphics 800,600

SetColor 255,0,0
While Not KeyDown (Key_escape)

	Tracer_boite_angle (100,100,300,500,45)
	
	Flip
Wend 


Function Tracer_boite_angle (pbox_xmin#,pBox_ymin#,pBox_xmax#,pBox_ymax#, angle)

	Local temp#

	If pbox_xmax# < pbox_xmin# Then
	
		temp# = pbox_xmax#
		pbox_xmax# = pbox_xmin#
		pbox_xmin# = temp#
		
	End If

	If pbox_ymax < pbox_ymin Then
	
		temp# = pbox_ymax#
		pbox_ymax# = pbox_ymin#
		pbox_ymin# = temp#
		
	End If
	
		'px = x*Cos(angle) - y*Sin(angle) 
		'py = y*Cos(angle) + x*Sin(angle)

	pBox_xmin# = pBox_xmin# * Cos(angle) - pBox_ymin * Sin(Angle)
	pBox_ymin# = pBox_ymin# * Cos(angle) + pBox_xmin * Sin(Angle)
	
	pBox_xmax# = pBox_xmax# * Cos(angle) - pBox_ymax * Sin(Angle)
	pBox_ymax# = pBox_ymax# * Cos(angle) + pBox_xmax * Sin(Angle)

	DrawLine pBox_xmin#, pBox_ymin#, pBox_xmax#, pBox_ymin#
	DrawLine pBox_xmax#, pBox_ymin#, pBox_xmax#, pBox_ymax#
	DrawLine pBox_xmin#, pBox_ymax#, pBox_xmax#, pBox_ymax#
	DrawLine pBox_xmin#, pBox_ymin#, pBox_xmin#, pBox_ymax#
		
End Function

what's wrong with this ? (and how to set the facultative rotation origin)
Thanks !

Last edited 2011

Last edited 2011


Rick_72(Posted 2011) [#18]
Store pBox_x/y in local, rotate and push the values back to pBox:

local x#,y#

x = pBox_xmin# * Cos(angle) - pBox_ymin * Sin(Angle)
y = pBox_ymin# * Cos(angle) + pBox_xmin * Sin(Angle)

pBox_xmin# = x
pBox_ymin# = y



That'll help!


hub(Posted 2011) [#19]

this seems not be the solution !


Jesse(Posted 2011) [#20]
sorry hub, I should have just given you what you wanted and avoided all of this problems but I wanted you to understand the principle behind it.


Function Tracer_boite_angle (x#,y#,image:TImage, angle#)

	'px = x*Cos(angle) - y*Sin(angle) 
	'py = y*Cos(angle) + x*Sin(angle)
		
	Local c# = Cos(angle)
	Local s# = Sin(angle)
	Local x1# = -image.handle_x
	Local y1# = -image.handle_y
	Local x2# = image.width-image.handle_x
	Local y2# = -image.handle_y
	Local x3# = image.width-image.handle_x
	Local y3# = image.height-image.handle_y
	Local x4# = -image.handle_x
	Local y4# = image.height-image.handle_y
	
	Local px1# = x + x1# * c - y1# * s
	Local py1# = y + y1# * c + x1# * s
	Local px2# = x + x2# * c - y2# * s
	Local py2# = y + y2# * c + x2# * s
	Local px3# = x + x3# * c - y3# * s
	Local py3# = y + y3# * c + x3# * s
	Local px4# = x + x4# * c - y4# * s
	Local py4# = y + y4# * c + x4# * s
	DrawLine px1,py1,px2,py2 
	DrawLine px2,py2,px3,py3
	DrawLine px3,py3,px4,py4
	DrawLine px4,py4,px1,py1
		
End Function



Last edited 2011


hub(Posted 2011) [#21]
Jesse why do you use an image into this function ?

Last edited 2011


hub(Posted 2011) [#22]
Jesse is this function stay 'compatible' with an image setting on middhandleimage and frame animated ?

Last edited 2011


Jesse(Posted 2011) [#23]
isn't that what you want to do? you want to frame the image right?
the problem you are having is because you want to rotate the image not the location of the image.
the way you are doing it, it's going to rotate the pBox_xmin etc. relative to origin(0,0) but you want to rotate the box not the position of the box.
in order to rotate the box you need to find all four corners of the unrotated box then rotate it along its handle to get it right. the reason I passed the image is because i needed the image specifications to figure out the four corner coordinates and that is what the x1,y1 x2,y2 etc. does.

and yes it stays compatible with the image settings midhandleimage or any image handle you choose.

Last edited 2011


hub(Posted 2011) [#24]




Try this : strange lines !

Last edited 2011

Last edited 2011


Jesse(Posted 2011) [#25]
my stupid mistake. I had some variables transposed in the function. I fixed it already. try it now should work.


hub(Posted 2011) [#26]
animated ennemi strip here

Last edited 2011


Jesse(Posted 2011) [#27]
try it with the corrected code in post #20


hub(Posted 2011) [#28]

Now i'm trying to check a point x,y inside the box...


hub(Posted 2011) [#29]

Yes ! this seems ok. Many thanks Jesse.


Jesse(Posted 2011) [#30]
good :).