I am making a game in which you play a tank. I was wondering, how do you get the graphic to rotate? I have looked at tip 104 in VBworld, but that wasn't any help at all. Please help!
Printable View
I am making a game in which you play a tank. I was wondering, how do you get the graphic to rotate? I have looked at tip 104 in VBworld, but that wasn't any help at all. Please help!
This may help (from vbcode)
VB Code:
Const SRCCOPY = &HCC0020 Const Pi = 3.14159265359 Private Declare Function SetPixel Lib "GDI32" (ByVal hDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal crColor As Long) As Long Private Declare Function GetPixel Lib "GDI32" (ByVal hDC As Integer, ByVal X As Integer, ByVal Y As Integer) As Long Private Declare Function StretchBlt% Lib "GDI32" (ByVal hDC%, ByVal X%, ByVal Y%, ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%, ByVal YSrc%, ByVal nSrcWidth%, ByVal nSrcHeight%, ByVal dwRop&) ' add three command buttons and two pictureboxes. Load a bitmap into picture1 in design ' mode. Set both box to the same size. Routines execute 3 times faster than routines ' found in Microsoft's Knowledge Base. 'Sub Form_Load () Picture1.ScaleMode = 3 Picture2.ScaleMode = 3 'End Sub 'Sub Command1_Click () 'flip horizontal picture2.Cls px% = picture1.ScaleWidth py% = picture1.ScaleHeight retval% = StretchBlt(picture2.hDC, px%, 0, -px%, py%, picture1.hDC, 0, 0, px%, py%, SRCCOPY) 'End Su 'Sub Command2_Click () 'flip vertical picture2.Cls px% = picture1.ScaleWidth py% = picture1.ScaleHeight retval% = StretchBlt(picture2.hDC, 0, py%, px%, -py%, picture1.hDC, 0, 0, px%, py%, SRCCOPY) 'End Sub 'Sub Command3_Click () 'rotate 45 degrees picture2.Cls Call bmp_rotate(picture1, picture2, 3.14 / 4) 'End Sub 'Sub bmp_rotate (pic1 As PictureBox, pic2 As PictureBox, ByVal theta!) ' bmp_rotate(pic1, pic2, theta) ' Rotate the image in a picture box. ' pic1 is the picture box with the bitmap to rotate ' pic2 is the picture box to receive the rotated bitmap ' theta is the angle of rotation Dim c1x As Integer, c1y As Integer Dim c2x As Integer, c2y As Integer Dim a As Single Dim p1x As Integer, p1y As Integer Dim p2x As Integer, p2y As Integer Dim n As Integer, r As Integer c1x = pic1.ScaleWidth \ 2 c1y = pic1.ScaleHeight \ 2 c2x = pic2.ScaleWidth \ 2 c2y = pic2.ScaleHeight \ 2 If c2x < c2y Then n = c2y Else n = c2x n = n - 1 pic1hDC% = pic1.hDC pic2hDC% = pic2.hDC For p2x = 0 To n For p2y = 0 To n If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x) r = Sqr(1& * p2x * p2x + 1& * p2y * p2y) p1x = r * Cos(a + theta!) p1y = r * Sin(a + theta!) c0& = GetPixel(pic1hDC%, c1x + p1x, c1y + p1y) c1& = GetPixel(pic1hDC%, c1x - p1x, c1y - p1y) c2& = GetPixel(pic1hDC%, c1x + p1y, c1y - p1x) c3& = GetPixel(pic1hDC%, c1x - p1y, c1y + p1x) If c0& <> -1 Then xret& = SetPixel(pic2hDC%, c2x + p2x, c2y + p2y, c0&) If c1& <> -1 Then xret& = SetPixel(pic2hDC%, c2x - p2x, c2y - p2y, c1&) If c2& <> -1 Then xret& = SetPixel(pic2hDC%, c2x + p2y, c2y - p2x, c2&) If c3& <> -1 Then xret& = SetPixel(pic2hDC%, c2x - p2y, c2y + p2x, c3&) Next t% = DoEvents() Next 'End Sub
No, not really. You know in the GTA series, where the vehicles are in a 3d environment, but the cars aren't actually 3d? well thats what Im doing (with a few changes, of course). I just need (in simpleton terms, cos I'm not too bright :) ) a piece of code that rotates the one graphic so that when a key is pressed (I know how to do that), it will rotate until the key is let go
Thanks for your help anyway, but any other ideas?
James
Use SLH's reply and attach it to a timer instead of command buttons
Use SLH's reply and attach it to a timer, and have a variable increase (to make it spin faster or whatnot) when you press a key instead of command buttons (di I post this twice? stupid slow comp...)
Hello folks,
I was doing a search for a sub for rotating a bitmap through any angle and ran into the nice code above. I've tested it with a timer control for values of theta starting at 0 and increasing in steps of 0.1 radians.
The problem is, for some reason, it sometimes produces run-time error #6 (overflow). But the funny thing is, sometimes it works, sometimes it doesn't, without any changes in between.
The line the overflow occurs most often at (i.e. gets highlighted) is
c0& = GetPixel(pic1hdc&, c1x + p1x, c1y + p1y)
Also at this line:
If c0& <> -1 Then xret& = SetPixel(pic2hdc&, c2x + p2x, c2y + p2y, c0& )
where the SetPixel part is highlighted.
I've just tried it 5 times in a row and while the first, second and fifth gave error 6, the third and fourth were successful.
Oops, the SetPixel and Getpixel declarations have integers instead of longs... no wonder I was getting overflows.
Just wanted to drop by and tell you that it is much fater to rotate that pic at design time, then it is at runtime. So if you are looking for speed thats the way to go...
Well, the thing is the end user will have to apply the rotation to a scanned image so I can't guess beforehand how much it will have to be rotated by. But thanks for the hint.Quote:
Originally posted by NoteMe
Just wanted to drop by and tell you that it is much fater to rotate that pic at design time, then it is at runtime. So if you are looking for speed thats the way to go...
Very true NoteMe, way faster to do as design time.
If you are lazy and don't want to rotate your pic and then save, 360 times....just use the above code and rotate it through every angle and save the new image into a bmp...You can save DC to bmp I know. Do that once, and bam you have 360 angles of your image.....
Seriously it is super laggy to rotate an image, I was using code for a 2D top view shooter, to have the player face the angle of the line he was walking...requires a rotate each click, time_Delta went from 20ms to 220ms during the second it was rotating...
Well you don't have to rotate it 360 times for it to look fluid, I suppose you could, but it shouldn't be necessary. Also, I remember someone made something that was floating around my computer a while ago that rotated and saved the images automatically for you.
That was me and Cyborg...:DQuote:
Originally posted by dsheller
Well you don't have to rotate it 360 times for it to look fluid, I suppose you could, but it shouldn't be necessary. Also, I remember someone made something that was floating around my computer a while ago that rotated and saved the images automatically for you.
Yah I designed my own one to, but the code came for the rotation came from that example you always use to show of DD Rotation from PSC...Not sure of the URL.
I understand what the rotation code does, I just don't get cos/sin ****, I need to sit down and study that again or something, i was either high or skippin school during those lessons...
Actualy it was a friend of mine at an other forum that made that GTA DD rotation demo...I just improved the speed with using pre designed sprites...
I don't mind doing the rotation on the fly since my application is not about animation.
For animation it's true that precomputed images make a difference. However I think the subroutine above would be faster (than it is now, not than precomputing the images) if a table of sin & cos were precalculated and then linear interpolation used.
That is true. Even the Quack 2 engine used a look up table for sin cos. And it is not hard to make. And if you know a bit of cach misses and stuff like that too, you can make that look up table even faster...;)