Rotation Script for Image in Real Time?

Blitz3D Forums/Blitz3D Beginners Area/Rotation Script for Image in Real Time?

Apollonius(Posted 2003) [#1]
Example if I take a Ship for example if U have one image of the ship facing up, as I read you cannot use the rotation command in real time, so I though maybe rotate, copy the image and assemble them to create a 360 degree rotation on KeyPress Left,Right.

This is just an Idea, but what I want is to see a code example of any kind, could someone show me how its done?
^__^ thanks [using blitzplus of course]


semar(Posted 2003) [#2]
Hi Kaisuo,
the best way would be to produce pre-rendered images, store it in an array of images, and then use it in real time.

An example could be:
dim arr_img(360) ;360 images
image = loadimage("ship.bmp")
for n = 1 to 360
rotateimage image,n
arr_img(n) = image
next

;now if you want to display the image ship at x,y, and rotated of 45 degree, you just need:
DrawImage arr_img(45),x,y


Hope this has sense for you,
Sergio.


WolRon(Posted 2003) [#3]
Take note that 360 images is a LOT of video memory. If each image was only 1K (32x32), you would consume 360K of video memory for just one sprite. Try 180, 90, 45, 22.5 or something.


Ross C(Posted 2003) [#4]
There's a dll or userlib for this type of thing isn't there. Can't remember who wrote it but it allows real time rotation and scaling. Peter Scheltus(SP?) or something...


Apollonius(Posted 2003) [#5]
maybe draw the ship in rotation put it in a tile image and make the animation with it cause i dont really need 360 images to make a 360 rotation effect do i?

dunno how i would exactly do it with an image of 32x32 ship image on it


Apollonius(Posted 2003) [#6]
Okay I drew a ship for my game, I setted to bad quality so no one could really steal it.

So what I want is that it rotates on right,left arrow key of course on right it rotates right and on left it rotates left but on up arrow it moves but u need to keep ur finger on the up arrow on release it stops, how would I do that using this below:


50k is better then 360k isnt it ^^

Make the code simple for stupid people, like me ;)


WolRon(Posted 2003) [#7]
Left and right are very simple. Just advance to the next/previous frame depending on which way you want to turn and in/decrement a variable that holds the angle (looks like you chose angles of 10 degrees).

I'm at work and don't have the time right now to figure it out but you could use Sin and Cos to figure out which direction to move when you press the up button (how much in x plus or minus, how much in y plus or minus.


Paradox7(Posted 2003) [#8]
Hi there, i wrote this real fast using your image. I'll explain at the bottom of this code.

Graphics 800, 600, 16
SetBuffer BackBuffer()
ship = LoadAnimImage ("shipro.png", 113,113,0,36)

startframe = 27
frame = 27

Repeat 

	If speed# < 0 Then speed# = speed + .01
	If speed# > 0 Then speed# = speed - .01

	If KeyDown(200) Then ;UP Arrow
		speed# = speed + .05
			If speed# > 2 Then Speed# = 2
	End If 
	
	If KeyDown(208) Then ;Down Arrow
		speed# = speed - .05
			If speed# < -2 Then Speed# = -2
	End If
	
	If timer+50 < MilliSecs() Then
		
		timer = MilliSecs()

		If KeyDown(203) Then ;Left Arrow
			frame = frame - 1
			If frame < 0 Then frame = 35
		End If
		
		If KeyDown(205) Then ;Right Arrow
			frame = frame + 1
			If frame > 35 Then frame = 0
		End If
		
	End If 


	angle = (frame-startframe)*10

	x# = x# + Cos(angle)*speed#
	y# = y# + Sin(angle)*speed#
	
	DrawImage ship, x#, y#, frame

	Flip
	Cls
Until KeyHit(1)


Ok, first off there is a few problems with your image, you have it bordered with the white lines, best not to, have them equally spaced, if its a 113x113 image, then they should be spaced as so, you will see the white border in the little demo, and the ships are slightly offset because you can't grab and save to a animation correctly. Anyways, just fix your image, plug it in, and it will be perfect :D
if a 113x113 frame is layed out the same as you have it, 10x4, then it should be a 1130pixel By 452pixel Image.


Also your image starts off at the wrong frame, in this example, it goes like this
---/270\
180-----0/360
---\90 /

0 or 360, points right, and goes clockwise
thats why you see the Startframe varible, its just bascially a offset, so i get the correct image out of your image, it would not be needed if your first image started right and rotated clockwise.
x# = x# + speed#*Cos((frame-startframe)*10)
y# = y# + speed#*Sin((frame-startframe)*10)

none of it has to be floats, just works better for smoother effects, as you can have .1 .whatever

actually if you change the signs you can make it start and rotate in different directions, for example
x# = x# - speed#*Cos((frame-startframe)*10)
y# = y# - speed#*Sin((frame-startframe)*10)
starts Left and rotates clockwise as you increase 0 to 360

a mixed + - makes it rotated counterclock wise, anyways, just mess with it, you will see

basic formual is

x = x + Cos(ANGLE)*Offset
y = y + Sin(ANGLE)*Offset

Pretty simple, Give it a go, just copy and paste the code and with your image in the dir, and run it, you move with foward with the Up key, down with the down key, rotate left and right with the left/right arrow keys. Good Luck

p.s. this isn't perfect code, just something wiped up ;D


Paradox7(Posted 2003) [#9]
oh just wanted to say, the methods used to control speed, and the little slow down may not be the best, just did it real quick, some way better space physics out there somewhere :P

Also to explain that Timer i used, its just there to delay the check for pressing left or right, without it, your ship would spin really fast, rotate really fast, so i put that a delay of 50millsecs, was a nice speed for rotation. Its basically a Animation Timer.


Apollonius(Posted 2003) [#10]
Well its very hard to center the images if u dont have anything to separe them :( if there was a good program for that, that would sure help with B+


Paradox7(Posted 2003) [#11]
Here you go, Somebody else here on the forums posted this a long while back, I forgot where and who, but it works perfectly

Function ExportRotatedImage(Image1,Frames,FileName$,MaskRed,MaskGreen,MaskBlue)
Local PreviousBuffer = GraphicsBuffer()
Local PreviousRed = ColorRed(),PreviousGreen = ColorGreen(),PreviousBlue = ColorBlue()
Local Image2
Local Angle#,AngleStep#
Local MaxWidth,MaxHeight
Color MaskRed,MaskGreen,MaskBlue
If Image1
	AngleStep# = 360.0 / Frames
	Angle# = 0
	For i = 1 To Frames
		Image2 = CopyImage(Image1)
		MidHandle Image2
		RotateImage(Image2,Angle#)
		If ImageWidth(Image2) > MaxWidth Then MaxWidth = ImageWidth(Image2)
		If ImageHeight(Image2) > MaxHeight Then MaxHeight = ImageHeight(Image2)	
		FreeImage(Image2)
		Angle# = Angle# + AngleStep#
	Next
	ImgResult = CreateImage(MaxWidth * Frames,MaxHeight)
	SetBuffer ImageBuffer(ImgResult)
	Rect 0,0,MaxWidth * Frames,MaxHeight
	Angle# = 0
	For i = 0 To Frames - 1
		Image2 = CopyImage(Image1)
		MaskImage Image2,MaskRed,MaskGreen,MaskBlue
		MidHandle Image2
		RotateImage(Image2,Angle#)

		CopyRect(0,0,ImageWidth(Image2) - 1,ImageHeight(Image2) - 1,MaxWidth * i + 0.5 * MaxWidth,0.5 * MaxHeight,ImageBuffer(Image2),ImageBuffer(ImgResult))

		FreeImage(Image2)
		Angle# = Angle# + AngleStep#		
	Next
	SaveImage(ImgResult,FileName$)
	FreeImage(ImgResult)
EndIf
SetBuffer PreviousBuffer
Color PreviousRed,PreviousGreen,PreviousBlue
Return MaxWidth
End Function

;Change this to your needs
Image = LoadImage("YOURIMAGE.png")
TFormFilter False 
Size = ExportRotatedImage(Image,36,"Result.bmp",255,0,255)

NewImage = LoadAnimImage("Result.bmp",Size,Size,0,36)


now the only thing you should change is YOURIMAGE.png, set it to your image, and then in the

Size = ExportRotatedImage(Image,36,"Result.bmp",255,0,255)

the 36 is how many frames you want, so 36 is correct, that'll give you a 10 degree rotation, the 255, 0, 255 is the images Mask, if you want a mask.

and thats it, basically plug in your picture, and run, window will pop up, and then close when its done, and look in your Directory, and you will have a perfectlly Created Animation frame to use for rotation, it'll be named Result.bmp :P

I suggest once its made, to open it in say paintshop, and resave it as a PNG, save some space.

This will create all your rotation frames for you, so just give it a SINGLE picture of your ship, of your ship pointing Right, just 1, it will take that 1, and create all the other rotated frames, and glue them together into one image.


Apollonius(Posted 2003) [#12]
This is to heavy, I'm not that advance yet, plus this looks weird to me, I don't like to use code I dont understand... Anyhow I'll figure a way if I have question Ill make a new post later...


Paradox7(Posted 2003) [#13]
who cares how the creation of the rotation image works, that wasn't the point, just Use it to make your IMAGE for you, i mean its like any other tool, i could make it a .exe, you open it, give it your image, and it'll output your new image for you, how hard is that? You wanted a tool to make the rotation image for you, basically the same thing you Posted above, but perfectly made without the whiteborders. Well THERE is the tool right there. I don't get whats to advance here :P Don't try to understand it, just use it as a tool, like it was meant to be.

But at any rate, good luck.


Apollonius(Posted 2003) [#14]
Not the point I need practice for ISOMETRIC Ship rotation, u cant just rotate the ship wont make it Isometric will it.


Paradox7(Posted 2003) [#15]
ahhhh, ISOMETRIC, you said nothing of that. Yup that makes a difference, you'll have to make each frame yourself in that case.


Apollonius(Posted 2003) [#16]
The ship above is not isometric but Im practicing for future isometric ships :)


tonyg(Posted 2003) [#17]
Paradox7... unless it's a 3D model of a ship....;-)


Paradox7(Posted 2003) [#18]
yes true, then you could use Blitz3D to spin it and grab shots :D have it paste it together, and there ya go, a final isometric animation image, I remember seeing some code somebody posted that did exactly that, don't know exactly where at the moment. :P


Neo Genesis10(Posted 2003) [#19]
If you have Blitz 3D, why dont you just position 3D models to give an isometric perspective? Failing that, just load in individual models, position the camera correctly and save a screenshot. Bingo - one isometric frame.


Paradox7(Posted 2003) [#20]
didn't i just bascially say that? :P hehe, and to which tonyg was implying to also. If you have a 3D model of the object you want, you can use Blitz3D or actually any 3D Program, position it correctly, take snapshot and there you go, an Isometric frame, although i think that someone actually made a little program in blitz3D, that did the entire steps for you, just run, and it done, positions correctly, rotates snaps, rotates snaps, ect, ect, pastes it all together, and there you have a finished image strip when its done, of a isometic 3D object from all the angles you need :D


Apollonius(Posted 2003) [#21]
Actually I'm using 3D models for 2D project, since I don't have Blitz3D, I wish I did but I don't have the money for it right now so, Im using 3D with BlitzPlus.

So I have to save a billion image of the 3D sprite in Maya to get a rotation :|

However here is the sprite without the white lines!



Ross C(Posted 2003) [#22]
To correct my post, the real time rotation/scaling and transparency thingy was Turtles (Patrick Lesters) User Lib.

Get it here:

http://www.blitzbasic.com/bbs/posts.php?topic=21685


Apollonius(Posted 2003) [#23]
Nice but I'm not searching for any dll.


Paradox7(Posted 2003) [#24]
what are you using to make your frames with? They are still not centered correctly, you can see this by putting the image into the demo i posted the very first time, and just pressing the right or left arrow key, they don't rotate that smoothly but jump just slightly off between rotation, thats because you don't have your images centered correctly. If i had somewhere to put up this picture i made of your ship, rotated correctly, you will see what its suppose to look like in the demo, it should be smooth rotation, and not all jerky because each frame isn't centered with the origin.


Apollonius(Posted 2003) [#25]
I know it doesnt look quite right lol.
I'm using PhotoShop,
I'm trying to fix it, should be good soon :).


Apollonius(Posted 2003) [#26]
hmmm, cant get perfect rotation with the time of image i showed u above >.>

http://www.lexdark.com/movie1.swf < thats how its suppose to be done