2d realtime image rotation

Blitz3D Forums/Blitz3D Programming/2d realtime image rotation

bloos_magoos(Posted 2013) [#1]
I know that everybody can do it. Everybody except for me :(

I want to be able to take any image and rotate it in real time, like for example to face the direction the player is walking (think SAS Zombie Assault).

Im pretty sure that the way most people do it is to create an image, and render a rotated image on each frame. So there would be 360 frames, each rotated one degree.

However, I have no idea how to do that. Plus isn't RotateImage() very slow? I do not know.

Thanks

Also, this has nothing to do with any 3d programming whatsoever.


RemiD(Posted 2013) [#2]

Im pretty sure that the way most people do it is to create an image, and render a rotated image on each frame. So there would be 360 frames, each rotated one degree.


wow i hope that you are not planning to create 360 different images !

If the image is always the same, what i would do is create a quad mesh, set proper UV coords, make the image a texture, and texture the quad.
With this you will be able to rotate your image as you want.

This is not designed to do what you want but you can see how to make a quad here :
http://blitzbasic.com/codearcs/codearcs.php?code=3029


Stevie G(Posted 2013) [#3]
If you're not using 3d then you need to do a bit of preprocessing. I'm afraid I rarely work in 2d but here is an example.

I've just created a dummy SourceImage but this is where you would load your own image. You need to make sure there is plenty of space around the edge of the image so that it doesn't get clipped when rotated.

Basically, the demo creates a larger image which has 360 copies of the original image in 360 rotations (would recommend using less by changing the frames parameter). You can save this larger image using saveimage and load it again using the loadanimimage command to save you all the preprocessing.

You could probably make the rotatated versions look better by using tformfiler true too.

Graphics 1024,768,32,1

Global SourceImage = IMAGEcreate()
Global AnimatedImage = ANIM_IMAGEcreate( SourceImage,360 )
Global R = 0

While Not KeyHit(1)

	Cls

	DrawImage SourceImage, 100,100
	DrawImage AnimatedImage, GraphicsWidth()*.5,GraphicsHeight()*.5,R
	R = ( R + 1 ) Mod 360
	
	Flip

Wend
End

Function IMAGEcreate()

	Local Image = CreateImage( 32,32 )

	SetBuffer ImageBuffer( Image )
	Color 255,0,0
	Rect 8,12,16,8,1
	Color 0,255,0
	Rect 8,14,4,4,1
	SetBuffer FrontBuffer()
	MidHandle Image
	
	Return Image

End Function

;=========================================================
;=========================================================
;=========================================================

Function ANIM_IMAGEcreate( Image, Frames )

	Local Sx = ImageWidth( Image )
	Local Sy = ImageHeight( Image )
	Local AnimImage = CreateImage( Sx,Sy,Frames )
	Local L, Angle#, Copy

	For L = 0 To Frames-1
		Angle = 360 * L / Float(Frames)
		Copy = CopyImage( Image )
		RotateImage Copy, Angle
		CopyRect 0,0,Sx,Sy,Sx*.5, Sy*.5,ImageBuffer( Copy ), ImageBuffer( AnimImage,L )
		FreeImage Copy
	Next
	
	Return AnimImage
	
End Function



Rroff(Posted 2013) [#4]
FastExt (and probably fastimage to) has a very fast function for rotating a 2D image I'd use that as nothing your likely to come up with in pure B3D will be in the same league performance wise.


_PJ_(Posted 2013) [#5]
I would seriously consider using less than 360 different values.

Even if your on-screen character is, say, 256x256 pixels in size, the parallax difference between 1 degree is something like 2 pixels which is so slight on even 800x600 monitor (i.e. a factor of 300!) that the difference is negligible.

Many games use 8 frames of rotation with 12 or 16 being pretty much the upper limit. Any more than this and you're really just wasting resources, since players dont tend to need that much definition and it can even make things more frustrating